【问题标题】:How do you find coordinates of pseudo elements?你如何找到伪元素的坐标?
【发布时间】: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


    【解决方案1】:

    css 属性bottom 与边界矩形的bottom 属性不同。在第一次测试中,顶部和底部 css 值最终成为伪元素的高度,这只是巧合。

    边界矩形bottom是根据它的y位置和高度计算的:

    https://drafts.fxtf.org/geometry/#dom-domrectreadonly-domrect-bottom

    bottom 属性,在获取时,必须返回 max(y 坐标,y 坐标+高度尺寸)。

    css bottom 属性是一个位置值。使用 absolute 定位元素:

    https://www.w3.org/TR/CSS2/visuren.html#propdef-bottom

    类似于 'top',但指定框的底部边缘偏移多远 在盒子的包含块的底部上方。

    所以你不能简单地使用公式bottom-top 来获取伪元素的高度。您必须考虑最近定位的容器元素的高度,在您的情况下是块引用。

    所以对于块引用元素:它的高度是105px。引用的顶部是顶部上方的25px,其底部是底部下方的50px。使用这些值,您可以获得伪元素的高度:

    105 - -25 - -50 = 180
    

    至于坐标:x,y 属性似乎是特定于浏览器的,因为它们在 Firefox、IE 等中不存在。而且我不知道它们到底应该包含什么。边界框上的值也只是左上角的值。

    因此,如果您想计算左侧、顶部值,则必须使用其左侧、顶部值并再次考虑最近定位的父级位置

    var rect = e.getClientRects();
    var pseudoStyle = window.getComputedStyle(e, ':before');
    
    //find the elements location in the page
    //taking into account page scrolling
    var top = rect.top + win.pageYOffset - document.documentElement.clientTop;
    var left = rect.left + win.pageXOffset - document.documentElement.clientLeft;
    
    //modify those with the pseudo element's to get its location
    top += parseInt(pseudoStyle.top,10);
    left += parseInt(pseudoStyle.left,10);
    

    【讨论】:

    • 感谢您的回复。现在我看到了伪元素的高度和宽度是如何得出的。但是如何推导出伪元素的 x、y 位置值呢?上面例子中元素的坐标是多少?
    • @Dan,很抱歉忘记这是主要问题。编辑答案以包含该信息
    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 2020-07-02
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多