更改指针光标的CSS URL(使用Javascript) [英] Changing CSS URL for pointer cursor (using Javascript)

查看:152
本文介绍了更改指针光标的CSS URL(使用Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在此处提出了一个有关具有游标的问题定期使用javascript进行更改,以使其看起来更生动。我得到了一个很好的答案(谢谢你,湿婆!)。我现在一直在尝试获取两种不同的动画光标,一种用于自动光标,另一种用于指针光标。

I asked a question yesterday here about having a cursor that changes regularly using javascript, to make it look animated. I got a great answer (Thank you Shiva!). I've now been trying to get two different 'animated' cursors, one for the 'auto' cursor, and a different one for the 'pointer' cursor.

I尝试了很多不同的方法,但是却无法解决(我必须承认,我对此是全新的-尝试改进)。这是我尝试执行的方法之一:

I tried it lots of different ways, but just can't work it out (I must admit, I'm completely new to this - trying to improve). Here's one of the ways I tried to do it:

<!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。

If possible I'd like to do it without jQuery.

再次感谢您的帮助。

谢谢! :)

推荐答案

您可以尝试使用jQuery:

You can try using jQuery for this :

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的情况:

Or without 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 是否会覆盖同一处理程序(如果调用)

I had to name EH functions (and remove EH each call) because I wasn't sure that addEventListener overrides same handler if called again.

编辑

对于非jQuery方式,您需要添加 onload =您的< body> 标记上的 changeLinkCursorHoverBinding() 如下:
< body onload = changeLinkCursorHoverBinding()> (页面加载后的初始调用)。

for non jQuery way, you need to add onload="changeLinkCursorHoverBinding()" on your <body> tag this way : <body onload="changeLinkCursorHoverBinding()"> (the initial call after page's load).

这篇关于更改指针光标的CSS URL(使用Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆