【问题标题】:Remove JQuery hover event when hovering over children将鼠标悬停在子项上时删除 JQuery 悬停事件
【发布时间】:2012-07-11 17:12:20
【问题描述】:

我正在构建一个工具提示,如果您将鼠标悬停在标记上,则会出现工具提示。

HTML:

<div class="trigger" data-loc="locationE" id="locationE">
    <div class="tooltip">
        <h2>Here is a title</h2>
        <p>Here is some Compy About this location</p>
        <h3>215.237.9932</h3>
    </div>
</div>

JQuery:

$(document).ready(function() {
    $(".tooltip").each(function() {
        toolHeight = $(this).outerHeight();
        toolHeightPlus = $(this).outerHeight() + 20;
        $(this).css({
            "top": -+toolHeight
        });
    });
    $(".trigger").hover(function() {
        $("#" + $(this).data("loc") + " .tooltip").stop().animate({
            "opacity": "1",
            "top": -+toolHeightPlus
        });
    }, function() {
        $("#" + $(this).data("loc") + " .tooltip").stop().animate({
            "opacity": "0",
            "top": -+toolHeight
        });
    });
});

我使用 data 属性的原因在这里不需要提及,因为它是用于应用程序的另一部分。我遇到的问题是,当用户将鼠标悬停在工具提示所在的位置时会触发工具提示动画,即使不透明度为 0。我希望工具提示仅在用户悬停在 .trigger 而不是 @ 时触发987654324@ 具有零不透明度。我将如何做到这一点?

【问题讨论】:

  • $('element').unbind('hover');

标签: jquery hover tooltip


【解决方案1】:

一种选择是在隐藏时将工具提示 css 显示设置为“无”,并在显示时设置为“阻止”。这将防止 .tooltip html 元素在不应该可见时占用任何空间。

【讨论】:

  • 对我来说,这个解决方案效果最好,结合不透明度和显示来达到预期的效果。谢谢!
  • @JCHASE11 - 这行不通。当您制作.tooltip display:none 时,.trigger 将没有大小,您将无法悬停它,除非您添加其他内容或使用 CSS 修复它的大小。
  • 哦,效果很好,看看我的 jquery: $(".trigger").hover(function(){ $("#" + $(this).data("loc") + " .tooltip").stop().css({"display" : "block"}).animate({"opacity" : "1", "top" : - + toolHeightPlus}); }, function(){ $("#" + $(this).data("loc") + ".tooltip").stop().animate({ opacity : 0, top : - + toolHeight}, 500, function(){ $( this).css({"display" : "none"}); }); });
【解决方案2】:

将工具提示标记移出触发器。由于触发器 div 包装了工具提示,它实际上是弹出工具提示。如果您打算将鼠标悬停在触发器上,然后将它们设为单独的元素。

【讨论】:

  • 当然,我可以这样做,但这会篡改我的应用程序的其余部分......如果没有解决方案,我可以这样做,同时将它们保留在触发器中。有什么办法让它们留在里面?
【解决方案3】:

您的请求看起来与您的 HTML 有问题,因为您的 HTML 中没有空间,只有 .trigger,所以在我看来,您的请求永远无法工作,除非您向不是的 .trigger 元素添加更多内容.tooltip 元素。

如果您确实为 .trigger 元素添加了更多空间,那么您可以在操作之前查看 target of the event 并查看它是否具有这样的正确类:

$(".trigger").hover(function(e) {
    if (!$(e.target).hasClass(".trigger")) return;
    $("#" + $(this).data("loc") + " .tooltip").stop().animate({
        "opacity": "1",
        "top": -+toolHeightPlus
    });
}, function(e) {
    $("#" + $(this).data("loc") + " .tooltip").stop().animate({
        "opacity": "0",
        "top": -+toolHeight
    });
});

【讨论】:

  • 感谢这个,很好的解决方案,但我不想添加任何额外的元素。
  • @JCHASE11 - 您是否了解您的 HTML 在屏幕上没有空间,只有 .trigger 而不是 .tooltip,所以如果不更改您的 HTML,就无法解决您的问题。
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多