如何延迟()qtip()工具提示加载

时间:2011-03-26 03:32:09

标签: javascript jquery delay qtip

我这样加载:

$('.selector').each(function(){
$(this).qtip({
     content: { url: '/qtip.php?'+$(this).attr('rel')+' #'+$(this).attr('div'), text:'<center><img src="/images/loader.gif" alt="loading..." /></center>'  },

     show: { delay: 700, solo: true,effect: { length: 500 }},
     hide: { fixed: true, delay: 200 },

     position: {
     corner: {
        target: 'topRight',
        tooltip: 'left'
                }
                },
                show: {
          // Show it on click
         solo: true // And hide all other tooltips
      },
     style: {
       name: 'light',
       width: 730,border: {
         width: 4,
         radius: 3,
         color: '#5588CC'
      }    
       } 
   });

});

看起来如果有效果的原因。但qtip.php它没有延迟,这是我真正想要的(减少不必要的请求)

那么,我可以在加载qtip.php之前延迟300ms吗?

非常感谢

3 个答案:

答案 0 :(得分:7)

您可以将其设置为使用自定义事件,然后在超时后触发事件。如果您想等到鼠标停止,hoverIntent plugin可能会有所帮助。

使用hoverIntent:

$(selector).hoverIntent(function() {
    $(this).trigger('show-qtip');
}, function() {
    $(this).trigger('hide-qtip');
}).qtip({
    // ...
    show: {
        when: { event: 'show-qtip' }
    },
    hide: {
        when: { event: 'hide-qtip' }
    }
});

如果你想让hoverIntent在触发之前等待更长时间,你可以给它一个具有interval属性的配置对象:

$(selector).hoverIntent({
    over: showFunction,
    out: hideFunction,
    interval: 300 // Don't trigger until the mouse is still for 300ms
});

没有插件(我没有测试过):

(function() { // Create a private scope
    var timer = null;
    var delay = 300; // Set this to however long you want to wait

    $(selector).hover(function() {
        var $this = $(this);
        timer = setTimeout(function() {
            $this.trigger('show-qtip');
        }, delay);
    }, function() {
        if (timer) {
            clearTimeout(timer);
        }
    }).qtip({
        // ...
        show: {
            when: { event: 'show-qtip' }
        }
    });
})();

答案 1 :(得分:2)

这里我刚刚创建了另一个参数,它使用起来更简单,我在qtip1中测试了这个(不确定qtip2)

$('.qtip').qtip({
    show: { 
        when: 'mouseover',
        //customized param, when 'mouseout' the qtip will not shown and delay will clean
        cancel : 'mouseout',
        delay: 500
    }
});

要添加此参数,您需要在qtip中修改assignEvents()函数的代码:

function assignEvents()
{
    ...

    function showMethod(event)
    {
        if(self.status.disabled === true) return;

         // If set, hide tooltip when inactive for delay period
         if(self.options.hide.when.event == 'inactive')
         {
            // Assign each reset event
            $(inactiveEvents).each(function()
            {
               hideTarget.bind(this+'.qtip-inactive', inactiveMethod);
               self.elements.content.bind(this+'.qtip-inactive', inactiveMethod);
            });

            // Start the inactive timer
            inactiveMethod();
         };

         // Clear hide timers
         clearTimeout(self.timers.show);
         clearTimeout(self.timers.hide);
// line : 1539         

// Added code
--------------------------------------------
         // Cancel show timers
         if(self.options.show.cancel)
         {
             showTarget.bind(self.options.show.cancel,function(){
                 clearTimeout(self.timers.show);
             });
         }
--------------------------------------------

         // Start show timer
         self.timers.show = setTimeout(function(){ self.show(event); },self.options.show.delay);
      };

答案 2 :(得分:1)

对于qtip2,在初始化插件时会有一个名为show的参数,它表示在show.event上触发show.target时延迟显示工具提示的时间(以毫秒为单位) {1}}。

例如:

/*This tooltip will only show after hovering the `show.target` for 2000ms (2 seconds):*/

jQuery('.selector').qtip({
    content: {
        text: 'I have a longer delay then default qTips'
    },
    show: {
        delay: 2000
    }
});