Last active
October 29, 2024 08:23
-
-
Save brumm/74fd7eaafd50c8477519 to your computer and use it in GitHub Desktop.
Find out which element is scrolling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:!function() { var slice = Array.prototype.slice; function throttle(type, name, obj) { obj = obj || window; var running = false; var func = function() { if (running) { return; } running = true; requestAnimationFrame(function() { obj.dispatchEvent(new CustomEvent(name)); running = false; }); }; obj.addEventListener(type, func); } slice .call(document.querySelectorAll("*")) .filter( e => e.scrollWidth > e.offsetWidth || e.scrollHeight > e.offsetHeight ) .filter(e => { var style = window.getComputedStyle(e); return [style.overflow, style.overflowX, style.overflowY].some( e => e === "auto" || e === "scroll" ); }) .forEach(e => { var color = Math.floor(Math.random() * 16777215).toString(16); e.style.backgroundColor = "#" + color; throttle("scroll", "optimizedScroll", e); e.addEventListener("scroll", event => { console.log("%c[scroll]", "color: white; background-color:#" + color, event.target); }); }); }() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var slice = Array.prototype.slice; | |
function throttle(type, name, obj) { | |
obj = obj || window; | |
var running = false; | |
var func = function() { | |
if (running) { | |
return; | |
} | |
running = true; | |
requestAnimationFrame(function() { | |
obj.dispatchEvent(new CustomEvent(name)); | |
running = false; | |
}); | |
}; | |
obj.addEventListener(type, func); | |
} | |
slice | |
.call(document.querySelectorAll("*")) | |
.filter( | |
e => e.scrollWidth > e.offsetWidth || e.scrollHeight > e.offsetHeight | |
) | |
.filter(e => { | |
var style = window.getComputedStyle(e); | |
return [style.overflow, style.overflowX, style.overflowY].some( | |
e => e === "auto" || e === "scroll" | |
); | |
}) | |
.forEach(e => { | |
var color = Math.floor(Math.random() * 16777215).toString(16); | |
e.style.backgroundColor = "#" + color; | |
throttle("scroll", "optimizedScroll", e); | |
e.addEventListener("scroll", event => { | |
console.log("%c[scroll]", "color: white; background-color:#" + color, event.target); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet will apply a random background-color to any scrolling element on the page, and log the color-coded element to the console on scroll events