100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > jQuery $(document).height()与$(window).height() – 判断div随滚动条滚动到一定

jQuery $(document).height()与$(window).height() – 判断div随滚动条滚动到一定

时间:2023-04-05 23:36:52

相关推荐

jQuery $(document).height()与$(window).height() – 判断div随滚动条滚动到一定

注意当浏览器窗口大小改变时(如最大化或拉大窗口后)jQuery(window).height() 随之改变,但是jQuery(document).height()是不变的。

$(document).scrollTop() 获取垂直滚动的距离 即当前滚动的地方的窗口顶端到整个页面顶端的距离

$(document).scrollLeft() 这是获取水平滚动条的距离

要获取顶端 只需要获取到scrollTop()==0的时候 就是顶端了

要获取底端 只要获取scrollTop()>=$(document).height()-$(window).height() 就可以知道已经滚动到底端了

jQuery判断div随滚动条滚动到一定位置后停止:

<scripttype="text/javascript">varrollSet=$(#widget);varoffset=rollSet.offset();varfwidth=$("#footer").height();$(window).scroll(function(){varscrollTop=$(window).scrollTop();varscrollBtm=$(document).height()-$(window).scrollTop()-$("#widget").height();if(offset.top<scrollTop){if(scrollBtm>fwidth){rollSet.removeClass(absolute).addClass(fixed)}else{rollSet.removeClass(fixed).addClass(absolute)}}else{rollSet.removeClass(fixed)}})</script>

方法说明:

由页面总高度减去已滚动的高度再减去ID为widget的层的高度即等于该层底部距离底部的高度;

当距离底部的高度小于或等于特定位置距离底部的高度时,去掉样式fixed,然后给该层添加绝对定位!

CSS中要给父父层添加position:relative;

jQuery $(document).height()与$(window).height() – 判断div随滚动条滚动到一定位置后停止 – js/jQuery – 前端 html5 jquery 滑动

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。