Find element that is causing the showing of horizontal scrollbar in Google Chrome

后端 未结 5 1495
天命终不由人
天命终不由人 2020-12-22 22:46

When I size my Chrome window to 328 x 455 pixels I still see a horizontal scrollbar. How can I find out which element is causing this? I\'ve been looking elements via the de

5条回答
  •  不知归路
    2020-12-22 23:22

    My quick solution with jQuery, stijn de ryck's createXPathFromElement and the console:

    /**
     * Show information about overflowing elements in the browser console.
     *
     * @author Nabil Kadimi
     */
    var overflowing = [];
    jQuery(':not(script)').filter(function() {
        return jQuery(this).width() > jQuery(window).width();
    }).each(function(){
        overflowing.push({
            'xpath'    : createXPathFromElement(jQuery(this).get(0)),
            'width'    : jQuery(this).width(),
            'overflow' : jQuery(this).width() - jQuery(window).width()
        });
    });
    console.table(overflowing);
    
    
    /**
      * Gets the Xpath of an HTML node
      *
      * @link https://stackoverflow.com/a/5178132/358906
      */
    function createXPathFromElement(e){for(var t=document.getElementsByTagName("*"),a=[];e&&1==e.nodeType;e=e.parentNode)if(e.hasAttribute("id")){for(var s=0,l=0;l1));l++);if(1==s)return a.unshift('id("'+e.getAttribute("id")+'")'),a.join("/");a.unshift(e.localName.toLowerCase()+'[@id="'+e.getAttribute("id")+'"]')}else if(e.hasAttribute("class"))a.unshift(e.localName.toLowerCase()+'[@class="'+e.getAttribute("class")+'"]');else{for(i=1,sib=e.previousSibling;sib;sib=sib.previousSibling)sib.localName==e.localName&&i++;a.unshift(e.localName.toLowerCase()+"["+i+"]")}return a.length?"/"+a.join("/"):null}
    
    //**/
    

提交回复
热议问题