Alex Rivera | Logout

Detect when Scroll reaches the BOTTOM of the page [ without jQuery ]

Asked 2012-04-08T02:15:00.810
18

I want to alert something when the scroll reaches the BOTTOM of the page, like this:

$(function(){
  $(document).scroll(function() {
    if($(document).scrollTop() == 0) alert("top");
  })
})

But without jQuery, and alert when reaches the Bottom.

Edit
Report

1 Answer

4
if(window.addEventListener){
    window.addEventListener('scroll',scroll)
}else if(window.attachEvent){
    window.attachEvent('onscroll',scroll);
}

function scroll(ev){
    var st = Math.max(document.documentElement.scrollTop,document.body.scrollTop);
    if(!st){
            console.log('top');
    }else if((st+document.documentElement.clientHeight)>=document.documentElement.scrollHeight ){
           console.log('bottom');
    }
}

example: http://jsfiddle.net/ampersand/AEnzJ/

tested with http://browserling.com in chrome 17/18, safari 5, ff 10/11.0, ie 7-9

answered 2012-04-08T02:29:45.517

Your Answer