CSS图片视觉差效果

36 篇文章 1 订阅
<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .wrapper {
            height: 100wh;
        }

        .parallax-img {
            /* ! */
            background-attachment: fixed;
            /* ! */
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            height: 100%;
            background-image: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg");
        }

        p {
            font-size: 20px;
            padding: 1.5rem 3rem;
            min-height: 1000px;
            /* 当你的文字内容不够,也能撑出足够的高度来看到效果,当然如果你文字足够多,就不需要了 */
        }

        .head {
            height: 500px;
            background: #ccc;
            font-size: 20px;
            padding: 1.5rem 3rem;
            background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_9jl3a3bpyja8h.png");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }

        .foot {
            height: 500px;
            background: #ccc;
            font-size: 20px;
            padding: 1.5rem 3rem;
            background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_jsss7i9zf2yf7.png");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="head">
            afgsdgsdg
        </div>
        <div class="parallax-img"></img>
            <p>sdfsdfdsf</p>
        </div>
        <div class="foot">
            adsdasdasd
        </div>
    </div>
</body>

</html>

参考

上面那个在手机上浏览没有效果,查了后写了新的👇

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .wrapper {
            height: 100wh;
        }

        

        .parallax-img:before {
          content: ' ';
          position: fixed;
          z-index: -1;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          background: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg") center 0 no-repeat;
          background-size: cover;
        }

        p {
            font-size: 20px;
            padding: 1.5rem 3rem;
            min-height: 200px;
            /* 当你的文字内容不够,也能撑出足够的高度来看到效果,当然如果你文字足够多,就不需要了 */
        }

        .head {
            height: 500px;
            background: #ccc;
            font-size: 20px;
            padding: 1.5rem 3rem;
            background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_9jl3a3bpyja8h.png");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }

        .foot {
            height: 500px;
            background: #ccc;
            font-size: 20px;
            padding: 1.5rem 3rem;
            background-image: url("https://s.seetao.com/Public/Uploads/thumbnail/2020-03-16/y_jsss7i9zf2yf7.png");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="head">
            afgsdgsdg
        </div>
        <div class="parallax-img"></img>
            <p>sdfsdfdsf</p>
        </div>
        <div class="foot">
            adsdasdasd
        </div>
    </div>
</body>

</html>

修改后主要的代码是加了伪类用fixed来当作固定背景图:

.parallax-img:before {
          content: ' ';
          position: fixed;
          z-index: -1;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          background: url("http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg") center 0 no-repeat;
          background-size: cover;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值