javascript - jQuery: Follow cursor with delay - Stack Overflow

I want a div to follow the cursor-movement with a short delay like this: As you can see, the 'fol

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262106a4619257.html

相关推荐

  • javascript - jQuery: Follow cursor with delay - Stack Overflow

    I want a div to follow the cursor-movement with a short delay like this: As you can see, the 'fol

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信