Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Forked from cuth/debug-scroll.md
Created June 2, 2020 15:54
Show Gist options
  • Save akiatoji/47fff4c8b1fc746a9e1c700a76c73797 to your computer and use it in GitHub Desktop.
Save akiatoji/47fff4c8b1fc746a9e1c700a76c73797 to your computer and use it in GitHub Desktop.
Find the elements that are causing a horizontal scroll. Based on http://css-tricks.com/findingfixing-unintended-body-overflow/

Debug Horizontal Scroll

(function (d) {
    var w = d.documentElement.offsetWidth,
        t = d.createTreeWalker(d.body, NodeFilter.SHOW_ELEMENT),
        b;
    while (t.nextNode()) {
        b = t.currentNode.getBoundingClientRect();
        if (b.right > w || b.left < 0) {
            t.currentNode.style.setProperty('outline', '1px dotted red', 'important');
            console.log(t.currentNode);
        }
    };
}(document));

Bookmarklet

javascript:(function(d){var w=d.documentElement.offsetWidth,t=d.createTreeWalker(d.body,NodeFilter.SHOW_ELEMENT),b;while(t.nextNode()){b=t.currentNode.getBoundingClientRect();if(b.right>w||b.left<0){t.currentNode.style.setProperty('outline','1px dotted red','important');console.log(t.currentNode);}};}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment