Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
aamiri Avatar asked Sep 13 '12 18:09

aamiri


People also ask

How do you get the scroll position element?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

What triggers a scroll event?

The scroll event occurs when the user scrolls in the specified element. The scroll event works for all scrollable elements and the window object (browser window). The scroll() method triggers the scroll event, or attaches a function to run when a scroll event occurs.

How can detect scroll event in react JS?

To handle the onScroll event in React: Set the onScroll prop on an element or add an event listener on the window object. Provide an event handler function. Access relevant properties on the event or window objects.


1 Answers

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
}
like image 166
Brian Avatar answered Oct 20 '22 01:10

Brian