码迷,mamicode.com
首页 > Web开发 > 详细

原生js实现全屏滚动

时间:2017-03-16 19:11:02      阅读:252      评论:0      收藏:0      [点我收藏+]

标签: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实现全屏滚动

标签:html   z-index   通过   auto   动手   str   兼容   隐藏   hid   

原文地址:http://www.cnblogs.com/sheepsmall/p/6560919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!