css实现-王者换肤的效果

最近发现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;
        }

  • 效果如下:

元素发生滚动,背景图片没有跟随滚动,那我们再看一下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;

      }
  • 效果如下:

元素滚动,背景图片跟随滚动,我们再看一下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;

      }
  • 效果如下:

背景图片不随元素滚动,当元素即将滚动出视口,其他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);
      }
  • 效果如下:
  • 页面看起来就像在动态切换背景一样,我称之为王者换肤。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章