ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

background-attachment:fixed实现酷炫效果

2021-09-27 19:34:29  阅读:188  来源: 互联网

标签:100% 酷炫 height attachment background images fixed image


最近发现一个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);
      }
  • 效果如下:

王者换肤

  • 页面看起来就像在动态切换背景一样,我称之为王者换肤。

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有