简体   繁体   English

React.JS onMouseOver animation 失败

[英]React.JS onMouseOver animation failing

I've been trying to get a two image slider working with a mouseOver event in react.js.我一直在尝试获取两个图像 slider 与 react.js 中的 mouseOver 事件一起使用。 I've seen a similar component linked here: codepen.io/bradtraversy/pen/NaKxdP我在这里看到了一个类似的组件:codepen.io/bradtraversy/pen/NaKxdP

The problem seems to be a huge delay with refreshing the image as well as onMouseOver events that occasionally fire with an offsetX coordinate that is way off from what it should be.问题似乎是刷新图像以及 onMouseOver 事件的巨大延迟,这些事件偶尔会触发 offsetX 坐标,该坐标与应有的坐标相差甚远。 Thanks for any help.谢谢你的帮助。 A demonstration of the problem I'm experiencing is linked in the codesandbox below.我遇到的问题的演示链接在下面的代码框中。

https://codesandbox.io/s/romantic-perlman-tzze4?file=/src/App.js https://codesandbox.io/s/romantic-perlman-tzze4?file=/src/App.js

I can't exactly contest as to why the offsetX is acting the way it does and am unable to verify what exactly you mean by refreshing the image being slow but using your codesandbox code I replaced:我无法完全质疑为什么 offsetX 会以它的方式行事,并且无法通过刷新图像缓慢但使用我替换的代码和框代码来验证您的确切含义:

if (Math.abs(xCoor - e.nativeEvent.offsetX) >= 2)
    setXCoor(e.nativeEvent.offsetX);
console.log(e.nativeEvent.offsetX);

With:和:

if (Math.abs(xCoor - e.nativeEvent.clientX) >= 2)
    setXCoor(e.nativeEvent.clientX -5);
console.log(e.nativeEvent.clientX);

And I am at least able to fix the flashing when the offsetX sets back to the odd numbers.当 offsetX 设置回奇数时,我至少能够修复闪烁。

Based on some other things I was reading while researching this, offsetX seems to provide inconsistent values randomly (as you have noticed).根据我在研究此内容时阅读的其他一些内容,offsetX 似乎随机提供了不一致的值(正如您所注意到的)。 This answer delves into it more and may be able to help you along further setting something up that is more consistent.这个答案更深入地研究了它,并且可能能够帮助您进一步设置更一致的东西。

I've attached an updated answer here:https://codesandbox.io/s/zealous-johnson-73myy?file=/src/App.js我在这里附上了一个更新的答案:https://codesandbox.io/s/zealous-johnson-73myy?file=/src/App.js

This uses a ref to the underlying DOM node to access the bounding rectangle and then use the clientX method as suggested by JFoxx64 to get offset within the element itself.这使用对底层 DOM 节点的引用来访问边界矩形,然后使用 JFox64 建议的 clientX 方法在元素本身内获取偏移量。 I think his method is also just as valid, we just discovered them at the same time.我认为他的方法也同样有效,我们只是同时发现了它们。 Thanks!谢谢!

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

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