Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getBoundingClientRect().width未返回正确的宽度_Javascript_Flexbox - Fatal编程技术网

Javascript getBoundingClientRect().width未返回正确的宽度

Javascript getBoundingClientRect().width未返回正确的宽度,javascript,flexbox,Javascript,Flexbox,我试图获取元素的宽度,但该值始终小于实际宽度。这是我目前的代码: this.rightSectionElement = this.$('.sections').eq(1).get(0); this.rightSectionOffset = this.rightSectionElement.getBoundingClientRect().width; 所讨论的元素上的CSS如下所示: nav{ position: relative; width: 100%; height

我试图获取元素的宽度,但该值始终小于实际宽度。这是我目前的代码:

this.rightSectionElement = this.$('.sections').eq(1).get(0);
this.rightSectionOffset = this.rightSectionElement.getBoundingClientRect().width;
所讨论的元素上的CSS如下所示:

nav{
    position: relative;
    width: 100%;
    height: 100%;
    @include inline-flex();
    flex-wrap: nowrap;
    justify-content: space-between;
    z-index: 1;

    .sections{
              @include flex-value(0 1 auto);
              height: 100%;
              width: auto;
              display: block;
...

这种方法是否不能很好地处理flexbox排列的元素?

是否对元素应用了任何边距或边框?getBoundClientRect()在计算宽度时不考虑边距或边框

请看这里:


你知道发生了什么事吗?当我将getBoundingClientRect()与flexbox一起使用时,我的宽度损失了40px,高度损失了5px。
#container {
  display: flex;
  width: 300px;
  margin: 20px;
  border: 5px;
}
--------------
//Outputs 300
document.write(document.getElementById("container").getBoundingClientRect().width);