2

How to check the scrollbar is scrolled down a half.

I mean when the user scrolls down over a half of screen, it should alert a message.

Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139

3 Answers3

9

since you are using jquery, you could use jQuery.scroll():

$(window).scroll(function() {
    if ($(window).scrollTop()  > $(window).height() / 2) {        
        alert('At Half the screen');
    }
});
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Doesn't work. After scrolling to the end scrollTop value is 693 and `$(window).height()/2` value is 745 – TomSawyer Aug 21 '19 at 07:06
0

You can get the half of the visible screen by

$(window).height() / 2

Half of the entire size of your page would be

$(document).height() / 2

Then compare the result against the scroll position:

$(window).scrollTop();
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0
$(document).ready(function(){
    var mHeight = $(window).height();
    $(window).scroll(function(){
        var sPosition = $('body').scrollTop();
        if(sPosition > (mHeight/2)) alert('hola :)');
    });
});
David
  • 15,894
  • 22
  • 55
  • 66
HADI
  • 2,829
  • 1
  • 26
  • 26