前端小tip(1):使用css将图片遮罩并在遮罩上显示内容,并添加动画缓入

效果图

如图,当鼠标悬浮在图片上时,显示遮罩并展示具体内容

在这里插入图片描述

全部代码

<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
      
      
            width: 500px;
            height: 500px;
            border-radius: 40px;
            overflow: hidden;
            background-color: rgb(216, 135, 135);
            position: relative;
        }

        .img {
      
      
            width: 100%;
            height: 100%;
            border-radius: 30px;
        }

        .mask {
      
      
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            text-align: center;
            vertical-align: auto;
            background: rgba(0, 0, 0, 0.35);
            color: #ffffff;
            opacity: 0;
        }
        
        a:hover .mask{
      
      
            animation: maskframes 700ms ease-in-out forwards;
        }

        @keyframes maskframes {
      
      
            0% {
      
      
                opacity: 0;
            }
            100% {
      
      
                opacity: 1;
            }
        }
    </style>
</head>

<body>
    <div class="box">
        <image class="img"
            src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"></image>
            <a href="#">
                <div class="mask">
                   <h3>A Picture of food</h3>
                </div>
            </a>
    </div>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/languageStudent/article/details/127622700