我正在try 编写一个定制的光标,当悬停在某些元素上时,它会改变其样式.
如我的代码所示,我试图定义一个函数,该函数在将光标悬停在div上时更改光标的样式,并使用<div id="control" onmouseover="cursorFill()></div>"
对其进行调用.我本以为光标在悬停时会改变,一旦它不再位于div上方,就会变回原来的状态,但实际上它改变了,没有变回原来的状态.
如果可能的话,我更喜欢使用普通的JavaScript(没有jQuery或其他扩展).
const cursor = document.querySelector('#cursor');
document.addEventListener('mousemove', (e) => {
cursor.style.left = e.pageX + 'px';
cursor.style.top = e.pageY + 'px';
})
function cursorFill() {
document.getElementById("cursor").style.backgroundColor = "#fff";
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
cursor: none;
}
body {
background-color: #000;
align-items: center;
justify-content: center;
display: flex;
}
#cursor {
position: fixed;
width: 20px;
height: 20px;
border-radius: 50%;
border: 5px solid #fff;
background-color: transparent;
transform: translate(-50%, -50%);
pointer-events: none;
mix-blend-mode: difference;
}
#control {
background-color: #fff;
width: 10em;
height: 10em;
margin-top: 50vh;
}
<div id="cursor"></div>
<div id="control" onmouseover="cursorFill()"></div>