cursor_div wont follow the cursor when I scroll to the bottom

huangapple go评论57阅读模式
英文:

cursor_div wont follow the cursor when I scroll to the bottom

问题

当我在页面顶部时,curose_div 会完美地跟随鼠标光标。但当我开始向底部滚动时,cursor_div 的位置将不会正确跟随。

以下是我的代码:

<div id="cursor"></div>
<div id="cursor_border"></div>
var cursor = $('#cursor');
var cursor_border = $('#cursor_border');

$(document).mousemove(function(e){
    console.log(e.pageY + ' and '+ e.pageX );

    var x = e.pageX;
    var y = e.pageY;
    cursor.css("left", x+'px');
    cursor.css("top", y+'px');

    cursor_border.css("left", x+'px');
    cursor_border.css("top", y+'px');
});
英文:

When Im at the top of the page. The curose_div will follow the cursor perfectly. But when I start scrolling to the bottom the cursor_div position will not follow correctly.

Heres my code

&lt;div id=&quot;cursor&quot;&lt;/div&gt;&lt;/div id=&quot;cursor_border&quot;&gt;&lt;/div&gt;

<div id="cursor"></div>
<div id="cursor_border"></div>

`var cursor = $('#cursor');
var cursor_border = $('#cursor_border');

$(document).mousemove(function(e){
	console.log(e.pageY + &#39; and &#39;+ e.pageX ) ;

	
	var x = e.pageX;
	var y = e.pageY;
	cursor.css(&quot;left&quot;,x+&#39;px&#39;);
	cursor.css(&quot;top&quot;,y+&#39;px&#39;);
	
	cursor_border.css(&quot;left&quot;,x+&#39;px&#39;);
	cursor_border.css(&quot;top&quot;,y+&#39;px&#39;);`

答案1

得分: 0

以下是要翻译的内容:

这段代码可能会修复错误:

function updateCursor() {
  cursor.style.left = mouseX + 'px';
  cursor.style.top = mouseY + 'px';

  cursorBorder.style.left = mouseX + 'px';
  cursorBorder.style.top = mouseY + 'px';

  requestAnimationFrame(updateCursor);
}

updateCursor();

使用经典的JavaScript可以提高性能,并可能纠正由jQuery引起的错误!
希望这对你有帮助!

英文:

this code may correct the error :

  function updateCursor() {
  cursor.style.left = mouseX + &#39;px&#39;;
  cursor.style.top = mouseY + &#39;px&#39;;

  cursorBorder.style.left = mouseX + &#39;px&#39;;
  cursorBorder.style.top = mouseY + &#39;px&#39;;

  requestAnimationFrame(updateCursor);
}

updateCursor();

The use of classic Javascript can improve the performance, and may correct the error caused by jQuery !
I hope this helped you !

答案2

得分: 0

使用 event.clientXevent.clientY#cursor { position: fixed; } 一起:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
window.onmousemove = event => cursor.style.transform = `translate3d(${event.clientX}px, ${event.clientY}px, 0)`;

<!-- language: lang-css -->
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 200vh;
  background-image: linear-gradient(lightgreen, lightblue);
}

#cursor {
  position: fixed;
  width: 24px;
  height: 24px;
  display: none;
  margin: -12px 0 0 -12px;
  border: solid 2px red;
  border-radius: 50%;
  will-change: transform;
}

#cursor[style] {
  display: block;
}

<!-- language: lang-html -->
<div id="cursor"></div>

<!-- end snippet -->
英文:

Use event.clientX and event.clientY together with #cursor { position: fixed; }:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.onmousemove = event =&gt; cursor.style.transform = `translate3d(${event.clientX}px, ${event.clientY}px, 0)`;

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 200vh;
  background-image: linear-gradient(lightgreen, lightblue);
}

#cursor {
  position: fixed;
  width: 24px;
  height: 24px;
  display: none;
  margin: -12px 0 0 -12px;
  border: solid 2px red;
  border-radius: 50%;
  will-change: transform;
}

#cursor[style] {
  display: block;
}

<!-- language: lang-html -->

&lt;div id=&quot;cursor&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 15:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定