简体   繁体   中英

Javascript how to find the source a scroll event

I have this funny bug that occurs and I am at a loss as to how to debug it. Every time a page loads on my site a scroll event fires. The page doesn't visibly move, and i certainly am not triggering the scrolling via the mouse or keyboard. I know that scroll event is firing because i put a line of code that reads

$(window).bind('scroll', function (e) {console.log(e)});

Sure enough on every page i get a little "jQuery.Event" message in my console's log. When i break point it my call stack ends at jQuery.even.dispatch.apply(eventHandle.elem, arguments), which doesn't give me a ton to work with.

Here's the question. How do I find out what is triggering this scroll event? Is there an attribute in the jquery event object that will tell me if the scroll was user triggered or triggered by a script? In this situation what would you do to figure this out?

Alright, it looks like jQuery hides all event binding data within a hidden attribute. This post describes ways that lets you find out at least what is being run -- it is still your responsibility to find out where the handlers are in which file.

In the case where scroll events are involved:

var scrollHandlers = jQuery._data(window, 'events')['scroll'];
for (var i = 0; i < scrollHandlers.length; i++) {
    console.error(scrollHandlers[i].handler);  // or console.debug, whatever proves they exist
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM