【发布时间】:2021-12-01 04:28:53
【问题描述】:
尝试设置水平滚动(使用inline-flex)我遇到了以下问题:
无法在滚动 div 周围设置填充。试图理解为什么,似乎简单地添加子项的宽度会给出一个比计算出的父项宽度大的数字(远)。
https://jsfiddle.net/jniac/vv9zhcj3/
var divs = document.querySelectorAll('.content > div');
var widths = [], sum = 0;
for (var i = 0; i < divs.length; i++) {
var div = div = divs[i]
// offsetWidth & getComputedStyle give (almost) the same value
var w = div.offsetWidth;
//var w = parseFloat(getComputedStyle(div).width);
widths.push(w);
sum += w;
}
console.log('div width:', widths.join(', '));
console.log('sum (including padding):', sum + 200 + 'px');
console.log('computed style:', getComputedStyle(document.querySelector('.content')).width);
console.log('...can\'t get spacing to the right :\'(');
* {
margin:0;
box-sizing: border-box;
font-family: courier;
font-size: 14px;
}
.container {
width: 100vw;
height: 100vh;
}
.content {
display: inline-flex;
height: 100%;
padding: 100px;
}
.content > div {
height: 100%;
padding: 20px;
flex: none;
}
.A { background: red; }
.B { background: blue; }
.C { background: orange; }
.inside {
height: 100%;
padding: 40px;
border: rgba(255,255,255,.75) 2px dashed;
color: white;
font-size: 90px;
}
<div class="container">
<div class="content">
<div class="A"><div class="inside">AA</div></div>
<div class="B"><div class="inside">B</div></div>
<div class="C"><div class="inside">Long-C</div></div>
<div class="A"><div class="inside">Quite-Long-A</div></div>
<div class="C"><div class="inside">CC</div></div>
</div>
</div>
var w = parseFloat(getComputedStyle(div).width);
2062px > 1684.19px
结果相当惊人: 虽然计算的样式为宽度提供了一个小值,但实际上 div 滚动的偏移量比计算的值更大(滚动是否使用另一个宽度评估?),它仍然小于它应该的值:无法获得间距向右。
【问题讨论】:
-
对不起,我不明白这个问题。究竟是什么问题/问题?它们对我来说都是一样的。 i.imgur.com/6TFEFOp.png
标签: css flexbox getcomputedstyle