• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    CSS完成视差滚动效果
    POST TIME:2021-10-16 23:55

    一、是什么

    视差滚动(Parallax Scrolling)是指多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验

    我们可以把网页解刨成:背景层、内容层、悬浮层

    当滚动鼠标滑轮的时候,各个图层以不同的速度移动,形成视觉差的效果

    二、实现方式

    使用css形式实现视觉差滚动效果的方式有:

    background-attachment

    作用是设置背景图像是否固定或者随着页面的其余部分滚动

    值分别有如下:

    完成滚动视觉差就需要将background-attachment属性设置为fixed,让背景相对于视口固定。及时一个元素有滚动机制,背景也不会随着元素的内容而滚动

    也就是说,背景一开始就已经被固定在初始的位置

    核心的css代码如下:

    section {
        height: 100vh;
    }
    
    .g-img {
        background-image: url(...);
        background-attachment: fixed;
        background-size: cover;
        background-position: center center;
    }

    整体例子如下:

    <style>
    div {
                height: 100vh;
                background: rgba(0, 0, 0, .7);
                color: #fff;
                line-height: 100vh;
                text-align: center;
                font-size: 20vh;
            }
    
            .a-img1 {
                background-image: url(https://images.pexels.com/photos/1097491/pexels-photo-1097491.jpeg);
                background-attachment: fixed;
                background-size: cover;
                background-position: center center;
            }
    
            .a-img2 {
                background-image: url(https://images.pexels.com/photos/2437299/pexels-photo-2437299.jpeg);
                background-attachment: fixed;
                background-size: cover;
                background-position: center center;
            }
    
            .a-img3 {
                background-image: url(https://images.pexels.com/photos/1005417/pexels-photo-1005417.jpeg);
                background-attachment: fixed;
                background-size: cover;
                background-position: center center;
            }
    </style>
     <div class="a-text">1</div>
        <div class="a-img1">2</div>
        <div class="a-text">3</div>
        <div class="a-img2">4</div>
        <div class="a-text">5</div>
        <div class="a-img3">6</div>
        <div class="a-text">7</div>

    transform:translate3D

    同样,让我们先来看一下两个概念transform和perspective:

    3D视角示意图如下所示:

    举个例子:

    <style>
        html {
            overflow: hidden;
            height: 100%
        }
    
        body {
            /* 视差元素的父级需要3D视角 */
            perspective: 1px;
            transform-style: preserve-3d; 
            height: 100%;
            overflow-y: scroll;
            overflow-x: hidden;
        }
        #app{
            width: 100vw;
            height:200vh;
            background:skyblue;
            padding-top:100px;
        }
        .one{
            width:500px;
            height:200px;
            background:#409eff;
            transform: translateZ(0px);
            margin-bottom: 50px;
        }
        .two{
            width:500px;
            height:200px;
            background:#67c23a;
            transform: translateZ(-1px);
            margin-bottom: 150px;
        }
        .three{
            width:500px;
            height:200px;
            background:#e6a23c;
            transform: translateZ(-2px);
            margin-bottom: 150px;
        }
    </style>
    <div id="app">
        <div class="one">one</div>
        <div class="two">two</div>
        <div class="three">three</div>
    </div>

    而这种方式实现视觉差动的原理如下:

    以上就是CSS完成视差滚动效果的详细内容,更多关于CSS 视差滚动效果的资料请关注脚本之家其它相关文章!

  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信