使用 JavaScript 更改鼠标指针

发布于 2024-10-09 14:47:10 字数 198 浏览 5 评论 0原文

我想使用 JavaScript 脚本来更改网站上的鼠标指针。 最好用 CSS 来完成,但我的要求是一个可以分发给很多人以嵌入到他们网站的头部部分的脚本。 通过CSS,可以通过

html
{
    cursor: *cursorurl*
}

How to do the same in JavaScript?来操作它。

I wanted to use a script to change the mouse pointer on my website using JavaScript.
It's better done by CSS but my requirement is of a script that can be distributed to many people to embed in the head section of their websites.
Through CSS, this can be manipulated by

html
{
    cursor: *cursorurl*
}

How to do the same in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场信仰旅途 2024-10-16 14:47:11
document.body.style.cursor = 'cursorurl';
document.body.style.cursor = 'cursorurl';
咋地 2024-10-16 14:47:11

Body 可能有余量,所以更好的解决方案是:

document.documentElement.style.cursor = 'pointer';

Body may have margin, so better solution will be:

document.documentElement.style.cursor = 'pointer';
悍妇囚夫 2024-10-16 14:47:10

JavaScript 非常擅长操作 CSS:

 document.body.style.cursor = *cursor-url*;
 //OR
 var elementToChange = document.getElementsByTagName("body")[0];
 elementToChange.style.cursor = "url('cursor url with protocol'), auto";

或使用 jQuery:

$("html").css("cursor: url('cursor url with protocol'), auto");

Firefox 将无法工作,除非您在图像后指定默认光标!

其他光标关键字

另请记住,IE6 仅支持 .cur 和 .ani 光标

如果光标没有改变:如果您相对于光标位置移动光标下的元素(例如元素拖动),则必须强制重绘元素:

// in plain js
document.getElementById('parentOfElementToBeRedrawn').style.display = 'none';
document.getElementById('parentOfElementToBeRedrawn').style.display = 'block';
// in jquery
$('#parentOfElementToBeRedrawn').hide().show(0);

工作示例:

document.getElementsByTagName("body")[0].style.cursor = "url('http://wiki-devel.sugarlabs.org/images/e/e2/Arrow.cur'), auto";
div {
  height: 100px;
  width: 1000px;
  background-color: red;
}
<html xmlns="http://www.w3.org/1999/xhtml">

  <body>
    <div>
    hello with a fancy cursor!
    </div>
  </body>

</html>

JavaScript is pretty good at manipulating CSS:

 document.body.style.cursor = *cursor-url*;
 //OR
 var elementToChange = document.getElementsByTagName("body")[0];
 elementToChange.style.cursor = "url('cursor url with protocol'), auto";

or with jQuery:

$("html").css("cursor: url('cursor url with protocol'), auto");

Firefox will not work unless you specify a default cursor after the imaged one!

other cursor keywords

Also remember that IE6 only supports .cur and .ani cursors.

If cursor doesn't change: In case you are moving the element under the cursor relative to the cursor position (e.g. element dragging) you have to force a redraw on the element:

// in plain js
document.getElementById('parentOfElementToBeRedrawn').style.display = 'none';
document.getElementById('parentOfElementToBeRedrawn').style.display = 'block';
// in jquery
$('#parentOfElementToBeRedrawn').hide().show(0);

working sample:

document.getElementsByTagName("body")[0].style.cursor = "url('http://wiki-devel.sugarlabs.org/images/e/e2/Arrow.cur'), auto";
div {
  height: 100px;
  width: 1000px;
  background-color: red;
}
<html xmlns="http://www.w3.org/1999/xhtml">

  <body>
    <div>
    hello with a fancy cursor!
    </div>
  </body>

</html>

马蹄踏│碎落叶 2024-10-16 14:47:10

查看此页面:http://www.webcodingtech.com/javascript/change-cursor。 php.看起来您可以脱离样式访问光标。此页面显示整个页面都已完成此操作,但我确信子元素也可以正常工作。

document.body.style.cursor = 'wait';

Look at this page: http://www.webcodingtech.com/javascript/change-cursor.php. Looks like you can access cursor off of style. This page shows it being done with the entire page, but I'm sure a child element would work just as well.

document.body.style.cursor = 'wait';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文