繁体   English   中英

如何使用Javascript找到:before元素的位置?

[英]How to find the position of :before element using Javascript?

我正在播放此动画,当小圆圈触摸它时,我需要更改中心元素的颜色,我以为可以使用position()找到它,但由于我使用:be来为div设置动画,所以位置值没有用,有没有一种方法可以实现这一目标,或者我可以通过其他任何方法找出小圆与大圆的交点。

https://jsfiddle.net/pwqfh7ys/5/

html

<figure class="glob-wrapper">
    <div class="goos">
        <div class="goo"></div>
        <div class="goo"></div>
        <div class="goo"></div>
        <div class="goo"></div>
        <object type="image/svg+xml" data="./img/hello.svg" class="hello"></object>
    </div>
    <div class="blobs">
        <div class="blob"></div>
    </div>
</figure>

css

您无法通过JavaScript修改伪元素,因为它们不是DOM的一部分。 最好的选择是使用所需的样式在CSS中定义另一个类,然后将其添加到元素中。 由于从您的问题看来这似乎是不可能的,因此也许您需要研究使用真正的DOM元素而不是伪元素。

希望以下代码片段对您有所帮助,您可以使用CSS attr()函数通过JS指定所需的内容值。

Java脚本

$('.blob').on('click', function () {
    //do something with the callback
    $(this).attr('data-before','anything'); //anything is the 'content' value
});

CSS:

.blob:before {
    content: attr(data-before); //value that that refers to CSS 'content'
    //css here
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM