Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.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()是否返回了错误的值?_Javascript_Css - Fatal编程技术网

Javascript getBoundingClientRect()是否返回了错误的值?

Javascript getBoundingClientRect()是否返回了错误的值?,javascript,css,Javascript,Css,我想把div“.indicator”放在li元素开始的位置,所以 例如,如果我想相对于 第二,李,我有: indicator.style.left = li[1].getBoundingClientRect().left+"px"; 这是我的代码: var li=document.queryselectoral(.ulcontainer>li”); var indicator=document.querySelector(“.indicator”); indicator.style.left

我想把div“.indicator”放在li元素开始的位置,所以 例如,如果我想相对于 第二,李,我有:

indicator.style.left = li[1].getBoundingClientRect().left+"px";
这是我的代码:

var li=document.queryselectoral(.ulcontainer>li”);
var indicator=document.querySelector(“.indicator”);
indicator.style.left=li[1].getBoundingClientRect().left+“px”
html{
框大小:边框框;
}
*,*:之前,*:之后{
框大小:继承;
}
.Ulcainer{
空白:nowrap;
填充:0;
位置:相对位置;
}
李先生{
列表样式:无;
显示:内联块;
边框:1px实心;
填充:0 20px;
}
.指标{
背景色:红色;
显示:内联块;
位置:相对位置;
}
  • 选项1
  • 选项2
  • 选项3

指示器
。指示器
具有相对定位。因此,任何偏移都将相对于其默认位置,而不是相对于浏览器窗口。(相比之下,固定位置几乎总是相对于浏览器窗口,绝对位置是相对于最近定位的祖先-通常是浏览器窗口。)

由于
.indicator
是块级元素,其直接父元素是文档正文,因此其默认左侧位置等于文档正文的左侧边距

将主体边距设置为0,它将对齐:

var li=document.queryselectoral(.ulcontainer>li”);
var indicator=document.querySelector(“.indicator”);
indicator.style.left=li[1].getBoundingClientRect().left+“px”
正文{
保证金:0;
}
html{
框大小:边框框;
}
*,*:之前,*:之后{
框大小:继承;
}
.Ulcainer{
空白:nowrap;
填充:0;
位置:相对位置;
}
李先生{
列表样式:无;
显示:内联块;
边框:1px实心;
填充:0 20px;
}
.指标{
背景色:红色;
显示:内联块;
位置:相对位置;
}
  • 选项1
  • 选项2
  • 选项3

指示器
我认为它可能与您的CSS样式有关
显示:内联块和/或<代码>位置:相对。试着摆弄一下,告诉我这有什么用。