英文:
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
<div id="cursor"</div></div id="cursor_border"></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');`
答案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 + 'px';
cursor.style.top = mouseY + 'px';
cursorBorder.style.left = mouseX + 'px';
cursorBorder.style.top = mouseY + 'px';
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.clientX
和 event.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 => 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论