How to determine where vertical scrolling is using Javascript

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

This sentence returns true when it scrolls, say, the “end” of the scroll bar.

fine

how can I find out when it is 80% (20% left) ???

I am trying to use expressions:

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

But none of them ever comes back.

any idea why?

Many thanks!

+3
source share
1 answer

edited

Take a look here: 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 , , .

+1

All Articles