javascript - Make div follow cursor on scroll additionally to mousemove - Stack Overflow

I'm currently trying to implement some custom cursor - a div that moves with the cursor. To do so,

I'm currently trying to implement some custom cursor - a div that moves with the cursor. To do so, I use the following code:

document.addEventListener('mousemove', this.handleMouseMove);

handleMouseMove = (event) => {
    const y = event.pageY;
    const x = event.pageX;

    const ref = document.querySelector('.follow-cursor');
    ref.style.left = x + 'px';
    ref.style.top = y + 'px';
};

It works fine, but there's one more problem: scrolling. So far, the div is not moving on scroll and hence not following the cursor on scroll. How can I change that? Reference: this website.

I'm currently trying to implement some custom cursor - a div that moves with the cursor. To do so, I use the following code:

document.addEventListener('mousemove', this.handleMouseMove);

handleMouseMove = (event) => {
    const y = event.pageY;
    const x = event.pageX;

    const ref = document.querySelector('.follow-cursor');
    ref.style.left = x + 'px';
    ref.style.top = y + 'px';
};

It works fine, but there's one more problem: scrolling. So far, the div is not moving on scroll and hence not following the cursor on scroll. How can I change that? Reference: this website.

Share Improve this question edited Mar 22, 2020 at 22:23 Tom asked Mar 22, 2020 at 21:59 TomTom 4,0636 gold badges26 silver badges63 bronze badges 4
  • can you please provide a working example? – Samet Conrad Commented Mar 22, 2020 at 22:15
  • @SametC the link is a working example, but having a look at the non-functional code OP has would be good – NickSlash Commented Mar 22, 2020 at 22:16
  • yes its an example for what he will do, but yes the non-functional code would be great. You are right. – Samet Conrad Commented Mar 22, 2020 at 22:18
  • @SametC That's all I have so far, I didn't figure a way yet to include scrolling. – Tom Commented Mar 22, 2020 at 22:23
Add a ment  | 

2 Answers 2

Reset to default 7

I don't think this is how the site you linked to does this, but it works.

handleMouseMove = (event) => {
    const y = event.pageY;
    const x = event.pageX;
    const ref = document.querySelector('.follow-cursor')
    const scrollLeft = (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
    const scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
    ref.style.left =  x - scrollLeft + 'px';
    ref.style.top = y - scrollTop + 'px';
};

If you apply style position: fixed to your .follow-cursor it should work.

example: https://jsfiddle/bf5wy9v3/

Yeah NickSlash's answer works fine. You can try both. Just in this case do not forget to add the id in the html

const cursor = document.getElementById("cursor")
const moveCursor = (event) => {
  const y = event.pageY
  const x = event.pageX
  const scrollLeft =
window.pageXOffset !== undefined
  ? window.pageXOffset
  : (document.documentElement || document.body.parentNode || document.body)
      .scrollLeft
onst scrollTop =
window.pageYOffset !== undefined
  ? window.pageYOffset
  : (document.documentElement || document.body.parentNode || document.body)
      .scrollTop
cursor.style.left = x - scrollLeft + "px"
cursor.style.top = y - scrollTop + "px"`}`
document.addEventListener("mousemove", moveCursor)

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信