【问题标题】:delay qTip call using setTimeout and clearTimeout使用 setTimeout 和 clearTimeout 延迟 qTip 调用
【发布时间】:2011-09-28 14:36:20
【问题描述】:

这工作正常:

$('#panel_derecho a.tooltip').each(function(){

  $(this).qtip({
     content: { url: '/includes/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: 'topLeft',
        tooltip: 'middleRight'
                }
                },
     style: {
       name: 'light',
       width: 700,
       padding: 0,border: {
         width: 4,
         radius: 3,
         color: '#5588CC'
      }
       }
   });

});  

但有时用户在向下滚动页面时悬停,并且许多对 qtip 的调用不会显示在调用的位置,因此认为添加超时是最好的选择(不是吗?)

所以我尝试将 delayTip var 设置为等待时间(悬停时)并在鼠标移出时清除它:

$('#panel_derecho a.tooltip').each(function(){
    var delayTip;

       $(this).bind('mouseover',function(){
            delayTip = setTimeout(function () {
              $(this).qtip({
                 content: { url: '/includes/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: 'topLeft',
                    tooltip: 'middleRight'
                            }
                            },
                 style: {
                   name: 'light',
                   width: 700,
                   padding: 0,border: {
                     width: 4,
                     radius: 3,
                     color: '#5588CC'
                  }
                   }
               });
           }, 500);
       };
       $(this).bind('mouseout',function(){  clearTimeout(delayTip);  });

    });

提示是没有显示工具提示,并且在 firebug 中没有错误跳转,

我错过了什么?

【问题讨论】:

  • 我会在其中添加一些调试代码,以确保您知道 mouseover 和 mouseout 事件处理程序正在触发。 console.log('mouseover fired')

标签: javascript jquery timeout settimeout qtip


【解决方案1】:

当超时触发 this 关键字引用全局对象(很可能是 window)时,尝试执行以下操作:

$('#panel_derecho a.tooltip').each(function(){
    var delayTip = 0;
    var self = $(this);
    self.bind('mouseover',function(){
        if (delayTip === 0) delayTip = setTimeout(function () {
            self.qtip({
                content: { url: '/includes/qtip.php?'+self.attr('rel')+' #'+self.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: 'topLeft',
                        tooltip: 'middleRight'
                    }
                },
                style: {
                    name: 'light',
                    width: 700,
                    padding: 0,border: {
                        width: 4,
                        radius: 3,
                        color: '#5588CC'
                    }
                }
            });
            delayTip = 0;
        }, 500);
    };
    self.bind('mouseout',function(){ clearTimeout(delayTip); delayTip = 0; });
});

【讨论】:

  • 这种方式看起来是一种改进。提示已显示,但在悬停时会多次调用...?
  • 嘿,谢谢!,这样会更好;但是第一次悬停(我使用console.log检查)没有触发qtip,第二次+次是的。 (而且只有一个电话);为什么这是可能的?我也将 delayTip 从 .each() 函数中移出
猜你喜欢
  • 2021-02-23
  • 2022-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多