2 个回答

大致原理是一样的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .imgbox,.imgbox2{
            width:200px;
            height: 140px;
            position:relative;
            overflow: hidden;
        }
        img{
            width:100%;
        }
        .imgbox:after{
            position:absolute;
            left: 0;
            top:0;
            display: block;
            width:100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            content: attr(data-text);
            transition:all 0.4s ease;
            transform: translateY(-100%);
            color: #FFF;
        }
        .imgbox2:after,.imgbox2:before{
            position:absolute;
            left: 0;   
            display: block;
            height: 50%;
            background-color: rgba(0, 0, 0, 0.5);
            transition:all 0.4s ease;
            color: #FFF;
            content: '';
            width: 100%;
        }
        .imgbox2:before{
            top: 0;
            transform: translateY(-100%);
        }
        .imgbox2:after{
            bottom:0;
            transform: translateY(100%);
        }
        .imgbox:hover:after{
            transform: translateY(0);
        }
        .imgbox2:hover:after{
            transform: translateY(0);
        }
        .imgbox2:hover:before{
            transform: translateY(0);
        }
    </style>
</head>
<body>
    <div class="imgbox" data-text="我是文字">
            <img src="./2.jpg" alt="">
    </div>
    <div class="imgbox2" data-text="我是文字">
            <img src="./2.jpg" alt="">
    </div>
</body>
</html>

在css上加div:hover之后的效果

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题