案例:仿小米logo过渡渐变效果

效果图:

布局思路:

代码:

<!DOCTYPE html>
<html lang="en">
<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>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            position: relative;
            margin-left: 500px;
            margin-top: 100px;
        }
        .box .logo {
            display: block;
            width: 55px;
            height: 55px;
            background-color: #ff6700;
            /* 超出盒子范围的不显示 */
            overflow: hidden;
        }

        .logo::before {
            /* 小米logo定位到盒子里 */
            position: absolute;
            content: '';
            left: 0;
            top: 0;
            width: 55px;
            height: 55px;
            background: url(images/mi-logo.png) no-repeat center center;
            /* before的过渡 */
            transition: all .3s;
        }
        .logo::after {
            /* 房子logo */
            position: absolute;
            content: '';
            left: 0;
            top: 0;
            width: 55px;
            height: 55px;
            background: url(images/mi-home.png) no-repeat center center;
            /* 虽然定位到盒子里,但是用外边距给它挤到盒子外边 */
            margin-left: -55px;
            /* after的过渡 */
            transition: all .3s;
        }
        .logo:hover::before {
            /* 鼠标移动到a标签上,小米logo往右移动到盒子外面
                但是盒子里给了overflow:hidden 也就不显示了
            */
            margin-left: 55px;
        }
        .logo:hover::after {
            /* 将房子logo左外边距-55px重写为0,回到盒子里 */
            margin-left: 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <!-- a标签里包括伪元素的背景图片*2 -->
        <a href="javascript:;" class="logo"></a>
    </div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值