在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ 這個網(wǎng)頁中背景輪換的效果是如何實現(xiàn)的?

這個網(wǎng)頁中背景輪換的效果是如何實現(xiàn)的?

https://templated.co/roadtrip
這個網(wǎng)頁中當(dāng)上下滾動時,背景會跟著變換,這是如何實現(xiàn)的。

回答
編輯回答
真難過

background-position

2017年1月14日 08:37
編輯回答
大濕胸

提供個思路,js得到滾動的值,然后給元素的background-position做改變,代碼在下面,可以試一下。圖片的路徑要注意
css

    <style>
        html, body {height:100%;overflow:auto;margin: 0;}
        .wrap{
            width: 900px;
            height: 450px;
            overflow-x: hidden;
            overflow-y: scroll;
            position: relative;
        }
        .one,.two,.three{
            width: 900px;
            height: 450px;
        }
        .one{
            background-color: rgba(0, 255, 0, 0.2)
        }
        .two{
            background-color: rgba(255, 0, 0, 0.2)
        }
        .three{
            background-color: rgba(0, 0, 255, 0.2)
        }
        .bgImgOne{
            width: 900px;
            height: 450px;
            position: absolute;
            z-index: -1;
            top: 0;
            background: url("./img/1.jpeg") no-repeat top left / 900px 450px;
        }
        .bgImgTwo{
            width: 900px;
            height: 450px;
            position: absolute;
            z-index: -1;
            top: 450px;
            background: url("./img/2.jpeg") no-repeat top left / 900px 450px;
        }
        .bgImgThree{
            width: 900px;
            height: 450px;
            position: absolute;
            z-index: -1;
            top: 900px;
            background: url("./img/2.jpg") no-repeat top left / 900px 450px;
        }
    </style>

html

        <div class="wrap">
            <div class="bgImgOne"></div>
            <div class="bgImgTwo"></div>
            <div class="bgImgThree"></div>            
            <div class="one">111111111</div>
            <div class="two">222222222</div>
            <div class="three">333333333</div>
        </div>

js

            var wrap = document.getElementsByClassName('wrap')[0];
            var bgImgOne = document.getElementsByClassName('bgImgOne')[0];
            var bgImgTwo = document.getElementsByClassName('bgImgTwo')[0];
            var bgImgThree = document.getElementsByClassName('bgImgThree')[0];
            var num = 0;
            
            wrap.addEventListener("scroll", function() {
                num = wrap.scrollTop;
                bgImgOne.style.backgroundPosition = "0px " + num + "px";
        
                bgImgTwo.style.backgroundPosition = "0px " + (num-450) +"px";

                if(num>=450){
                    bgImgThree.style.backgroundPosition = "0px " + (num-900) +"px";
                }
            })

圖片描述圖片描述圖片描述

2017年11月5日 02:50