简体   繁体   English

滚动页面后自定义光标位置错误

[英]Custom cursor wrong position after scrolling the page

I'm trying to get one of those fancy circular cursors with a following outside ring, that changes when you hover an image for example.我正在尝试使用以下外环获得那些花哨的圆形光标之一,例如,当您悬停图像时会发生变化。

I found most of the code already built on codepen, but when i applied to a longer page, the cursors seems to be off by few too many pixels to be visible.我发现大部分代码已经建立在 codepen 上,但是当我应用到更长的页面时,光标似乎偏离了太多像素而无法看到。 It works fine until you start scrolling, as the scroll increases so does the offset and makes it pretty much useless if you scroll, you can't see the cursor.它工作正常,直到您开始滚动,随着滚动增加,偏移量也会增加,并且如果您滚动,它几乎没用,您看不到光标。

Here is a modified codepen: https://codepen.io/miguelpppires/pen/xxKLreP这是一个修改过的代码笔: https ://codepen.io/miguelpppires/pen/xxKLreP

I'm almost 100% sure this is the issue, but i have no idea how to fix it:我几乎 100% 确定这是问题所在,但我不知道如何解决它:

$(document).on('mousemove', function(e) {

$('.cursor, .follower').css({
"transform": "translate3d(" + (e.pageX) + "px, " + (e.pageY) + "px, 0px)"
});

Any and all help is appreciated, Thanks任何和所有的帮助表示赞赏,谢谢

You need to use clientX and clientY , becuase pageX and pageY take the offset from the top of the page and clientX and clientY take the offset from the top of the viewport.您需要使用clientXclientY ,因为pageXpageY从页面顶部获取偏移量,并且clientXclientY从视口顶部获取偏移量。

$('.cursor, .follower').css({
   "transform": "translate3d(" + (e.clientX) + "px, " + (e.clientY) + "px, 0px)"
});

我已经解决了使用 pageX 和 pageY 插入 clientX 和 clientY 的问题。

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

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