background-attachment: fixed;页面滚动得到不同的背景图(好看)

这段代码演示了如何使用CSS实现背景图片在页面滚动时产生固定(parallax)效果。通过设置`background-attachment:fixed`和`background-size:cover`,当背景图片大于元素高度时,可以创建出在页面滚动时元素背景图动态变化的视觉效果。
摘要由CSDN通过智能技术生成

 多说无意,上代码:

/* 写HTML ,当背景图片大于元素高度时,设置background-attachment:fixed; 当页面滚动,当前元素就可以得到不一样的背景图,很好看。 */

<style>
    body{
        height: 1500px;
    }
    .title {
        background: white;
        padding: 60px;
        margin: 0 auto;
        text-align: center;
    }

    .title h1 {
        font-size: 35px;
        letter-spacing: 8px;
    }

    .parallax-one {
        padding-top: 100px;
        padding-bottom: 100px;
        overflow: hidden;
        position: relative;
        width: 100%;
        background-image: url(https://b.zol-img.com.cn/desk/bizhi/image/10/960x600/1598319721647.jpg);
        
        /* 写HTML ,当背景图片大于元素高度时,设置background-attachment:fixed; 当页面滚动,当前元素就可以得到不一样的背景图,很好看。 */
        background-attachment: fixed;
        background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-repeat: no-repeat;
        background-position: top center;
    }
</style>
<body>
    <div>
        <!-- 第一个section姑且算作内容区域,其实第二个也可以作为内容区域的,看个人需求 -->
        <section>
            <div class="title">
                <h3>Let's do some</h3>
                <h1>PARALLAX</h1>
            </div>
        </section>
    
        <section>
            <div class="parallax-one">
                <h2>SOUTHERN CALIFORNIA</h2>
            </div>
        </section>
    </div>
  
    <div>
        <section>
            <div class="title">
                <h3>Let's do some</h3>
                <h1>PARALLAX</h1>
            </div>
        </section>
    
        <section>
            <div class="parallax-one">
                <h2>SOUTHERN CALIFORNIA</h2>
            </div>
        </section>
    </div>
</body>

效果图:(可惜我不会做动图,将就运行看吧)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值