background-attachment:fixed实现酷炫效果

本文介绍了CSS中background-attachment属性的一个酷炫应用,通过设置为fixed,实现背景图片在页面滚动时固定不动,创造出'王者换肤'的视觉效果。详细解释了scroll、local和fixed三个值的不同表现,并通过示例代码展示了如何创建这种动态背景切换的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近发现一个css的背景图片的属性,可以实现一个很酷璇的效果,根据效果,我给它命名为王者换肤,下面我们来看一下它的实现方式。

  • css3参考手册 的背景与边框目录里面,有一个介绍background-attachment属性的,它的值有3个,分别是fixed,scroll,local,他们分别代表的意义如下:

属性值的意义

  • 首先测试一下scroll属性的效果,页面结构如下:
                <div class="wrap item1">
                        <p>
                            Information1
                        </p>
                </div>
  • 样式如下:
.wrap {
        width: 100%;
        height: 800px;
        overflow: scroll;
        background-repeat: no-repeat;
        background-position: left center;
        background-size: 100% auto;
        background-color: black;
        background-image: url(images/th1.jpg);
        <!-- scroll -->
        background-attachment: scroll;
      }
p {
        width: 100%;
        height: 1000px;
        background: rgba(255,255,255,0.5);
        font-size: 30px;
        text-align: center;
        line-height: 100px;
        font-weight: 700;
        color:#2d2a2a;
        position: relative;
  }
.item1 p{
        margin-top: 50px;
        }

  • 效果如下:

scroll

元素发生滚动,背景图片没有跟随滚动,那我们再看一下local属性的效果,DOM结构不用变,只修改属性值如下:

.wrap {
        width: 100%;
        height: 800px;
        overflow: scroll;
        background-repeat: no-repeat;
        background-position: left center;
        background-size: 100% auto;
        background-color: black;
        background-image: url(images/th1.jpg);
        <!--local  -->
        background-attachment: local;

      }
  • 效果如下:

local

元素滚动,背景图片跟随滚动,我们再看一下fixed属性的效果,DOM结构不用变,只修改属性值如下:

.wrap {
        width: 100%;
        height: 800px;
        overflow: scroll;
        background-repeat: no-repeat;
        background-position: left center;
        background-size: 100% auto;
        background-color: black;
        background-image: url(images/th1.jpg);
        <!--fixed  -->
        background-attachment: fixed;

      }
  • 效果如下:

fixed

背景图片不随元素滚动,当元素即将滚动出视口,其他DOM结构出现,背景图片也未发生移动,看起来像是被其他元素遮挡了一样,其实是元素随着页面的滚动,部分内容已经不在视口的范围,所以不再显示,并不是被其他元素遮挡了。这个就是fixed的效果。一个元素是这样的效果,如果页面里面多放几个元素,属性都设置为fixed,会是什么效果呢?页面布局如下:

  <div class="wrap item1">
                    <p>
                        Information1
                    </p>
                </div>
                <div class="wrap item2">
                        <p>
                           Information2
                        </p>
                </div>
                <div class="wrap item3">
                        <p>
                           Information3
                        </p>
                </div>
                <div class="wrap item4">
                    <p>
                        Information4
                    </p>
                </div>
                <div class="wrap item5">
                    <p>
                        Information5
                    </p>
                </div>
  • 样式如下:
 .wrap{
        width: 100%;
        height: 800px;
        background-repeat: no-repeat;
        background-position: left center;
        background-size: 100% auto;
        background-color: black;
        overflow: scroll;
        /*fixed*/
        background-attachment: fixed;
        }
  p{
        width: 100%;
        height:100px;
        background: rgba(255,255,255,0.5);
        font-size: 30px;
        text-align: center;
        line-height: 100px;
        font-weight: 700;
        color:#2d2a2a;
        position: relative;
        }
.item1 p{
        margin-top: 50px;
        }
.item1{
        background-image: url(images/th1.jpg);
       }
.item2{
       background-image: url(images/th2.jpg);
      }
.item3{
       background-image: url(images/th3.jpg);
      }
.item4{
      background-image: url(images/th4.jpg);
      }
.item5{
      background-image: url(images/th5.jpg);
      }
  • 效果如下:

王者换肤

  • 页面看起来就像在动态切换背景一样,我称之为王者换肤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值