简体   繁体   English

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

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

I have this animation taking place in which i need to change the color of the element in the centre when the small circle touches it, i thought i could find it using position() but since i am using a :before for animating the div the position values are not useful, Is there a way i could achieve this or any other way to find out when the small circle intersects the large one. 我正在播放此动画,当小圆圈触摸它时,我需要更改中心元素的颜色,我以为可以使用position()找到它,但由于我使用:be来为div设置动画,所以位置值没有用,有没有一种方法可以实现这一目标,或者我可以通过其他任何方法找出小圆与大圆的交点。

https://jsfiddle.net/pwqfh7ys/5/ 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

You can't modify pseudo elements through JavaScript since they are not part of the DOM. 您无法通过JavaScript修改伪元素,因为它们不是DOM的一部分。 Your best bet is to define another class in your CSS with the styles you require and then add that to the element. 最好的选择是使用所需的样式在CSS中定义另一个类,然后将其添加到元素中。 Since that doesn't seem to be possible from your question, perhaps you need to look at using a real DOM element instead of a pseudo one. 由于从您的问题看来这似乎是不可能的,因此也许您需要研究使用真正的DOM元素而不是伪元素。

hope the below snippet might help, you can specify the content value you want via JS using the CSS attr() function. 希望以下代码片段对您有所帮助,您可以使用CSS attr()函数通过JS指定所需的内容值。

Javascript Java脚本

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

CSS: 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