Jquery .scroll() not working in IE with both $(window) and $(document). (issue with window.pageYOffset?)

13,077

From the MDN docs:

For cross-browser compatibility, use window.pageYOffset instead of window.scrollY. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties.

You could always use jQuery's implementation of scrollTop(), it should work for all browsers:

$(window).scroll(function() {
    var y_scroll_pos = $(this).scrollTop();
    var scroll_pos_test = 200;   

    if(y_scroll_pos > scroll_pos_test) {
       $('.extratext').slideDown('slow');
    }
});
Share:
13,077
MeltingDog
Author by

MeltingDog

Updated on June 05, 2022

Comments

  • MeltingDog
    MeltingDog almost 2 years

    I have this code:

    $(window).scroll(function() {
        var y_scroll_pos = window.pageYOffset;
        var scroll_pos_test = 200;   
    
        if(y_scroll_pos > scroll_pos_test) {
    
           $('.extratext').slideDown('slow');
    
        }
    });
    

    That works fine in FF, Chrome and IE 10 but not IE 9 or below. I have researched answers and they all say it should work with $(window) instead of the usual $(document), which is what Ive got.

    Does anyone know another way of amending this?

    EDIT:

    Added console.log(y_scroll_pos); and it comes up with 'undefined'. Does IE not like window.pageYOffset;?

    • Karl-André Gagnon
      Karl-André Gagnon over 10 years
      When you have html and body at height : 100%, sometime $('html, body') work.... Worth a try!