【问题标题】:Show popover if hovering an element longer如果将元素悬停更长时间,则显示弹出框
【发布时间】:2017-04-28 17:35:01
【问题描述】:

使用 Bootstrap 弹出框将隐藏 div 的内容显示为弹出框内容 在主元素没有悬停至少一秒钟之前,如何实现不显示弹出框?

$(function () {
$("[data-toggle=popover]").popover({
    trigger: "manual",
    html: true,
    content: function () {
        var content = $(this).attr("data-popover-content");
        return $(content).children(".popover-body").html();
    },
    title: function () {
        var title = $(this).attr("data-popover-content");
        return $(title).children(".popover-heading").html();
    }
})
    .on("mouseenter", function () {
        var _this = this;

        $(_this).popover("show");

        $(".popover").on("mouseleave", function () {
            setTimeout(function () {
                if (!$(".popover:hover").length) {
                    $(_this).popover("hide");
                }
            }, 300);
        });
    })
    .on("mouseleave", function () {
        var _this = this;
        setTimeout(function () {
            if (!$(".popover:hover").length) {
                $(_this).popover("hide");
            }
        }, 300);
    });});

我有一个包含很多图标的表格的页面。每个图标都有一些很大的数据,因此被移动到可滚动的弹出窗口中。我希望显示 popowers,但不要在页面上移动鼠标,它们都会亮起。这就是为什么我需要在它们出现之前延迟。鼠标离开后的延迟是这样的,当我想输入它们并滚动它们的内容时,它们不会关闭。 我更改我的代码以单击打开它们,直到我得到另一个解决方案。 小提琴:https://jsfiddle.net/mandor/v5e73fzu/14/

【问题讨论】:

    标签: twitter-bootstrap-3 bootstrap-popover


    【解决方案1】:

    使用标志变量并使用setTimeouts...检查/设置它...

    var timerReady = false
    var showPopup;
    
    $("[data-toggle=popover]").popover({
      trigger: "manual",
      html: true,
      content: function() {
        var content = $(this).attr("data-popover-content");
        return $(content).children(".popover-body").html();
      },
      title: function() {
        var title = $(this).attr("data-popover-content");
        return $(title).children(".popover-heading").html();
      }
    })
    .on("mouseenter", function() {
      var _this = this;
    
      timerReady = true
    
      showPopup = setTimeout(function(){
        if(timerReady){
          $(_this).popover("show");
        }
      }, 1000)
    })
    .on("mouseleave", function() {
      clearTimeout(showPopup)
      timerReady = false
      var _this = this;
      setTimeout(function() {
        if (!$(".popover:hover").length) {
          $(_this).popover("hide");
        }
      }, 300);
    });
    

    【讨论】:

    • 你能看看我为测试这个而制作的小提琴吗?我不确定如何解决此延迟。我是一个JS菜鸟。 jsfiddle.net/mandor/v5e73fzu/12
    • 是的,这比我最初想象的要复杂一些。我已经编辑了我的答案 - 与标志变量类似的概念,只需要稍微改变范围。如果您发现这可以完成工作,请接受它。
    • 这行得通!谢谢你,先生。我已经更新了小提琴,以防其他人发现这有帮助:jsfiddle.net/mandor/v5e73fzu
    • 说得太早了。你看到当你有多个弹出项目并且你移动第一个到另一个?它显示了两个弹出窗口,尽管您只将第二个悬停了超过一秒钟:-/
    • 哦,是的,我明白你在说什么。让我再考虑一下 - 我不会放弃你,但我鼓励你看看你是否可以扩展我提供的解决方案,看看你是否也可以解决其他问题。
    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多