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)
罪魁祸首出现了
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)
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)
Bap*_*aud 11
将其添加到您的 css 文件中:
* {
outline: 1px solid #f00 !important;
opacity: 1 !important;
visibility: visible !important;
}
Run Code Online (Sandbox Code Playgroud)
它确保在使用红色边框进行调试时所有内容都可见。
我使用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)