【发布时间】:2019-03-12 01:19:43
【问题描述】:
我一直在寻找这个问题的答案,但找不到有效的答案。我们以以下网站为例: https://www.atlassian.com/devops
在以下元素之前有一个伪元素:
var e = document.querySelector('li[class=language-selector]');
e.getClientRects();
top: 9797
bottom: 9818
left: 78
right: 162
x: 78
y: 9797
width: 85
height: 21
window.getComputedStyle 函数返回顶部、底部、左侧等的一些值:
window.getComputedStyle(e, ':before').top; //10.5
window.getComputedStyle(e, ':before').bottom; //-9.5
window.getComputedStyle(e, ':before').left; //-26
window.getComputedStyle(e, ':before').right; //90.6
window.getComputedStyle(e, ':before').x; //0
window.getComputedStyle(e, ':before').y; //0
window.getComputedStyle(e, ':before').width; //20
window.getComputedStyle(e, ':before').height; //20
起初它似乎是基本元素的相对值,但如果我检查同一页面中的其他元素,行为似乎不一致:
var e3=document.querySelectorAll('blockquote[class="cite large-quote"]')[0];
top: 2303
bottom: 2408
left: 78
right: 1038
x: 78
y: 2303
width: 960
height: 105
函数window.getComputedStyle返回如下:
window.getComputedStyle(e3, ':before').top; //-25
window.getComputedStyle(e3, ':before').bottom; //-50
window.getComputedStyle(e3, ':before').left; //0
window.getComputedStyle(e3, ':before').right; //889.25
window.getComputedStyle(e3, ':before').x; //0
window.getComputedStyle(e3, ':before').y; //0
window.getComputedStyle(e3, ':before').width; //70.75
window.getComputedStyle(e3, ':before').height; //180
例如,第一个伪元素的顶部和底部分别是 10.5 和 -9.5,而 (10.5) - (-9.5) 是伪元素 (20) 的高度。
第二个伪元素的顶部和底部是-25和-50,但伪元素的高度是180。它们的“位置”属性都具有“绝对”。所以行为不一致。
如果有人能阐明如何获取伪元素的位置或坐标,将不胜感激。
【问题讨论】:
标签: javascript html css css-selectors pseudo-element