簡體   English   中英

jQuery 鼠標懸停在文檔上無法按預期工作

[英]jQuery mouseover doesn't work as expected on document

我想讓我自己的光標只顯示在特定類別的特定圖像上。

我已經為此寫了幾行。

這是我的代碼:

function registerCursorHoverEffect() {
    if (!isTouch()) {
        const el = document.body;
        var cursorDiv = document.createElement("div");
        cursorDiv.setAttribute("id", "cursor");
        cursorDiv.setAttribute("class", "light-spot light-spot--cursor light-spot--center-center light-spot--color-red light-spot--filled light-spot--outside");
        el.before(cursorDiv);

        document.getElementById("cursor").innerHTML = '<svg class="cursor-main" xmlns="http://www.w3.org/2000/svg" width="70" height="70" viewport="0 0 100 100" style="stroke: white; fill:white;font-size:300px; z-index: 9999999;position: absolute; top: 40px; right: 40px; bottom: 0;"><path d="M59.71,31.29l-10-10a1,1,0,0,0-1.42,1.42L56.59,31H5a1,1,0,0,0,0,2H56.59l-8.3,8.29a1,1,0,0,0,0,1.42,1,1,0,0,0,1.42,0l10-10A1,1,0,0,0,59.71,31.29Z"/></svg>';
        var cursorDivAppend = document.createElement("div");
        cursorDivAppend.setAttribute("id", "cursor-append");
        cursorDivAppend.setAttribute("class", "cursor-append");
        el.before(cursorDivAppend);

        // Mousemove
        $(document).on('mousemove', function (event) {
            if($('.col-3:hover').length != 0) {
                const appendScale = 1
                var destinationX = event.pageX;
                var destinationY = event.pageY;
                var transformScale = `translate(calc(${destinationX}px - 50%), calc(${destinationY}px - 50%)) scale(${appendScale})`
                $('#cursor').css('transform', transformScale);
            }
        })
    }
}

以下屏幕截圖顯示了將鼠標懸停在具有特定類的圖像上時生成的內容:

在此處輸入圖片說明

這一切的關鍵部分在這里:

        // Mousemove
        $(document).on('mousemove', function (event) {
            if($('.col-3:hover').length != 0) {
                const appendScale = 1
                var destinationX = event.pageX;
                var destinationY = event.pageY;
                var transformScale = `translate(calc(${destinationX}px - 50%), calc(${destinationY}px - 50%)) scale(${appendScale})`
                $('#cursor').css('transform', transformScale);
            }
        })

event.pageX應該總是給我$(document)而不是事件本身的當前鼠標位置。 event.pageY完全一樣。 但是我總是得到事件的當前位置,我不知道為什么。

有人可以幫我嗎?

更新:

但是,光標元素的寬度阻礙了我,因為我得到了$ (documents)的坐標。 只有當我在光標之外時才會觸發該事件。 這是屏幕截圖並澄清問題: 在此處輸入圖片說明

我想讓我自己的光標只顯示在特定類別的特定圖像上。

你只能用css來做到這一點,

 .cursors { display: flex; justify-content: flex-start; align-items: stretch; height: 100vh; } .cursors > div { display: flex; justify-content: center; align-items: center; flex-grow: 1; box-sizing: border-box; padding: 10px 2px; text-align: center; } .cursors > div:nth-child(odd) { background: #eee; } .cursors > div:hover { opacity: 0.25; } .png { cursor: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/9632/heart.png"), auto; } .rotated { transform: rotate(45deg); /* Equal to rotateZ(45deg) */ background-color: pink; }
 <div class="cursors"> <div><img class="png rotated"src="https://uxwing.com/wp-content/themes/uxwing/images/patreon_btn.png"/> </div> <div class="gif">GIF</div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM