Last active
January 18, 2020 09:49
-
-
Save koirikivi/7952081952002e95fc4702358fa296be to your computer and use it in GitHub Desktop.
Find elements that cause horizontal 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
/* | |
Paste this in JS console or create a bookmarklet to find which elements cause a horizontal scroll on the page. | |
Correctly detects even elements that are hidden by css tricks (e.g. clip property). | |
Requires jQuery but could probably be optimized to work without it with modest effort. | |
*/ | |
(function() { | |
var mainSelector = prompt( | |
'Selector under which to search', | |
'body' | |
); | |
var windowWidth = prompt('Window width', String(window.innerWidth)); | |
windowWidth = parseInt(windowWidth); | |
if(isNaN(windowWidth)) { | |
alert('Enter valid window width'); | |
return; | |
} | |
console.log('Finding visible elements under ' + mainSelector + ' where the position of right edge is over ' + windowWidth); | |
var num = 0; | |
$(mainSelector).find('*').each(function () { | |
var $this = $(this); | |
var outerWidth = $this.outerWidth(true); | |
var offset = $this.offset(); | |
var right = offset.left + outerWidth; | |
var visible = $this.is(':visible'); | |
if (visible && right > windowWidth) { | |
console.log($this[0], right); | |
num++; | |
} | |
}); | |
alert('Found ' + num + ' elements. See javascript console'); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment