原生js實現全屏滾動

來源:互聯網
上載者:User

標籤:html   z-index   通過   auto   動手   str   相容   隱藏   hid   

原生js實現全屏滾動(firefox相容性問題還沒解決)

最近發現刷前端線上簡曆,發現許多人都使用了全屏滾動特效

所以今天特意來實現一下

css樣式:
* {
  margin: 0;
  padding: 0;
}
h1 {
  width: 200px;
  height: 200px;
  margin: 0 auto;
}
div{
  overflow: hidden;
  position: absolute;
}
.show {
  display: block;
}

/*第一頁*/
#one {
  background: red;
  z-index: 4;
}
/*第二頁*/
#two {
  background: blue;
  z-index: 3;
}

/*第三頁*/
#three {
  background: pink;
  z-index: 2;
}

/*第四頁*/
#four {
  background: black;
  z-index: 1;
}

HTML布局:
<!-- 第一頁-->
  <div id="one"class="pops hiddens show">
    <h1>楊曉煊</h1>
  </div>
<!-- 第二頁-->
  <div id="two" class="pops hiddens">
    <h1>楊曉煊</h1>
  </div>
<!-- 第三頁-->
  <div id="three" class="pops hiddens">
    <h1>楊曉煊</h1>
  </div>
<!-- 第四頁-->
  <div id="four" class="pops hiddens">
    <h1>楊曉煊</h1>
  </div>

 

js代碼:

<script>

 

//擷取螢幕有效寬和高.
windowHeight=document.docementElement.clientHeight;
windowWidth=document.documentElement.clientWidth;

//得到div元素的集合

divs=document.getElementsByTagName("div");

/*js實現方法:

(1):先迴圈div元素集合.然後為div元素繫結事件;

(2):然後為每個div設定一個line屬性,從0~div元素的個數.根據line屬性的值判斷當前顯示的div的索引;

(3):然後滑輪滾動時再判斷滾動向上還是向下轉動.

(4):然後根據滑動方向顯示出當前div的上一個或者下一個,同時將當前對象隱藏起來(通過添加類名來實現隱藏!!)

(5):同時設定上限和下限,到達頂部則上滑時直接return;到達底部下滑則直接return.

*/

for(var i=0;i<pops.length;i++){
  divs[i].style.width=windowWidth+"px";
  divs[i].style.height=windowHeight+"px";
  divs[i].setAttribute("line",i);
  divs[i].onmousewheel=function(){
  var line=parseInt(this.getAttribute("line"));

  if(event.wheelDelta<0){
    if(line==3){return;}
    this.classList.remove("show");
    divs[line+1].classList.add("show");
  }else{
    if(line==0){return;}
    this.classList.remove("show");
    divs[line-1].classList.add("show");
    };
  }
}

 

結語:;第一次發博.第一次自己動手做特效.很粗糙,以後再改進.

原生js實現全屏滾動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.