【问题标题】:Change mouse cursor on movement移动时更改鼠标光标
【发布时间】:2017-09-29 02:11:03
【问题描述】:
我不知道如何做到这一点。我制作的代码可以很好地更改我网站上的光标;但是,我希望它仅在用户移动鼠标时生效,然后在用户停止移动鼠标时恢复为默认光标。
到目前为止,这是我的代码:
<style type="text/css">body, a:hover {cursor: url(https://www.weebly.com/weebly/images/file_icons/image.png), progress !important;}</style>
【问题讨论】:
标签:
java
jquery
html
css
cursor
【解决方案1】:
您可以使用一些 JavaScript 来添加和删除 CSS 类。
为你的 CSS 添加一个类:
.change-cursor {
cursor: url(https://www.weebly.com/weebly/images/file_icons/image.png), progress !important;
}
然后在你的 JavaScript 中:
var timeout;
document.onmousemove = function() {
// Clear timeout, as mouse is still moving
clearTimeout(timeout);
// Add class, as mouse is still moving
document.querySelector('body ').classList.add('change-cursor')
// Schedule class to be removed very shortly in the future
timeout = setTimeout(function() {
document.querySelector('body').classList.remove('change-cursor')
}, 100)
}