javascript - 更改指针光标的 CSS URL(使用 Javascript)

标签 javascript css cursor

我昨天问了一个问题here关于使用 javascript 定期更改光标,使其看起来像动画。我得到了很好的答案(谢谢 Shiva!)。我现在一直在尝试获得两种不同的“动画”光标,一种用于“自动”光标,另一种用于“指针”光标。

我尝试了很多不同的方法,但就是无法解决(我必须承认,我对此完全陌生 - 正在尝试改进)。这是我尝试过的一种方法:

<!DOCTYPE html>
<html>
<head>

<script type = "text/javascript">   
var images = [
'assets/shared/cursors/drum1.cur',
'assets/shared/cursors/drum2.cur',
'assets/shared/cursors/drum3.cur',
'assets/shared/cursors/drum4.cur'
];

var x = 0;

function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.body.style.cursor = 'url("' + images[x] + '"), default';
} 

setInterval(displayNextImage, 250);
</script>

<script type = "text/javascript">   
var images = [
'assets/shared/cursors/point1.cur',
'assets/shared/cursors/point2.cur',
'assets/shared/cursors/point3.cur',
'assets/shared/cursors/point4.cur'
];

var x = 0;

function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.body.style.cursor:pointer = 'url("' + images[x] + '"), default';
} 

setInterval(displayNextImage, 250);
</script>

<body>

<div style="height: 1000vh; width: 1000vw;"></div>
</body>
</html>
</head>
</html>

如果可能的话,我希望不用 jQuery。

再次感谢您的帮助。

谢谢! :)

最佳答案

您可以为此尝试使用 jQuery:

var images = [
  'assets/shared/cursors/point1.cur',
  'assets/shared/cursors/point2.cur',
  'assets/shared/cursors/point3.cur',
  'assets/shared/cursors/point4.cur'
];

var x = 0;

function changeLinkCursorHoverBinding(){
  $("a").hover(function(){/*this is the mouseenter triggered function*/
    $(this).css("cursor",'url("' + images[x] + '"), default');
  }, function(){
    //handle mouseleave here if needed
  });

  x = (x === images.length - 1) ? 0 : x + 1;//put at the end to start at 0

  setTimeout(changeLinkCursorHoverBinding, 250);
}

$(document).ready(changeLinkCursorHoverBinding);//initial call of the function

编辑

或者没有 jQuery :

var images = [
  'assets/shared/cursors/point1.cur',
  'assets/shared/cursors/point2.cur',
  'assets/shared/cursors/point3.cur',
  'assets/shared/cursors/point4.cur'
];

var x = 0;

function changeLinkCursorHoverBinding(){
 var elems = document.querySelectorAll("a");
 elems.removeEventListener("mouseenter", onHover);
 elems.removeEventListener("mouseleave", offHover);

 elems.addEventListener("mouseenter", onHover);
 elems.addEventListener("mouseleave", offHover);

 x = (x === images.length - 1) ? 0 : x+1;

 setTimeout(changeLinkCursorHoverBinding, 250);
}

function onHover(){
  var elems = document.querySelectorAll("a");
  for(e in elems){
    e.style.cursor = "url('" + images[x] + "'), default";
  }//you can use the regular for loop here if you wanna
}

function offHover(){
  var elems = document.querySelectorAll("a");
  for(e in elems){
    /*handle mouseleave here*/
  }
}

我必须命名 EH 函数(并在每次调用时删除 EH),因为我不确定 addEventListener如果再次调用,将覆盖相同的处理程序。

编辑

对于非jQuery方式,你需要添加onload="changeLinkCursorHoverBinding()"在你的 <body>以这种方式标记: <body onload="changeLinkCursorHoverBinding()"> (页面加载后的初始调用)。

关于javascript - 更改指针光标的 CSS URL(使用 Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410117/

相关文章:

mysql - 如何将这个 MSSQL 游标函数转换为 MYSQL

mysql - 登录身份验证的存储过程不工作 MySQL

javascript - 无法使用Axios和Vue.Js从其他网站检索rss xml信息

html - 平行四边形形式的导航面板链接 - 需要纯文本

CSS定位绝对需要兄弟元素的相对位置

javascript - jQuery 使用 click 停止函数,该函数在 doc ready 上运行 - 然后在再次单击时启动函数

postgresql - PostgreSQL 中游标和获取如何工作

javascript - gtag.js 用户计时未出现在报告中

Javascript拆分正则表达式转义分隔符

javascript - jquery ajax 未捕获语法错误 : Unexpected token : while calling an api