简体   繁体   English

鼠标移出后触发jQuery Ajax mouseover事件

[英]jQuery Ajax mouseover event firing after mouseout

I'm creating a tooltip using jQuery that gets the tooltip contents via Ajax request and then stores it in a variable so Ajax isn't constantly fired off every time they mouseover. 我正在使用jQuery创建一个工具提示,该提示通过Ajax请求获取工具提示的内容,然后将其存储在变量中,这样Ajax不会在每次鼠标悬停时不断触发。 It all works perfectly except if you quickly swipe your mouse across a node and out it will load the tooltip and display it rather than hide it as defined in the mouseout. 除非您在节点上快速滑过鼠标并将其滑出,否则它将全部正常工作,除非它会加载工具提示并显示它,而不是按照mouseout中的定义隐藏它。

Order of events expected: 1. Mouse over 2. Ajax loads content and places it in hidden tooltip div 3. Tooltip is adjusted to the corner of the node and shown 4. Mouse out 5. Tooltip is hidden from view 预期的事件顺序:1.鼠标移到2. Ajax加载内容并将其放置在隐藏的工具提示div中。3.将工具提示调整到节点的角处并显示4.鼠标移出5.工具提示从视图中隐藏

Order of events seen in the scenario above: 1. Mouse over 2. Mouse out quickly 3. Tooltip is hidden from view 4. Ajax loads content and tooltip is shown in a fixed position until you mouse over again to get rid of it. 在以上场景中看到的事件顺序:1.鼠标移到2.鼠标移出3.工具提示从视图中隐藏4. Ajax加载内容,工具提示显示在固定位置,直到再次将鼠标移到摆脱它为止。

Here's the applicable code, there's obviously a bigger object that this is written in but I think you'll get the gist. 这是适用的代码,显然有一个更大的对象,但是我想您会明白的。

//Cancel code snippet
$('.tt').html();
$('.tt').hide();

//Tooltip init code snippet
$.ajax({
    type: "GET",
    url: "/tooltip/" + Tooltip.slug,
    dataType: "json",
    global: false,
    success: function(data) {
        $('.tt').html(data.description);
        Tooltip.init();
        $('.tt').attr("style","left:"+Tooltip.settings.left_offset+"px;top:"+Tooltip.settings.top_offset+"px");
        Tooltip.cache[slug] = data;
        $('.tt').show();
    }
});

The hoverIntent plug-in allows you to avoid accidental mouseovers. 使用hoverIntent插件可以避免意外的鼠标悬停。

Add code to see if the mouseout has fired. 添加代码以查看是否触发了mouseout。 If it did, than do not run the show() portion. 如果这样做,则不要运行show()部分。

Not sure why you need tool-tip from server. 不确定为什么需要服务器提供的工具提示。 You can use jQuery Tooltip to achieve same stuff Demo Page 您可以使用jQuery Tooltip来实现相同的内容演示页

Hope this help 希望这个帮助

我认为您应该检查成功句柄,看看是否应该在鼠标移出时显示工具提示。

That's happening because the ajax request is not finished yet when the mouse has already left the node. 之所以发生这种情况,是因为当鼠标已经离开节点时,ajax请求尚未完成。 It will just do its thing, wait for a response from the server, and then show the tooltip as defined in your success function. 它只会做它的事情,等待服务器的响应,然后显示成功功能中定义的工具提示。

One way to combat this would be to have an variable, say isHovering , which contains the node that is currently being hovered over. 解决此问题的一种方法是使用一个变量,即isHovering ,其中包含当前正在悬停的节点。 You could use the node's id for this: add it to the var immediately upon hover (before firing the ajax function), and set the var back to false upon mouseout. 您可以为此使用节点的id:将鼠标悬停时将其添加到var中(在触发ajax函数之前),并在鼠标移出时将var设置回false。

Then, in your ajax function, before rendering anything, check the var and only render if it contains the correct id. 然后,在您的ajax函数中,在呈现任何内容之前,检查var并仅在其包含正确的id时进行呈现。

Off the top of my head (not tested): 离开我的头顶(未经测试):

var isHovering = false;
$('.node').mouseover(function() {
      isHovering = $(this).attr('id');
}).mouseout(function() {
      isHovering = isHovering == $(this).attr('id') ? false : isHovering; //don't change if mouse on different node
});

and then in your ajax() function: 然后在你的ajax()函数中:

//Tooltip init code snippet
$.ajax({
    type: "GET",
    url: "/tooltip/" + Tooltip.slug,
    dataType: "json",
    global: false,
    success: function(data) {
        if(isHovering == $(this).attr('id')) {
           $('.tt').html(data.description);
           Tooltip.init();
           $('.tt').attr("style","left:"+Tooltip.settings.left_offset+"px;top:"+Tooltip.settings.top_offset+"px");
           $('.tt').show();
        }

        Tooltip.cache[slug] = data; //still cache it
    }
});

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

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