查找导致在Google Chrome中显示水平滚动条的元素

Flo*_*Flo 28 css overflow scrollbar

当我将Chrome窗口的尺寸设置为328 x 455像素时,我仍会看到水平滚动条.如何找出导致此问题的元素?我一直在通过开发者控制台查找元素,但找不到元素.

然后我尝试了我在这里找到的脚本,但没有记录任何内容.我在元素和其他一些人上尝试过body,section1但不知道还能做什么.

    $(function () {
        var f = $('body'); //document.getElementById("body");
        var contentHeight = f.scrollHeight;
        var declaredHeight = $(f).height();

        var contentWidth = f.scrollWidth;
        var declaredWidth = $(f).width();
        if (contentHeight > declaredHeight) {
            console.log("invalid height");
        }
        if (contentWidth > declaredWidth) {
            console.log("invalid width");
        }
    });
Run Code Online (Sandbox Code Playgroud)

Luc*_*uca 74

.slide-content .scroller {
  width: 1024px;
}
Run Code Online (Sandbox Code Playgroud)

"fastestest"方式:在检查员中添加:

* {
  border: 1px solid #f00 !important;
}
Run Code Online (Sandbox Code Playgroud)

罪魁祸首出现了

  • 注意这种方法,边框实际上可以改变滚动条出现的位置,因为它本身有宽度。 (5认同)
  • 不错的伎俩。不过,我用“轮廓”替换了“边框”。这样可以更清楚地找到罪魁祸首(轮廓不会使框变大,它只是在其“上方”绘制线条) (5认同)
  • 红色边框非常有用.我很快发现`input [type ="file"]`的字体很大,导致屏幕比我想要的要宽.谢谢. (4认同)
  • 这再次帮了我大忙,我发现一些条纹形式的“输入”字段是罪魁祸首,但是我不得不使用“边界:1px纯红色!重要”。我建议在您的答案中添加`!important`。 (3认同)
  • 真是个好主意!超级有帮助。 (2认同)

Mos*_*ava 13

Chris Coyier 撰写了一篇很好的文章,解释了你对这个问题所需的一切.

阅读本文后,我个人在我的控制台中使用此代码来找出导致垂直滚动的元素:

F12在浏览器中按,然后选择console并复制粘贴此代码并按Enter键

var all = document.getElementsByTagName("*"), i = 0, rect, docWidth = document.documentElement.offsetWidth;
for (; i < all.length; i++) {
    rect = all[i].getBoundingClientRect();
    if (rect.right > docWidth || rect.left < 0){
        console.log(all[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:
它可能是iframe制作页面中垂直滚动的元素.在这种情况下,您应该使用以下代码检查每个可疑的iframe:

var frame = document.getElementsByTagName("iframe"); 
frame = frame[0];
frame = (frame.contentWindow || frame.contentDocument);
var all = frame.document.getElementsByTagName("*"), i = 0, rect, docWidth = document.documentElement.offsetWidth;
for (; i < all.length; i++) {
    rect = all[i].getBoundingClientRect();
    if (rect.right > docWidth || rect.left < 0){
        console.log(all[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是一个很好的技巧,它有效 (2认同)

Tam*_*n K 12

通过将以下 js 代码复制粘贴到 URL 地址栏中来查找罪魁祸首。

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));
Run Code Online (Sandbox Code Playgroud)

  • 如果由于某种原因无法将以下内容粘贴到 URL 地址栏上,则可以在浏览器控制台中复制粘贴以下内容 ``` (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&gt;w||b.left&lt;0){t.currentNode.style .setProperty('outline','1px 红色虚线','重要');console.log(t.currentNode);}};}(document)); ```` (3认同)
  • 哇,这太棒了。为所有内容添加边框的接受答案对我没有帮助,当我这样做时滚动条消失了。 (2认同)
  • 不过,我喜欢这个解决方案,您应该解释一下它,它标记了在左侧被切断的元素,这些元素通常(RTL 除外)不会导致滚动条。 (2认同)

Bap*_*aud 11

将其添加到您的 css 文件中:

* {
  outline: 1px solid #f00 !important;
  opacity: 1 !important;
  visibility: visible !important;
}
Run Code Online (Sandbox Code Playgroud)

它确保在使用红色边框进行调试时所有内容都可见。


Nab*_*imi 5

溢出元素

我使用jQuery stijn de ryckcreateXPathFromElement和控制台的快速解决方案:

/**
 * 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 /sf/answers/362469271/
  */
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;l<t.length&&(t[l].hasAttribute("id")&&t[l].id==e.id&&s++,!(s>1));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}

//**/
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您使用某种滑动动画(轮播等),它们也会出现在此列表中。 (3认同)