首页 > Web开发 > 详细

js应用例子——滚动固定

时间:2017-12-24 15:06:28      阅读:237      评论:0      收藏:0      [点我收藏+]

 


<style type="text/css">

*{ margin:0px auto; padding:0px;}
#wai{ width:100%; height:3000px;}
#head{ width:100%; height:200px; background-color:#C30;}
#dhl{ width:100%; height:150px; background-color:#00C;}
#neirong{ width:100%; height:2000px;}
.a{ width:100px; height:200px; background-color:#0FF; margin-top:30px;}
</style>

<div id="wai">
<div id="head"></div>
<div id="dhl"></div>
<div id="neirong">
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
</div>
</div>

<script>

//这里用到的window.onscroll 是网页滚动监听, scrollY是网页下拉是滚动的长度,用alert输出是不带px的数值;


window.onscroll = function(){
if(window.scrollY>200){
  var dhl = document.getElementById("dhl");
  dhl.style.position = "fixed";
  dhl.style.top = "0px";
  } else{
  dhl.style.position="";
}
}
</script>

 

js应用例子——滚动固定

原文:http://www.cnblogs.com/forqiwen/p/8098065.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!