半俗半雅半疯癫93
2018-07-11 09:55
为什么滚动到顶部和底部功能,不管是老师的js方法,还是jq方法代码一模一样,但是我都不能实现。是浏览器版本原因吗?
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>jq滚动到头部或底部</title> <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> </head> <body> <div style="height: 3000px; width: 100%;"></div> <script type="text/javascript"> $(window).scroll(function(){ var ks_area = $(window).height(); var wholeHeight = $(document).scrollTop(); var scrolltop = $(window).scrollTop(); if(ks_area + scrolltop >= wholeHeight){ alert('底部'); } if(scrolltop == 0){ alert('顶部'); } }); </script> </body></html>
因为你 wholeHeight应该是文档的高度,而不是文滚动的高度。
var wholeHeight=$(document).height();
这样写就可以啦。
JS/jQuery宽高的理解和应用
34044 学习 · 59 问题