如果事件队列不为空,则防止 onMouseOver 连续多次触发

发布于 2024-12-23 00:11:37 字数 1111 浏览 1 评论 0原文

我有一个关于“a”标签上的 onmouseover 的问题。我目前有一个“img”缩略图列表,其中 onMouseOver 附加到包装“img”的“a”标签。当鼠标悬停在图像上时,图像的“预览”将通过 JavaScript 函数加载到另一个 div 中,此 JavaScript 函数具有 jquery fadeto 效果,因此预览将淡入视图。

现在的问题是,如果用户快速将鼠标悬停在多个缩略图上,则 onMouseOver 事件会被注册多次,因此当用户最终停止快速将鼠标悬停在多个缩略图上时,最后一个缩略图将多次接收该事件,并且不仅淡入一次。下面是我的代码:

    function previewimage(loadimg){
      jloadimg='<img src="../images/'+loadimg+'.png" />';

      //stimulate a fade in effect by making the image has 0.7 opacity first
      $(#largeimage).fadeTo('fast', 0.7, function() {
      });
      $(#largeimage).fadeTo('slow', 1, function() {
      });

      document.getElementById('largeimage').innerHTML=jloadimg;
    }

对于缩略图列表,我有:

<a onMouseOver="previewimage('imagefile1');"><img src="path/to/thumbnail/image1.png" /></a>
<a onMouseOver="previewimage('imagefile2');"><img src="path/to/thumbnail/image2.png" /></a>
<a onMouseOver="previewimage('imagefile3');"><img src="path/to/thumbnail/image3.png" /></a>

如果前一个事件仍在队列中,如何取消连续运行的多个事件?谢谢。

I have a question regarding onmouseover on "a" tag. I currently have a list of "img" thumbnails which have onMouseOver attached to the "a" tag which wraps the "img". When the mouse is over the image, a "preview" of the image will load in another div via a javascript function, this javascript function has a jquery fadeto effect so the preview will fade into view.

The problem now is if a user hover over multiple thumbnails quickly, the onMouseOver event is registered multiple times, so when the user finally stop hovering over multiple thumbnails quickly, the last thumbnail will receive the event multiple times and fades in not only once. Below is my code:

    function previewimage(loadimg){
      jloadimg='<img src="../images/'+loadimg+'.png" />';

      //stimulate a fade in effect by making the image has 0.7 opacity first
      $(#largeimage).fadeTo('fast', 0.7, function() {
      });
      $(#largeimage).fadeTo('slow', 1, function() {
      });

      document.getElementById('largeimage').innerHTML=jloadimg;
    }

for the list of thumbnail images, I have:

<a onMouseOver="previewimage('imagefile1');"><img src="path/to/thumbnail/image1.png" /></a>
<a onMouseOver="previewimage('imagefile2');"><img src="path/to/thumbnail/image2.png" /></a>
<a onMouseOver="previewimage('imagefile3');"><img src="path/to/thumbnail/image3.png" /></a>

How do I cancel multiple event from running continuosly if the previous one is still in queue? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文