admin管理员组文章数量:1417050
I want a div to follow the cursor-movement with a short delay like this: /
As you can see, the 'follower' has a short delay in the animation.
I've rebuild some function which is not working very well:
$(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)
});
}
});
It's quite lagging and the animation is not very smooth and ease. Do you have another solution?
I want a div to follow the cursor-movement with a short delay like this: http://vanderlanth.io/
As you can see, the 'follower' has a short delay in the animation.
I've rebuild some function which is not working very well:
$(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)
});
}
});
It's quite lagging and the animation is not very smooth and ease. Do you have another solution?
Share Improve this question asked Feb 12, 2018 at 21:49 JonasJonas 4775 silver badges23 bronze badges 2- Without looking at the site you posted, I'm guessing it's one of those continuous transitions with an easing function from the 2000s. – Derek 朕會功夫 Commented Feb 12, 2018 at 21:52
- It is not a delay that is being used. It is just an easing function. – Korgrue Commented Feb 12, 2018 at 21:52
1 Answer
Reset to default 5You can achieve this effect with CSS transitions. In JavaScript you only have to update the position of the div.
$(document).on('mousemove', (event) => {
$('.follower').css({
left: event.clientX,
top: event.clientY,
});
});
.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%);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="follower"></div>
本文标签: javascriptjQuery Follow cursor with delayStack Overflow
版权声明:本文标题:javascript - jQuery: Follow cursor with delay - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262105a2650402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论