【问题标题】:Add delay to jquery mouseleave tooltip hover为 jquery mouseleave 工具提示悬停添加延迟
【发布时间】:2019-07-02 06:29:18
【问题描述】:

我正在尝试编辑 WordPress 插件的内置工具提示功能。目前它在mouseleave 上立即消失。我只需要它保持一两秒钟。

我已经阅读了大量类似的问题,但我不知道在哪里为插件的现有代码添加超时延迟:

init_tooltip();
$(window).resize(init_tooltip);
target.data('is_moving', false);

var remove_tooltip = function () {
  if (target.data('is_moving')) {
    return;
  }

  tooltip.removeClass(fadin).addClass(fadeout);
  var speed = 15000;

  tooltip.animate({
    opacity: 0
  }, speed, function () {
    $(this).remove();
  });

  if (!tiphtml && !is_swatch && !is_swatch_desc && !is_swatch_lbl_desc && !is_swatch_img && !is_swatch_img_lbl && !is_swatch_img_desc && !is_swatch_img_lbl_desc) {
    target.attr('title', tip);
  }
};

target
  .on('tmmovetooltip', function () {
    target.data('is_moving', true);
    init_tooltip(1);
  })
  .on('mouseleave tmhidetooltip', remove_tooltip);

target.closest('label').on('mouseleave tmhidetooltip', remove_tooltip);

tooltip.on('click', remove_tooltip);

targets.closest('label').on('mouseenter tmshowtooltip', showtooltip);
targets.on('mouseenter tmshowtooltip', showtooltip);

return targets;

【问题讨论】:

  • 你能告诉我们这个插件是什么吗?

标签: jquery mouseout


【解决方案1】:

编辑:我已经用最好的猜测更新了这个答案,只在鼠标移出时才会出现工具提示延迟。它的要点是你需要两个函数

您需要将 remove_tooltip 函数的内部包装在 setTimeout() 中,并具有所需的延迟。更改末尾的数字以更改延迟量。

将您的代码替换为以下内容:

    init_tooltip();
    $(window).resize(init_tooltip);
    target.data('is_moving', false);

    var remove_tooltip = function () {
      removeTooltipCore();
    };

    var remove_tooltip_with_delay = function () {
      setTimeout( function () {
        removeTooltipCore();
    }, 1000); //1 second delay
};

function removeTooltipCore() {
    if (target.data('is_moving')) {
            return;
          }

          tooltip.removeClass(fadin).addClass(fadeout);
          var speed = 15000;

          tooltip.animate({
            opacity: 0
          }, speed, function () {
            $(this).remove();
          });

          if (!tiphtml && !is_swatch && !is_swatch_desc && !is_swatch_lbl_desc && !is_swatch_img && !is_swatch_img_lbl && !is_swatch_img_desc && !is_swatch_img_lbl_desc) {
            target.attr('title', tip);
          }
}

target
  .on('tmmovetooltip', function () {
    target.data('is_moving', true);
    init_tooltip(1);
  })
  .on('tmhidetooltip', remove_tooltip)
  .on('mouseleave', remove_tooltip_with_delay);

target.closest('label').on('tmhidetooltip', remove_tooltip);
target.closest('label').on('mouseleave', remove_tooltip_with_delay);

tooltip.on('click', remove_tooltip);

【讨论】:

  • 你是圣人。这非常有效。太感谢了。作为参考,这个插件被称为 Woocommerce 的“额外产品选项”
  • 很高兴听到这个消息!如果您能接受这个答案是正确的,我将不胜感激,它给了我很多代表:)
  • 糟糕。忘了。新人来了它实际上只是有点工作。无论mouseleave如何,延迟都会开始。如果您有任何其他想法会很棒,但如果没有,也没什么大不了!
  • @Brian7865 看起来您为mouseleave 和tmhidetooltip 分配了相同的函数 - 如果您只想延迟mouseleave,您应该有两个函数实例,一个有延迟,一个没有。我已经更新了我的答案来演示这种方法。如果这不是 100% 没有语法错误或不能完美运行,请原谅我 - 我以前从未使用过这个特定的插件,我只是根据我的常识做出最好的猜测。希望这会有所帮助!
  • 我会尽可能尝试一下。无论哪种方式,我都感谢您的努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
相关资源
最近更新 更多