简体   繁体   English

更改鼠标光标移动

[英]Change mouse cursor on movement

I'm at bit of a loss on how to do this. 我对如何做到这一点感到茫然。 The code I made works fine for changing the cursor on my website; 我编写的代码可以很好地更改网站上的光标; however, I want it to only come into effect when the user moves the mouse, and then revert back to the default cursor when the user stops moving the mouse. 但是,我希望它仅在用户移动鼠标时才生效,然后在用户停止移动鼠标时恢复为默认光标。

Here is my code so far: 到目前为止,这是我的代码:

<style type="text/css">body, a:hover {cursor: url(https://www.weebly.com/weebly/images/file_icons/image.png), progress !important;}</style>

You can use some JavaScript to add and remove a CSS class. 您可以使用一些JavaScript添加和删除CSS类。

Add a class to your CSS: 在您的CSS中添加一个类:

.change-cursor {
  cursor: url(https://www.weebly.com/weebly/images/file_icons/image.png), progress !important;
}

And then in your JavaScript: 然后在您的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)

}

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

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