提问者:小点点

jQuery UI工具提示加载并延迟显示


该站点有带有动态内容的工具提示的链接。我正在使用jquery UI工具提示来显示它们。我不希望每次用户不小心将光标放在链接上时,都显示工具提示。我想要显示工具提示只有当它延迟光标在链接上几秒钟。这应该像Gmail一样,当你悬停在发件人的名字上时,你会看到关于他的信息(见图)。

我需要工具提示,用户可以与之交互,所以我使用了这段代码(感谢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(); })
        }
    );
  }
});

这里的示例(当鼠标悬停在带有工具提示的元素上时,您可以看到所有这些乱七八糟的东西)

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


共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://jsfidle.net/4sskc/