Element.scrollWidth只读属性是元素内容宽度的度量,包括由于中的overflow.The帖子而在屏幕上不可见的内容,因此详细介绍了scrollWidth、offsetWidth、clientWidth
an introduction on scrollWidth,offsetWidth,clientWidth in detail

边框,填充,边距都设置为零,溢出被隐藏在示例中,在imgbox1中,图像之间有一个空白,在imgbox2中没有空白。在firefox中单击Run code snippet。
var imgbox1 = document.getElementById("imgbox1");
imgbox1.innerHTML += imgbox1.innerHTML;
var span1 = document.getElementById("span1");
console.log("span1.scrollWidth " + span1.scrollWidth);
var imgbox2 = document.getElementById("imgbox2");
imgbox2.innerHTML += imgbox2.innerHTML;
var span2 = document.getElementById("span2");
console.log("span2.scrollWidth " + span2.scrollWidth);* {
padding: 0px;
margin: 0px;
border: 0px;
}
div {
border: 0px;
width: 933px;
height: 129px;
overflow: hidden;
white-space: nowrap;
}<div id="imgbox1">
<span id="span1">
<a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>
<br>
<div id="imgbox2">
<span id="span2"><a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>
我们得到了输出
span1.scrollWidth 39
span2.scrollWidth 10如何解释span1中的scrollWidth是39px,而span2中的scrollWidth是10px?为什么span1中的scrollWidth不是40px。
window.onload = function() {
var imgbox = document.getElementById("imgbox");
imgbox.innerHTML += imgbox.innerHTML;
var span = imgbox.getElementsByTagName("span");
console.log("span[0].offsetWidth " + span[0].offsetWidth);
console.log("imgbox.scrollWidth - imgbox.clientWidth " + (imgbox.scrollWidth - imgbox.clientWidth));
}* {
border: 0px;
margin: 0px;
padding: 0px;
}
div {
width: 933px;
height: 129px;
overflow: hidden;
white-space: nowrap;
}<div id="imgbox">
<span>
<a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>
我们得到了输出:
span[0].offsetWidth 943
imgbox.scrollWidth - imgbox.clientWidth 942请指出它们之间的1像素差异(943-942)在哪里。
发布于 2020-10-16 17:58:15
这是因为您正在尝试将span标签用作容器。因为它是默认的display: inline-block。它应用的计算与您在图像中显示的计算不同。该图像描述了display: block;元素的所有宽度。
每个控件都显示为自己的宽度规则。更极端的是,如果您要应用display: none;,则不能期望拥有与您的映像中相同的规则。
因此,设置display: block和规则将得到遵守:
span{
display: block;
}无论发生什么,你都不会有任何问题。
演示:
window.onload = function() {
var imgbox = document.getElementById("imgbox");
imgbox.innerHTML += imgbox.innerHTML;
var span = imgbox.getElementsByTagName("span");
console.log("span[0].offsetWidth " + span[0].offsetWidth, '\n', "imgbox.scrollWidth - imgbox.clientWidth " + (imgbox.scrollWidth - imgbox.clientWidth));
}* {
border: 0px;
margin: 0px;
padding: 0px;
}
div {
width: 933px;
height: 129px;
overflow: hidden;
white-space: nowrap;
}
span{
display: block;
}<div id="imgbox">
<span>
<a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a>
<a href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>
发布于 2020-10-13 01:18:27
之所以在"imgbox1“中得到图像之间的空格,是因为元素之间有空格(换行符和几个制表符就是一个空格,只是为了清楚起见)。
但在另一个"imgbox2“上没有空格,因为图像之间没有换行。
<div id="imgbox1">
<span id="span1">
<a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a><a
href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a><a
href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a><a
href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>
<br>
<div id="imgbox2">
<span id="span2"><a href=""><img src="https://i.stack.imgur.com/b7J9w.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/yh7YJ.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/5uIog.jpg" alt=""></a><a href=""><img src="https://i.stack.imgur.com/r5GCW.jpg" alt=""></a>
</span>
</div>https://stackoverflow.com/questions/64321430
复制相似问题