admin管理员组

文章数量:1294039

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 es up with 'undefined'. Does IE not like window.pageYOffset;?

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 es up with 'undefined'. Does IE not like window.pageYOffset;?

Share Improve this question asked Aug 27, 2013 at 4:38 MeltingDogMeltingDog 15.5k52 gold badges178 silver badges322 bronze badges 1
  • When you have html and body at height : 100%, sometime $('html, body') work.... Worth a try! – Karl-André Gagnon Commented Aug 27, 2013 at 4:39
Add a ment  | 

1 Answer 1

Reset to default 7

From the MDN docs:

For cross-browser patibility, 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');
    }
});

本文标签: