左右兩張圖片錯(cuò)位動(dòng)畫(huà)特效(仿大疆)

dji.gif

dji1.gif

           <!DOCTYPE html>
           <html lang="en">
                 <head>
                 <meta charset="UTF-8">
                 <title>Title</title>
                 <style>
                   *{
                      padding: 0;
                      margin: 0;
                     }
                   .banner{
                     width: 100%;
                     height: 2000px;
                     background-color: #0666;
                    }
                     .content{
                      overflow: hidden;
                      height: 100vh;
                      position: relative;
                      background-color: #eee;
                     }
                    .content .left{
                     width: 50%;
                     height: 100%;
                     border-right: 1px solid #000;
                     position: absolute;
                     left: 0;
                     overflow: hidden;
                    }
                   .content .right{
                    width: 50%;
                    height: 100%;
                    border-left: 1px solid #000;
                    position: absolute;
                    right: 0;
                    overflow: hidden;
                    }
                   .content .left img{
                    width: 100%;
                    height: 230px;
                    position: absolute;
                    right: -50%;
                    top:30%;
                    transition: right 0.5s;
                   }
                  .content .right img{
                   width: 100%;
                   height: 230px;
                   position: absolute;
                   left: -50%;
                   top:30%;
                   }
                  .rightcontent{
                   width: 50%;
                   height: 1000px;
                   background-color: green;
                   position: relative;
                   left: 50%;
                  }
                 .footer{
                  width: 100%;
                  height: 2000px;
                  background-color: red;
                  z-index: 100;
                  position: relative;
                  }
               </style>
              </head>
             <body>
               <div class="banner"></div>
               <div class="content">
                  <div class="left">
                  <img src="http://www3.djicdn.com/assets/images/products/supernova/s4/s4-pro- 327837106caf7f03d379e99ed09ebdb3.png?from=cdnMap">
                  </div>
                  <div class="right">
                  <img src="http://www4.djicdn.com/assets/images/products/supernova/s4/s4-zoom-b458b254ca10f1a813d80c4726d54335.png?from=cdnMap">
                  </div>
               </div>
                <div class="rightcontent">
                   右邊內(nèi)容
                </div>
                <div class="footer">

                </div>
  <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
  <script>
    var contentTop=$('.content').offset().top;                          //content區(qū)域距離窗口的高度
    var rightContentHeight=$('.rightcontent').offset().top;             //右邊內(nèi)容的高度距離窗口的高度
    var heightDifference=rightContentHeight-contentTop;                 //高度差,右邊內(nèi)容的高度距離窗口的高度減去content區(qū)域位于窗口的高度
    var pingjun=heightDifference/8;                                     //因?yàn)橐苿?dòng)8次,所以高度差÷8獲取每次移動(dòng)的平均高度觸發(fā)值
    var img=$('.left img').width()/2;                                   //獲取圖片一半的寬度
    var i=(img-140)/8;                                                  //要移動(dòng)140px,獲取平均移動(dòng)距離
    $(window).scroll(function() {
        var scroll=$(window).scrollTop();                               //滾動(dòng)的高度
        if(scroll>=contentTop){                                         //判斷content到達(dá)窗口頂部了
            $('.left').css({top:0,position:'fixed',background:'#eee'}); //把左邊設(shè)置成固定定位
            if (scroll<=rightContentHeight){                            //判斷滾動(dòng)的高度小于右邊內(nèi)容到窗口的高度
                if (contentTop<=scroll&&contentTop+pingjun>=scroll){
                    $('.left img').css('right',-(img-i)+'px');
                }else if (contentTop+pingjun<=scroll&&contentTop+pingjun*2>=scroll){
                    $('.left img').css('right',-(img-i*2)+'px');
                }else if (contentTop+pingjun*2<=scroll&&contentTop+pingjun*3>=scroll) {
                    $('.left img').css('right',-(img-i*3)+'px');
                }else if (contentTop+pingjun*3<=scroll&&contentTop+pingjun*4>=scroll) {
                    $('.left img').css('right',-(img-i*4)+'px');
                }else if (contentTop+pingjun*4<=scroll&&contentTop+pingjun*5>=scroll) {
                    $('.left img').css('right',-(img-i*5)+'px');
                }else if (contentTop+pingjun*5<=scroll&&contentTop+pingjun*6>=scroll) {
                    $('.left img').css('right',-(img-i*6)+'px');
                }else if (contentTop+pingjun*6<=scroll&&contentTop+pingjun*7>=scroll) {
                    $('.left img').css('right',-(img-i*7)+'px');
                }else if (contentTop+pingjun*7<=scroll&&contentTop+pingjun*8>=scroll) {
                    $('.left img').css('right',-(img-i*8)+'px');
                }
            }else {
                $('.left img').css('right',-(img-i*8)+'px');
            }
        }else{
            $('.left').removeAttr('style');
            $('.left img').removeAttr('style');
        }
    });
</script>
 </body>
 </html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容