2

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;?

4

1 回答 1

7

来自 MDN 文档:

为了跨浏览器兼容性,请使用 window.pageYOffset 而不是 window.scrollY。此外,旧版本的 Internet Explorer (< 9) 不支持任一属性,必须通过检查其他非标准属性来解决。

你总是可以使用 jQuery 的实现scrollTop(),它应该适用于所有浏览器:

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

    if(y_scroll_pos > scroll_pos_test) {
       $('.extratext').slideDown('slow');
    }
});
于 2013-08-27T04:49:34.587 回答