简体   繁体   English

Onmouseover在元素上悬停时多次运行?

[英]Onmouseover running multiple times while hovering on element?

Why does onmouseover run multiple times while I hover over one element? 当我将鼠标悬停在一个元素上时,为什么onmouseover会多次运行?

I'm trying to run a simple animation when the user hovers over an icon but it runs multiple times. 当用户将鼠标悬停在某个图标上但它会多次运行时,我正在尝试运行一个简单的动画。

I'm testing it here: 我在这里测试它:

http://lujanventas.com/test.php http://lujanventas.com/test.php

Anyway, any ideas on how to fix it? 无论如何,有关如何解决它的任何想法? maybe using a listener instead? 也许使用听众?

This is the javascript running onmouseover: 这是运行onmouseover的javascript:

function upIcon(n) {
    console.log("Mouseover icon: " + n);
    $('#icon' + n).animate({ backgroundPositionY : "-=15px" }, 200 );
    $('#icon' + n).animate({ backgroundPositionY : "+=15px" }, 200 );
    }

Try using mouseenter and mouseleave instead of mouseover and mouseout . 尝试使用mouseentermouseleave而不是mouseovermouseout The former fires only when the cursor enters the region of the element and the latter fires when the cursor moves between descendants as well. 前者仅在光标进入元素区域时触发,后者在光标在后代之间移动时触发。

I added this code to your page via my console and it worked as expected: 我通过我的控制台将此代码添加到您的页面,它按预期工作:

//bind an event handler to each nav element for the mouseenter event
$('.categoryButtons').children().bind('mouseenter', function () {

    //call the `upIcon` function with the index of the current element
    upIcon(($(this).index() + 1));
});

I also removed the inline onmouseover code for each nav item. 我还删除了每个导航项的内联onmouseover代码。

Update 更新

You can use your existing function a bit differently and not use the anonymous function for your event handler (notice the function chaining to avoid duplicate selections): 您可以稍微使用现有函数,而不是对事件处理程序使用匿名函数(注意函数链接以避免重复选择):

function upIcon(event) {
    var n = ($(this).index() + 1);
    $('#icon' + n).animate({ backgroundPositionY : "-=15px" }, 200 ).animate({ backgroundPositionY : "+=15px" }, 200 );
}

$('.categoryButtons').children().bind('mouseenter', upIcon);

When you reference an existing function as the event handler, it gets passed the event object like any other event handler and you can also use this to refer to the element on which the event was triggered. 当您将现有函数作为事件处理程序引用时,它会像任何其他事件处理程序一样传递event对象,您也可以使用this来引用触发事件的元素。

A couple of things: 有几件事:

  • To solve your problem I would use the mouseenter event instead - this means when the mouse enters the element the code is executed, and then you can use mouseleave to change the animation again when the mouse pointer leaves. 为了解决您的问题,我将使用mouseenter事件 - 这意味着当鼠标进入元素时执行代码,然后您可以使用mouseleave在鼠标指针离开时再次更改动画。

  • Rather than using messy onmouseover events, you would be better using jQuery for what it is meant for - unobtrusive codes. 而不是使用凌乱的onmouseover事件,你最好将jQuery用于它的意图 - 不引人注目的代码。 Bind events to the elements eg 将事件绑定到元素,例如

     $(document).ready(function() { $(".icon").mouseenter(function() { $(this).animate({ backgroundPositionY : "-=15px" }, 200 ); }); $(".icon").mouseleave(function() { $(this).animate({ backgroundPositionY : "+=15px" }, 200 ); }); }); 

Since you're using jQuery, you could use mouseenter instead of mouseover --it's designed to protect you from mouseout events when there are multiple items within your target. 由于您使用的是jQuery,因此您可以使用mouseenter而不是mouseover - 它旨在保护您在目标中有多个项目时免受mouseover移除事件的影响。

More: http://api.jquery.com/mouseenter/ 更多: http//api.jquery.com/mouseenter/

So, you'd want to remove the events attached to each item (which isn't great form in the first place, and instead do the following in a script block: 因此,您需要删除附加到每个项目的事件(首先不是很好的形式,而是在脚本块中执行以下操作:

$('.categoryButtons').find('a').mouseenter(function(){
    /* ... Do something, probably depending on $(this).attr('id') or $(this).attr('rel') ... */
});

Rather than using onmouseover, I'd recommend using JQuery's mouseenter to avoid the multiple firing of the handler due to event bubbling: 我建议使用JQuery的mouseenter来避免因事件冒泡而多次触发处理程序,而不是使用onmouseover:

http://api.jquery.com/mouseenter/ http://api.jquery.com/mouseenter/

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

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