简体   繁体   English

鼠标事件与jquery有关

[英]Mouse events issue with jquery

I created a menu and I tried to use mouseover (mouseenter) and mouseout(mouseleave) but unfortunately if you move the mouse too fast some of the events are not fired and the hover images are not changed back to the original image. 我创建了一个菜单,我尝试使用mouseover(mouseenter)和mouseout(mouseleave)但不幸的是,如果你移动鼠标太快,一些事件不会被触发,并且悬停图像不会更改回原始图像。

I need to use mouseover and mouseout events instead of hover because I need to display the original image if the image is clicked. 我需要使用mouseover和mouseout事件而不是悬停,因为如果单击图像我需要显示原始图像。

Please check the demo at: Demo link 请查看演示: 演示链接

I don't think that using two different image elements per icon is a good idea. 我不认为每个图标使用两个不同的图像元素是个好主意。

Single image element 单个图像元素

You should use one element per icon and switch the class and image src when mouseover and mouseout are fired. 每个图标应使用一个元素,并在触发mouseover和mouseout时切换类和图像src。

Like so (minimal example): 像这样(最小的例子):

 $(".side_left").mouseover(function() {
    $(this).prop("src", "http://www.spiritualawakeningradio.com/yaha.jpg");
 }).mouseout(function(){
    $(this).prop("src", "http://img.creativemark.co.uk/uploads/images/461/13461/smallImg.png");
 });

Working fiddle: DEMO 工作小提琴: DEMO

Update with CSS sprites 使用CSS sprites更新

Here is a better example with CSS sprites. 这是CSS sprites的一个更好的例子。 No need for JavaScript and most of the markup, only some CSS: 不需要JavaScript和大多数标记,只需要一些CSS:

.side_left {
    width: 60px;
    height: 60px;
    background: url('http://i.imm.io/mvRL.png');
}
.side_left:hover {
    background-position: 60px;
}

Working fiddle: DEMO 工作小提琴: DEMO

您可以尝试使用悬停进行初始更改,然后单击更改类或图像源。

This is happening because you are using mouse events on items that become hidden, so when you move fast enough the item isn't there to trigger the mouseout event. 发生这种情况是因为您在隐藏的项目上使用鼠标事件,因此当您移动得足够快时,该项目不会触发mouseout事件。

I slightly modified your original code to make it work, however, you should consider scrapping the two image tags and swap between two image src attribute values or CSS using jQuery, or use pure CSS and the :hover selector. 我略微修改了原始代码以使其工作,但是,您应该考虑使用jQuery去掉两个图像标记并在两个图像src属性值或CSS之间交换,或者使用纯CSS和:hover选择器。

http://jsfiddle.net/MATMD/ http://jsfiddle.net/MATMD/

You could just use css and adding css-classes per JavaScript to do this. 您可以使用css并为每个JavaScript添加css类来执行此操作。

I updated your fiddle to show you, this would reduce the needed JavaScript-Code significantly. 我更新了你的小提示,告诉你,这将显着减少所需的JavaScript代码。 I kept your existing Markup, though I think this could be done with less markup too: 我保留了现有的Markup,但我认为这也可以通过更少的标记来完成:

http://jsfiddle.net/3YBe4/12/ http://jsfiddle.net/3YBe4/12/

If you move the mouse to quickly, you're not giving the .side_left_hover element enough time to start listening to the mouse. 如果快速移动鼠标,则不会给.side_left_hover元素足够的时间来开始监听鼠标。

So how do you fix it? 那么你如何解决它? I would put all your mouse listeners in the .side_left/.side_left_hover container div. 我会将所有鼠标监听器放在.side_left / .side_left_hover容器div中。 That will also make your code simpler and cleaner. 这也将使您的代码更简单,更清洁。

Here's the Fiddle: http://jsfiddle.net/rJK3R/1/ 这是小提琴: http//jsfiddle.net/rJK3R/1/

jsFiddle demo jsFiddle演示

  • I used the .on() method and list the mouse events needed. 我使用了.on()方法并列出了所需的鼠标事件。
  • You can than attach an event handler (e) and check for it's type inside the function. 您可以附加一个事件处理程序(e)并在函数内检查它的类型。

.

$('img.side_left').parent('div').on('mouseenter mouseleave click', function(e){
    if(e.type === 'mouseenter' || e.type==='mouseleave'){
        $(this).find('img:not(.active)').toggle();
    }else{ // IS CLICK
        $('img.active').toggle().removeClass('active');                
        $(this).find('img').addClass('active');
    }
});

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

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