简体   繁体   English

CSS 将 hover 事件从一个元素传递到另一个元素

[英]CSS Pass hover event from one element to the other

I have a custom CSS animated cursor, that uses minimum JS to follow the real cursor.我有一个自定义的 CSS 动画 cursor,它使用最小的 JS 来跟随真正的 cursor。

The fake cursor is just an HTML element like anything else.假的 cursor 只是一个 HTML 元素,就像其他任何东西一样。

Since the fake cursor (element) is under the real cursor, CSS thinks you're always hovering it.由于假的 cursor(元素)在真正的 cursor 之下,CSS 认为你总是在徘徊。

So if I hover/click a button, it will ignore both events.因此,如果我悬停/单击一个按钮,它将忽略这两个事件。

What I imagine it to do is pass through the fake cursor, and go to the button event.我想它要做的是通过假的 cursor 和 go 到按钮事件。

Here's a screenshot:这是一个屏幕截图: 页面截图

Here's a screenshot of the fake cursor going under the "PLAY" button:这是假 cursor 在“播放”按钮下的屏幕截图: 光标截图

As you can see here, the fake cursor goes under the button.正如您在此处看到的,假 cursor 位于按钮下方。

I know I can change it with z-index:我知道我可以用 z-index 改变它: 在此处输入图像描述

But now you can see the CSS does not detect the hover event on the button.但是现在您可以看到 CSS 没有检测到按钮上的 hover 事件。

How can I have the z-index -effect on the page, while at the same time, detect all pointer events?如何在页面上产生z-index效果,同时检测所有指针事件? (AKA pass through the fake cursor) (也就是通过假光标)

Here's the full code for the cursor:这是 cursor 的完整代码:

<style>
body,
html,
* {
    cursor: none !important
}

#follower #circle1,
#follower #circle2 {
    cursor: none !important;
    position: absolute;
    border-radius: 50%;
    height: 0;
    width: 0;
    margin-top: 0;
    margin-left: 0
}

*,
body,
html {
    cursor: none !important
}

#follower {
    position: absolute;
    top: 50%;
    left: 50%
}

#follower #circle1 {
    -webkit-animation: 2s infinite pulse;
    animation: 2s infinite pulse;
    background: #fff
}

#follower #circle2 {
    -webkit-animation: 4s infinite pulse;
    animation: 4s infinite pulse;
    background: rgba(0, 0, 0, .8)
}

@-moz-keyframes pulse {

    0%,
    100% {
        opacity: .2;
        height: 1em;
        width: 1em;
        margin-top: -.5em;
        margin-left: -.5em
    }

    50% {
        opacity: .9;
        height: 3em;
        width: 3em;
        margin-top: -1.5em;
        margin-left: -1.5em
    }
}

@-webkit-keyframes pulse {

    0%,
    100% {
        opacity: .2;
        height: 1em;
        width: 1em;
        margin-top: -.5em;
        margin-left: -.5em
    }

    50% {
        opacity: .9;
        height: 3em;
        width: 3em;
        margin-top: -1.5em;
        margin-left: -1.5em
    }
}

@-o-keyframes pulse {

    0%,
    100% {
        opacity: .2;
        height: 1em;
        width: 1em;
        margin-top: -.5em;
        margin-left: -.5em
    }

    50% {
        opacity: .9;
        height: 3em;
        width: 3em;
        margin-top: -1.5em;
        margin-left: -1.5em
    }
}

@keyframes pulse {

    0%,
    100% {
        opacity: .2;
        height: 1em;
        width: 1em;
        margin-top: -.5em;
        margin-left: -.5em
    }

    50% {
        opacity: .9;
        height: 3em;
        width: 3em;
        margin-top: -1.5em;
        margin-left: -1.5em
    }
}
</style>
<div id="follower">
    <div id="circle1"></div>
    <div id="circle2"></div>
</div>
<script>
    (function() {
        var t, n, e, o;
        t = document.getElementById("follower"), document.getElementById("printout"), n = function(t) {
            return t.clientX
        }, e = function(t) {
            return t.clientY
        }, o = function(o) {
            var u;
            return u = {
                x: n(o),
                y: e(o)
            }, t.style.top = u.y + "px", t.style.left = u.x + "px"
        }, window.onmousemove = function(t) {
            var n;
            return n = t, setTimeout((function() {
                return o(n)
            }), 1)
        }
    }).call(this);
</script>

If you would like to see it live, no worries: www.parkingmaster.ml如果您想现场观看,不用担心: www.parkingmaster.ml

Ok, the easiest solution would be to pass the pointer-events: none;好的,最简单的解决方案是传递pointer-events: none; to the css of the fake cursor which is just the html element like div so, it will do is remove the events like hover or any other cursor events from it. to the css of the fake cursor which is just the html element like div so, it will do is remove the events like hover or any other cursor events from it.

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

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