简体   繁体   English

如何正确获取使用flex-direction:column显示的块的实际宽度?

[英]How to get actual width of a block displayed with flex-direction:column properly?

For Example: 例如:

HTML HTML

 console.log($('.container').width()); // 600px as we defined in css. console.log($('.list').width()); // 600px console.log($('.box').eq('-1').position().left + $('.box').outerWidth()); 
 body{padding:0;margin:0;} .container { width: 600px; backround:#E1BEE7; } .list { display: flex; flex-direction: column; flex-wrap: wrap; height:120px; background:#FFCDD2; } .box { margin:10px; flex-shrink:0; flex-grow:0; flex-basis:auto; width: 100px; height: 100px; text-align:center; line-height:100px; background: #F44336; color:#FFF; font-size:2rem; } .result{ padding:1rem; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="container"> <div class="list"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> <div class="box">10</div> </div> </div> 

There is 10 boxes, and each box with 100px width, the actual width of element .list should be 1000px not 600px. 有10个框,每个框的宽度为100px,元素.list的实际宽度应为1000px,而不是600px。

I tried to use the following method to get actual width of list: The left position of last child element and its outerWidth 我尝试使用以下方法获取列表的实际宽度:最后一个子元素的左侧位置及其externalWidth

console.log($('.box').eq('-1').position().left + $('.box').outerWidth());

What I want to know: is there a better method to get actual width of this kind of flexbox element? 我想知道的是:有没有更好的方法来获取这种flexbox元素的实际宽度? some better built-in javascript or jquery method exists for this? 为此存在一些更好的内置javascript或jquery方法?

我需要的只是el.scrollWidth

As you've discovered, scrollWidth will provide you the dimension you're looking for. 如您scrollWidthscrollWidth将为您提供所需的尺寸。 The reason that outerWidth isn't giving you the total width of the internal elements is that your .list element is actually 600px wide. outerWidth无法为您提供内部元素总宽度的原因是您的.list元素实际上为600px宽。 The additional content is overflowing its parent, allowing it to display despite being outside of the .list . 附加内容溢出了其父级,即使位于.list之外也允许其显示。

You can test this by changing the overflow property to hidden on .list : 您可以通过将overflow属性更改为hidden.list

 body{ padding:0; margin:0; } .container { width: 600px; } .list { display: flex; flex-direction: column; flex-wrap: wrap; height: 120px; background: #FFCDD2; overflow: hidden; } .box { margin:10px; flex-shrink:0; flex-grow:0; flex-basis:auto; width: 100px; height: 100px; text-align:center; line-height:100px; background: #F44336; color:#FFF; font-size:2rem; } .result{ padding:1rem; } 
 <div class="container"> <div class="list"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> <div class="box">10</div> </div> </div> 

Note that when you run this, the .list element clips the remaining boxes. 请注意,运行此命令时, .list元素将.list其余的框。

Hopefully this helps you understand what is going on in addition to solving the problem for you. 希望这可以帮助您除了为您解决问题之外,了解正在发生的事情。 Cheers! 干杯!

Probably more code than you wanted, but you could just sum the width of the elements inside the div, using javascript with jQuery like this: 可能比您想要的代码更多,但是您可以使用jQuery这样的javascript,将div中元素的宽度求和:

var totalWidth = 0;
$('.box').each(function() {
  totalWidth += $(this).width();

});

$('#msg').html(
  "<p>totalWidth: " + totalWidth + "</p>"
);

with a live example: https://jsfiddle.net/yeeqkd2e/4/ 带有实时示例: https//jsfiddle.net/yeeqkd2e/4/

or without jQuery 或没有jQuery

var totalWidth = 0;

var cont = document.getElementsByClassName('box');
for (var i = 0, len = cont.length; i < len; i++) {
    totalWidth += cont[i].offsetWidth;
}

document.getElementById('msg').innerHTML = "<p>totalWidth: " + totalWidth + "</p>";

Native Javascript: https://jsfiddle.net/yeeqkd2e/6/ 本机Javascript: https : //jsfiddle.net/yeeqkd2e/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作 flex-direction: column; 适当地。 我的代码不起作用 - How to make flex-direction: column; properly. My code doesn't work 具有 flex-direction column-reverse 的 Flex 容器不会滚动到顶部 - Flex container with flex-direction column-reverse will not scroll to top 如何在 JavaScript 中应用 display: flex 和 flex-direction: row? - How to apply display: flex and flex-direction: row in JavaScript? 创建一个按钮以在样式的行和列之间切换 flex-direction - Create a button to toggle flex-direction between row and column for a style justify-content: flex-end; 和 flex-direction: 列; 没有一起工作 - justify-content: flex-end; and flex-direction: column; are not working together 单击按钮时更改 flex-direction - Change the flex-direction on button click CSS - 在 ul 中使用 flex-direction 附加 li 标记:column-reverse 无法正常工作 - CSS - Appending li tag in ul with flex-direction: column-reverse not working correctly 我怎样才能像 flex-direction 一样设置我的孩子的方向,但不使用 flex? - How can I set the direction of my <th> 's children like flex-direction does but without using flex? 你能在 CSS 和 JavaScript 中动态改变 flexbox 的 flex-direction 吗? - Can you change the flex-direction of flexbox dynamically in CSS and JavaScript? 如何获取视口中显示的元素的实际尺寸 - how to get the actual dimensions of the element displayed in viewport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM