简体   繁体   English

为什么当我将鼠标光标悬停在 div 内时,我的图像会传送

[英]Why my image is teleporting when I'm hovering the image by my mouse cursor inside the div

So basically I've seen a lot of advance animation websites.所以基本上我看过很多高级动画网站。 And I tried to create a image wherein it follows the images as I move my mouse cursor along with div coordinates.我尝试创建一个图像,当我将鼠标光标与 div 坐标一起移动时,它会跟随图像。

Basically the code is like this in my codepen.基本上我的codepen中的代码是这样的。

https://codepen.io/myth-vince/pen/PoQJOXj https://codepen.io/myth-vince/pen/PoQJOXj

When you try to move the cursor into the image..it will just like teleport on the top left because I think that is the constant rect height ord width of the image.当您尝试将光标移动到图像中时..它就像左上角的传送一样,因为我认为这是图像的恒定矩形高度或宽度。

If anyone can help me with this..It will be a big thanks.如果有人可以帮我解决这个问题。非常感谢。 I've created some advance animation like this but never met this before that just teleport suddenly.我已经创建了一些像这样的高级动画,但在此之前从未遇到过这种情况,只是突然传送。

I believe it has something with this我相信它与此有关

var rect = e.target.getBoundingClientRect();
        console.log(rect)
        var x = e.clientX - rect.left; //x position within the element.
        var y = e.clientY - rect.top;  //y position within the element.

Or maybe the SCSS but I don't know where because everything seems fine..but I think the only really problem here is because when I try to make it also center the image in the mousecursor it will just adjust itself and will not align in center of mousecursor cause it is avoiding it to teleport.或者可能是 SCSS,但我不知道在哪里,因为一切看起来都很好..但我认为这里唯一真正的问题是因为当我尝试使它也使图像在鼠标光标中居中时,它只会自我调整并且不会对齐鼠标光标的中心,因为它正在避免它传送。

EDIT编辑

For more information.. the part of mouseover and mousemove the mousemove will only starts when the mouse is hovering the div so it the image will start to move out.有关更多信息.. mouseovermousemove的部分 mousemove 只会在鼠标悬停在 div 时开始,因此图像将开始移出。

Now for the second I need to get the e.clientX and e.clientY so that it could get the where the part of div is.现在第二个我需要获取e.clientXe.clientY以便它可以获取 div 的部分所在的位置。 And as rectX and rectY is the getting the height and width part of it由于rectXrectY是获取它的高度和宽度部分

Show DIV at mouse cursor on hover of span 在跨度悬停时在鼠标光标处显示 DIV

you can see it here the link.你可以在这里看到它的链接。

and where did I get the delaying the motion of images is here我从哪里得到延迟图像的运动在这里

https://stackoverflow.com/questions/9136261/how-to-make-a-setinterval-stop-after-some-time-or-after-a-number-of-actions#:~:text=To%20stop%20it%20after%20running,reached%20that%20number%20clear%20it . https://stackoverflow.com/questions/9136261/how-to-make-a-setinterval-stop-after-some-time-or-after-a-number-of-actions#:~:text=To%20stop %20it%20%20running 后,达到%20that%20number%20clear%20it

var rect = e.target.getBoundingClientRect();

There's your problem.有你的问题。

Like most other event types, the mousemove event bubbles up through the DOM.与大多数其他事件类型一样, mousemove事件通过 DOM冒泡

When you put the cursor over one of those images - then e.target is not the box any more, but that particular image.当您将光标放在其中一个图像上时 - e.target不再是box ,而是那个特定的图像。 So you are not working with the getBoundingClientRect data relating to your box now, but to the particular image.因此,您现在使用的不是与您的框相关的getBoundingClientRect数据,而是与特定图像相关的数据。

As soon as you make this只要你做这个

var rect = box.getBoundingClientRect();

instead, the problem is gone.相反,问题消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM