Jon*_*nas 2 javascript jquery mouseevent mousemove
我想要一个 div 跟随光标移动并有短暂的延迟,如下所示:http ://vanderlanth.io/
正如您所看到的,“跟随者”在动画中有短暂的延迟。
我重建了一些功能不太正常的功能:
$(document).ready(function () {
$("body").mousemove(function (e) {
handleMouseMove(e);
});
function handleMouseMove(event) {
var x = event.pageX;
var y = event.pageY;
$(".cursor-follower").animate({
left: (x - 16),
top: (y - 16)
}, 16);
$(".cursor").css({
left: (x - 4),
top: (y - 4)
});
}
});
Run Code Online (Sandbox Code Playgroud)
它相当滞后,动画也不是很流畅和轻松。您还有其他解决方案吗?
您可以通过 CSS 过渡来实现此效果。在 JavaScript 中,您只需更新 div 的位置。
$(document).on('mousemove', (event) => {
$('.follower').css({
left: event.clientX,
top: event.clientY,
});
});Run Code Online (Sandbox Code Playgroud)
.follower {
width: 20px;
height: 20px;
background-color: grey;
border-radius: 10px;
transition-duration: 200ms;
transition-timing-function: ease-out;
position: fixed;
transform: translate(-50%, -50%);
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="follower"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5301 次 |
| 最近记录: |