简体   繁体   English

onmouseover()的JavaScript转换不会在onmouseout()事件中转换回我想要的方式

[英]JavaScript transition for onmouseover() not transitioning back how I want it to in the onmouseout() event

I have some photos on my website and I'm trying to put the JS equivalent of a CSS :hover effect on the captions with a .25s transition. 我的网站上有一些照片,我试图将.25s过渡效果的CSS等效于JS :hover效果添加到字幕上。

You can see in the code below I'm doing this with onmouseover() and onmouseout() events in the HTML and then writing a transition function in the JS. 您可以在下面的代码中看到我正在用HTML中的onmouseover()onmouseout()事件执行此操作,然后在JS中编写转换函数。 Pretty basic stuff. 很基本的东西。 Everything works almost like it should. 一切工作都差不多 The caption transitions in when I put my mouse on the photo, and transitions out when I mouse off. 当我将鼠标放在照片上时,标题会过渡,而当我将鼠标移开时,标题会过渡。

HOWEVER , if I just fly my mouse over the photo very quickly, sometimes the caption will come in and stay there until I mouse over it again. 但是 ,如果我只是将鼠标快速移到照片上,有时字幕会进入并停留在那里,直到我再次将鼠标移到照片上。 That doesn't happen every time. 并非每次都这样。 I'm no expert in machine mechanics and order of operations, but it almost seems to me as if, when I do that, the onmouseover() and onmouseout() events overlap and the "winner" is what executes. 我不是机械力学和操作顺序方面的专家,但是在我看来,当我这样做时,似乎onmouseover()onmouseout()事件重叠并且执行“赢家”。

I was wondering if there's any certain way to make sure that when I flick my mouse over very quickly, the caption either doesn't show up or goes away every time instead of transitioning in and staying there. 我想知道是否有某种方法可以确保当我快速将鼠标滑过时,字幕不会出现或每次消失而不是过渡并停留在那里。 Is it a simple matter of putting a delay on one of the transitions? 延迟其中一个转换是否很简单? Or is there some way I can prioritize one function if they both try to execute simultaneously? 还是有某种方法可以让一个函数优先执行(如果它们都尝试同时执行)?

The site isn't live yet so I can't link you to it, but here's the code for one of the photos and the JS for the events. 该网站尚未启用,所以我无法将您链接到该网站,但这是其中一张照片的代码和这些活动的JS。 I didn't put the CSS in there so I think the <div> might be bigger than the picture, but run the code and flick your mouse over the image from right to left so you get off the <div> and see if it happens for you! 我没有将CSS放在其中,所以我认为<div>可能比图片大,但是运行代码并从右向左在图像上滑动鼠标,以便您退出<div>并查看它是否发生在你身上!

  function divMouseOver(num){ var elementX = document.getElementById("photo" + num + "Subtitle"); var elementZ = document.getElementById("photo" + num + "Text"); var opac = 0; var id = setInterval(frame, 10); function frame(){ if(opac < 1.0){ opac = opac + 0.04; elementX.style.opacity = '0' + opac; elementZ.style.opacity = '0' + opac; } else{ clearInterval(id); } } } function divMouseOut(num){ var elementX = document.getElementById("photo" + num + "Subtitle"); var elementZ = document.getElementById("photo" + num + "Text"); var opac = 1.0; var id = setInterval(frame, 10); function frame(){ if(elementX.style.opacity > 0.0){ opac = opac - 0.04; elementX.style.opacity = '0' + opac; elementZ.style.opacity = '0' + opac; if(elementX.style.opacity == 0.04){ elementX.style.opacity = 0.0; elementZ.style.opacity = 0.0; } } else{ clearInterval(id); } } } 
  <div class="sectionPhotos"> <div class="container-fluid"> <div id="photoRow" class="row"> <div class="row1Photo" onmouseover="divMouseOver(1)" onmouseout="divMouseOut(1)"> <img style ="width:160px;height:100px;"src="https://cdn.wallpapersafari.com/26/0/VTGXtw.jpg"/> <div id="photo1Subtitle" class="row1PhotoSubtitle"> <h5>PHOTO SUBTITLE:</h5> </div> <div id="photo1Text" class="row1PhotoText"> <h4 class="pageLinkHere">PAGE LINK ON PHOTO</h4> </div> </div> </div> </div> </div> 

Yes, this happens to other people in their browsers. 是的,这发生在其他人的浏览器中。 Article about this behaviour and how mitigate what you experience: https://javascript.info/mousemove-mouseover-mouseout-mouseenter-mouseleave 有关此行为以及如何减轻您的体验的文章: https : //javascript.info/mousemove-mouseover-mouseout-mouseenter-mouseleave

TL;DR mouse movement is too fast and browser checks for mouse position only time to time, better go for onmouseleave event TL; DR鼠标移动太快,浏览器仅会不时检查鼠标位置,因此最好进行onmouseleave事件

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

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