Chrome DevTools: setting a breakpoint on document scroll change?
Solution 1
You can use a JavaScript event listener breakpoint. In the sources tab for Chrome developer tools, locate the "Event Listener Breakpoints" and then the "scroll" breakpoint is located under "Control".
Solution 2
The way I use it to override/proxy the scroll
functions and set a debugger
in the new function.
I run the following snippet in the console, with the hope one of the scroll functions will be called:
const proxyFn = fnName => {
const oldFn = window[fnName];
window[fnName] = (...args) => {
debugger;
oldFn(...args)
}
}
Object.keys(window).filter(c => c.includes("scroll")).forEach(c => proxyFn(c))
// or, if you want to "catch" the "scrollTo" function
proxyFn("scrollTo")
Related videos on Youtube
![aaaidan](https://i.stack.imgur.com/zJfV7.jpg?s=256&g=1)
aaaidan
I'm a Kiwi living in The Bay Area with my wife and son. Technical adventures have included React, Figma, ES6, SASS/LESS/CSS, Hypertalk, THREE.js, ActionScript 3, PHP, Java, MySQL, C#, Kotlin, Knockout.js, Web Audio API.
Updated on June 12, 2022Comments
-
aaaidan about 2 years
Sometimes, in a large clientside webapp, it's necessary to identify which line of code has scrolled the document with a
scrollTo()
call (or if this is even the cause of the scroll*).Is there any Chrome DevTools feature which will break JS execution on a window scroll? I'm imagining a feature similar to the DOM Breakpoints feature.
* Scrolling can also happen for other reasons, such as text input in an offscreen
<input>
. -
Mario about 6 yearsdidnt work for me :(. Added a breakpoint, but when manually scrolling (using the mousewheel) or when entering text in a textbox something is scrolling the window, but no breakpoint
-
aaaidan about 6 years@Mario Perhaps this will only break on javascript event listeners? I notice it works on this stackoverflow page: it breaks in what looks like jQuery's scroll handler.
-
Mario about 6 yearsexactly, I don't have events on the scroll, so that didnt cause to break @ certain point
-
TXN about 3 yearsThis work well, but seems to work only if
window.scrollTo(...)
is called. What to do to catch it for other scrollable elements (e.g. any element being defined as "overflow: auto")? -
Ionică Bizău about 3 years@TXN You'd have to guess what function can be called, if the scroll is triggered programmatically, and usually
scrollTo
is being used. You may want to use the element reference instead ofwindow
to proxy the scroll functions on a particular element. -
drdrej over 2 years404 HTTP. Link not work