210

I am having trouble with jQuery functionality on my website. What it does, is that it uses the window.scroll() function to recognize when the windows changes its scroll position and at the change calls a few functions to load data from the server.

The problem is the .scroll() function is called as soon as there is even a little change in the scroll position and loads data at the bottom; however, what I wish to achieve is to load new data when the scroll/page position reaches at the bottom, like it happens for Facebook feed.

But I am not sure how to detect scroll position using jQuery?

function getData() {
  $.getJSON('Get/GetData?no=1', function (responseText) {
    //Load some data from the server
  })
};

$(window).scroll(function () {
    getData();
});
AshNaz87
  • 376
  • 3
  • 14
Maven
  • 14,587
  • 42
  • 113
  • 174
  • Does this answer your question? [How to detect scroll direction](https://stackoverflow.com/questions/7154967/how-to-detect-scroll-direction) – Aryan Beezadhur Jun 10 '20 at 20:10

9 Answers9

374

You can extract the scroll position using jQuery's .scrollTop() method

$(window).scroll(function (event) {
    var scroll = $(window).scrollTop();
    // Do something
});
Ivan
  • 34,531
  • 8
  • 55
  • 100
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • 10
    Attaching events to window scroll is a bad idea : see http://stackoverflow.com/questions/5036850/how-to-detect-page-scroll-to-a-certain-point-in-jquery – hendr1x Jan 29 '15 at 17:12
  • 14
    Listening to window scroll isn't bad in and of itself. It's when people try to _do_ things on each firing of that event that trouble occurs. If you only do something like set a variable's value to the current scroll position, or to true/false, and then make use of _those_ outside the event, you should generally be golden. – Jesse Dupuy May 12 '15 at 17:18
  • 7
    You should consider debouncing when working with this kind of events. See https://davidwalsh.name/javascript-debounce-function – Carlos Roso Mar 11 '16 at 17:26
  • As you already have `event` in function as argument you can get the same data from `event.originalEvent.pageY;` – Antoniossss Oct 11 '16 at 09:13
  • I was came here few times. Because I forgot to add jQuery before declare my javascript file. If this not work for you, please check you declare it first. – Dinith Rukshan Kumara Apr 14 '22 at 07:56
125

You are looking for the window.scrollTop() function.

$(window).scroll(function() {
    var height = $(window).scrollTop();

    if(height  > some_number) {
        // do something
    }
});
David Freitag
  • 2,252
  • 2
  • 16
  • 18
37

Check here DEMO http://jsfiddle.net/yeyene/Uhm2J/

function getData() {
    $.getJSON('Get/GetData?no=1', function (responseText) {
        //Load some data from the server
    })
};

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert("bottom!");
       // getData();
   }
});
yeyene
  • 7,297
  • 1
  • 21
  • 29
6
$(window).scroll( function() { 
 var scrolled_val = $(document).scrollTop().valueOf();
 alert(scrolled_val+ ' = scroll value');
});

This is another way of getting the value of scroll.

safeer008
  • 323
  • 4
  • 8
4

Now that works for me...

$(document).ready(function(){

    $(window).resize(function(e){
        console.log(e);                   
    });

    $(window).scroll(function (event) {
        var sc = $(window).scrollTop();
        console.log(sc);
    });

})

it works well... and then you can use JQuery/TweenMax to track elements and control them.

bummi
  • 27,123
  • 14
  • 62
  • 101
Ande Caleb
  • 1,163
  • 1
  • 14
  • 35
2

Store the value of the scroll as changes in HiddenField when around the PostBack retrieves the value and adds the scroll.

//jQuery

jQuery(document).ready(function () {

    $(window).scrollTop($("#<%=hidScroll.ClientID %>").val());

    $(window).scroll(function (event) {
        $("#<%=hidScroll.ClientID %>").val($(window).scrollTop());
    });
});

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function () {

    $(window).scrollTop($("#<%=hidScroll.ClientID %>").val());

    $(window).scroll(function (event) {
        $("#<%=hidScroll.ClientID %>").val($(window).scrollTop());
    });
});

//Page Asp.Net
<asp:HiddenField ID="hidScroll" runat="server" Value="0" />
0

You can add all pages with this code:

JS code:

 /* Top btn */
    $(window).scroll(function() {
        if ($(this).scrollTop()) {
            $('#toTop').fadeIn();
        } else {
            $('#toTop').fadeOut();
        }
    });
    var top_btn_html="<topbtn id='toTop' onclick='gotoTop()'>&#8593;</topbtn>";
    $('document').ready(function(){
        $("body").append(top_btn_html);
    });
    function gotoTop(){
        $("html, body").animate({scrollTop: 0}, 500);    
    }
    /* Top btn */

CSS CODE

/*Scrool top btn*/
#toTop{
    position: fixed;
    z-index: 10000;
    opacity: 0.5;
    right: 5px;
    bottom: 10px;
    background-color: #ccc;
    border: 1px solid black;
    width: 40px;
    height: 40px;
    border-radius: 20px;
    color: black;
    font-size: 22px;
    font-weight: bolder;
    text-align: center;
    vertical-align: middle;
}
Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
0
$('.div').scroll(function (event) {
 event.preventDefault()
 var scroll = $(this).scrollTop();
 if(scroll == 0){
   alert(123)
 }
});

This code for chat_boxes for loading previous messages

0

GET Scroll Position:

var scrolled_val = window.scrollY;

DETECT Scroll Position:

$(window).scroll
(
     function (event) 
     {
          var scrolled_val = window.scrollY;
          alert(scrolled_val);
     }
 );
Hassaan Raza
  • 91
  • 1
  • 11