if ($(window).scrollTop() == $(document).height() - $(window).height()){
这句话在滚动时返回真,比方说,滚动条的“末端”。
好的,
我怎么知道它何时达到 80%(还剩 20%)???
我正在尝试这样的表达:
if ($(window).scrollTop()*2 == $(document).height() - $(window).height()){ // at the middle?
if ($(window).scrollTop() == ($(document).height() - $(window).height())+100){ /// 100px left?
但是他们都没有返回 true..
知道为什么吗?
提前致谢!
最佳答案
已编辑
看这里:http://jsfiddle.net/CoolEsh/aQcBZ/7/
HTML
<div style="height:400px;">1 div</div>
<div style="height:300px;">2 div</div>
<div style="height:400px;">3 div</div>
<div style="height:200px;">4 div</div>
<div style="height:600px;">5 div</div>
JS
var handlerObj = {
_pageAboveCenter: null,
init: function()
{
handlerObj._pageAboveCenter = handlerObj._isPageAboveCenter();
$( window ).scroll( handlerObj._handleScroll );
},
_handleScroll: function()
{
var curentPositionAboveCenter = handlerObj._isPageAboveCenter();
if ( curentPositionAboveCenter != handlerObj._pageAboveCenter )
{
handlerObj._centerIntersection();
handlerObj._pageAboveCenter = curentPositionAboveCenter;
}
},
_centerIntersection: function()
{
console.log( "Center intersected!" ); // this is for firebug console
},
_isPageAboveCenter: function()
{
if ( ( Math.floor( $( window ).height() / 2 ) + $(window).scrollTop() ) > ( Math.floor( $(document).height() / 2 ) ) )
{
return true;
}
else
{
return false;
}
}
}
$( document ).ready( handlerObj.init );
如果有人快速滚动,这个示例工作事件。我在页面加载时初始化变量 _pageAboveCenter
。然后在滚动事件上我检查当前位置(它是在中心上方还是下方),如果 curentPositionAboveCenter
和 handlerObj._pageAboveCenter
的值不同,那么我调用中心相交的方法。
关于javascript - 如何使用 Javascript 检测垂直滚动条的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401877/