其他分享
首页 > 其他分享> > background-attachment:fixed实现酷炫效果

background-attachment:fixed实现酷炫效果

作者:互联网

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

属性值的意义

                <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);
      }

王者换肤

标签:100%,酷炫,height,attachment,background,images,fixed,image
来源: https://blog.csdn.net/dai_yanxia/article/details/106251677