【问题标题】:jQuery UI Tooltip loading and show with delayjQuery UI 工具提示加载和延迟显示
【发布时间】:2014-01-14 14:56:25
【问题描述】:

该网站包含带有动态内容的工具提示链接。我正在使用 jquery UI Tooltip 来显​​示它们。我不希望每次用户不小心将光标悬停在链接上显示工具提示。我只想显示工具提示,如果它将光标在链接上延迟几秒钟。 它应该像在 Gmail 中一样工作,当您将鼠标悬停在发件人姓名上时,您会看到有关他的信息(请参阅picture)。

我需要工具提示,用户可以与之交互,所以我使用此代码(感谢 Antonimo https://stackoverflow.com/a/15014759/274417)

$( document ).tooltip({
  show: null, // show immediately 
  items: 'input',
  hide: {
    effect: "", // fadeOut
  },
  open: function( event, ui ) {
    ui.tooltip.animate({ top: ui.tooltip.position().top + 10 }, "fast" );
  },
  close: function( event, ui ) {
    ui.tooltip.hover(
        function () {
            $(this).stop(true).fadeTo(400, 1); 
            //.fadeIn("slow"); // doesn't work because of stop()
        },
        function () {
            $(this).fadeOut("400", function(){ $(this).remove(); })
        }
    );
  }
});

Example here(当鼠标悬停在带有工具提示的元素上时,您会看到所有这些混乱)

如何做得更好?使用超时?或者也许我应该使用 hoverIntent 插件?但是它将如何编码?

【问题讨论】:

标签: jquery html css jquery-ui tooltip


【解决方案1】:

这是一种方法:

HTML

<body>
    <p><label for="age">Your age:</label><input class="age" id="age" /></p>
    <p><label for="age">Your age:</label><input class="age" id="age2" /></p>
    <p><label for="age">Your age:</label><input class="age" id="age3"/></p>
    <p><label for="age">Your age:</label><input class="age" id="age4"/></p>
    <p><label for="age">Your age:</label><input class="age" id="age5"/></p>
</body>

jQuery

var timeout;
var finishTimeout = false;
$(".age").tooltip({
  show: null, // show immediately 
  items: 'input',
  hide: {
    effect: "", // fadeOut
  },
     content: function(){
         if(!finishTimeout)
             return false;
         //ajax call here 
         return 'test';
     },
  open: function( event, ui ) {
    //ui.tooltip.animate({ top: ui.tooltip.position().top + 10 }, "fast" );
  },
  close: function( event, ui ) {
    ui.tooltip.hover(
        function () {
            $(this).stop(true).fadeTo(400, 1); 
            //.fadeIn("slow"); // doesn't work because of stop()
        },
        function () {
            $(this).fadeOut("400", function(){ $(this).remove(); })
        }
    );
  }
});
$('.age').mouseover(function(){
   var el = $(this);
    timeout = setTimeout(function(){
       finishTimeout = true;
       el.tooltip( "open" );
       finishTimeout = false;
    },1000);
});
$('.age').mouseout(function(){
    clearTimeout(timeout);
});

示例 http://jsfiddle.net/4sSkc/

【讨论】:

  • 是的,我知道这个选项,谢谢。但是,只要光标悬停在链接上,对服务器的请求仍然是不必要的,对吧?
  • 在 { content: "ajax request" } 选项中
  • 我收到$(...).tooltip is not a function
  • @JoshHarington 工具提示在标准 jquery 库中不可用。您还需要包含 jQuery ui。见:jqueryui.com/tooltip
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
相关资源
最近更新 更多