使用hoverIntent在鼠标悬停时在鼠标位置显示DIV

发布于 2024-12-26 11:36:58 字数 1149 浏览 5 评论 0原文

好的,我有一个包含有关工作信息的表。

目标是,当用户将鼠标悬停在该表中有关特定作业的行时,jQuery 会进行 Ajax 调用,检索有关该作业的数据并将其显示在鼠标位置的弹出窗口中。

我的 Javascript/jQuery 如下:

$('#report').find('tr').hoverIntent({  // mouseover
    over: statusOnHover,
    out: statusOffHover 
});


function statusOnHover(){   
        $.ajax({
            type: "POST",
            data: "data=" + $(this).attr('id'),
            url: "ajax/latest_update.php",
            success: function(msg){
                if (msg != ''){
                    $("#message").html(msg);
                    $("#message").css({
                        'position':absolute,
                        'top':event.pageY,
                        'left':event.pageX
                    });
                }
            }
        });
}
function statusOffHover(){
    $("#message").html('');
}

因此,我们找到一个表行,然后当用户打算将鼠标悬停在其上(使用悬停意图)时,它会运行鼠标悬停功能。该函数调用latest_update.php 脚本,该脚本根据从行ID 中提取的job_id 提供预格式化的HTML 数据示例。然后将此 HTML 数据插入到消息 div 中。

现在,AJAX 查询运行良好,并将数据复制到 div 中,但使 div 浮动到鼠标指针的 CSS 格式不起作用。当使用标准 .mouseover 和 .mouseout 时,此 CSS 确实有效。

到目前为止,我还没有很好地解决这个问题,并且已经尝试了很多方法。有人有什么想法吗?

Ok, so I have a table containing information about jobs.

The goal is that when the user hovers on a row in this table about a specific job, jQuery makes an Ajax call, retrieves the data about the job and displays it in a pop up at the mouse position.

My Javascript/jQuery is as follows:

$('#report').find('tr').hoverIntent({  // mouseover
    over: statusOnHover,
    out: statusOffHover 
});


function statusOnHover(){   
        $.ajax({
            type: "POST",
            data: "data=" + $(this).attr('id'),
            url: "ajax/latest_update.php",
            success: function(msg){
                if (msg != ''){
                    $("#message").html(msg);
                    $("#message").css({
                        'position':absolute,
                        'top':event.pageY,
                        'left':event.pageX
                    });
                }
            }
        });
}
function statusOffHover(){
    $("#message").html('');
}

So we're finding a table row, and then when the user intends to hover on it (using hoverIntent) it runs a mouse over function. This function calls the latest_update.php script which delivers a preformatted sample of HTML data based upon the job_id pulled from the row ID. This HTML data is then inserted into the message div.

Now the AJAX query runs fine, and it copies the data into the div, but the CSS formatting to make the div float to the mouse pointer does not work. This CSS DOES work when using standard .mouseover and .mouseout.

I haven't had much luck troubleshooting this so far and have tried a number of things. Does anyone have any ideas?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-01-02 11:36:58

我使用 mouseenter 和 mouseleave 使其工作,检查这个小提琴: http://jsfiddle.net/jv7YT/1/< /a>

$('#report').mouseenter(function(){
    //ajax call and show popup 
}).mouseleave(function(){ 
    // hide popup 
});

I make it work using mouseenter and mouseleave, check this fiddle: http://jsfiddle.net/jv7YT/1/

$('#report').mouseenter(function(){
    //ajax call and show popup 
}).mouseleave(function(){ 
    // hide popup 
});
月隐月明月朦胧 2025-01-02 11:36:58

不幸的是戴夫提供的答案没有给出正确的解决方案。它确实在悬停时显示了 div,但没有在鼠标指针位置显示必需的 DIV。

问题在于,在鼠标位置显示 div 的 CSS 只会在鼠标移动时调用,以获得所需的事件位置。

请注意,此解决方案仍然使用hoverIntent 来管理延迟。

正确代码如下:

$('#report').find('tr').hoverIntent({  // mouseover
    over: statusOnHover,
    out: statusOffHover 
});

function statusOnHover(){   
    $(this).mousemove(function(event){
        $('#message').css({'top':event.pageY,'left':event.pageX});
    });
    $.ajax({
        type: "POST",
        data: "data=" + $(this).attr('id'),
        url: "ajax/latest_update.php",
        success: function(msg){
            if (msg != ''){
                $('#message').html(msg).show();

            }
        }           
    }); 
}
function statusOffHover(){
    $("#message").html('');
}

Unfortunately the answer provided by Dave didn't give the correct solution. It did display the div on hover, but did not display the requisite DIV at the mouse pointer position.

The issue was that the CSS to display the div at mouse position would only be called at mouse move to get the required event positions.

Note that this solution still uses hoverIntent to manage the delay.

Correct code as follows:

$('#report').find('tr').hoverIntent({  // mouseover
    over: statusOnHover,
    out: statusOffHover 
});

function statusOnHover(){   
    $(this).mousemove(function(event){
        $('#message').css({'top':event.pageY,'left':event.pageX});
    });
    $.ajax({
        type: "POST",
        data: "data=" + $(this).attr('id'),
        url: "ajax/latest_update.php",
        success: function(msg){
            if (msg != ''){
                $('#message').html(msg).show();

            }
        }           
    }); 
}
function statusOffHover(){
    $("#message").html('');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文