$(document).scroll is not firing in IE8 only

2019-02-05 14:30发布

问题:

I have a site running some javascript. In IE8 only, the $(document).scroll is not firing when you scroll with or without the mousewheel. Code snippet below:

$(document).scroll(function () {
        //do something on scroll
      });

Is there a specific reason this function won't fire in IE8? I have searched online with no success.

Thanks for all advice and tips in advance!!!!!

回答1:

Try using window:

  $(window).scroll(function () {
    //do something on scroll
  });


回答2:

For a lot of areas, IE ties the event to window rather than document as other browsers will. $(window).scroll(function(e) {}); is what you're after here. Should generally also work in most other browsers too, but if not, use a check on the navigator to find IE and use window or document based on that Boolean.