jQuery如何防止在使用光标悬停元素加载页面时阻止mouseenter触发 [英] jQuery how to prevent mouseenter triggering when page loads with cursor hovering element

查看:93
本文介绍了jQuery如何防止在使用光标悬停元素加载页面时阻止mouseenter触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个元素,该元素上绑定了一个mouseenter事件,该事件会更改子图像的尺寸.

I have an element on my page with a mouseenter event bound to it which changes the dimensions of a child image.

它的工作原理与我想要的一样,但是,如果您将光标悬停在div所在的位置来敲击页面,则它会在加载后立即触发-这是不希望的,我希望它在鼠标指针实际不起作用之前输入 div,而不仅仅是从头开始.

It works just as I want, however if you hit the page with your cursor hovering where the div will be, it triggers as soon as it loads - this is undesired, I want it to do nothing until a mouse cursor actualy enters the div rather than just being there to start with.

我尝试在jsfiddle上敲一个例子,但是页面加载太快,我无法将光标放在正确的位置:(

I've tried knocking up an example on jsfiddle but the page loads too quickly for me to get the cursor in the right place :(

一种可能是将bind方法调用置于超时状态,以便花一秒钟时间绑定事件,但是如果用户将光标放在我的div上,问题仍然会发生.

One possibility is putting the bind method calls in a timeout so that it takes a second to bind the event, but the problem will still happen if the user leaves their cursor over my div.

有什么想法吗?

使用jQuery 1.6.2

Using jQuery 1.6.2

推荐答案

Hm.这是唯一的主意.

Hm. Its only idea.

var mouse_already_there = false;
var event_set = false;
$(document).ready(function() {
    $(item).hover(function(){
        if(!event_set) { mouse_already_there = true; }
    }, function(){
        if(!event_set) { mouse_already_there = false; }
    });
    if(mouse_already_there) {
        //do nothing
    } else {
        event_set = true;
        //set event
    }
});

这篇关于jQuery如何防止在使用光标悬停元素加载页面时阻止mouseenter触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆