繁体   English   中英

如何使用Javascript检测垂直滚动的位置

[英]how to detect where the vertical scroll is with Javascript

if  ($(window).scrollTop() == $(document).height() - $(window).height()){

当滚动条处于滚动条的“结束”时,该句子返回true。

好,

我怎么知道它何时达到80%(剩下20%)???

我正在尝试表达:

if  ($(window).scrollTop()*2 == $(document).height() - $(window).height()){  // at the middle?
if  ($(window).scrollTop() == ($(document).height() - $(window).height())+100){    /// 100px left?

但他们都没有回归真实......

任何想法为什么?

非常感谢提前!

EDITED

看这里: 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 然后在滚动事件中我检查当前位置(是在中心上方还是下方),如果curentPositionAboveCenterhandlerObj._pageAboveCenter值不同,那么我调用中心相交的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM