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



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

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

这里的例子(当鼠标悬停在带有工具提示的元素上时,你可以看到所有这些混乱)

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

这里有一种方法可以做到这一点:

.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/

最新更新