设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 238|回复: 0

Delay jquery hover event

[复制链接]
发表于 2013-10-27 10:10:40 | 显示全部楼层 |阅读模式
Without delay, when user hovers over a link or label. the hover event to occur immediately and that is annoying when the user is just moving the mouse across the screen.
  1. $(function() {
  2.     $('#container a').hover(function() {
  3.         $('<div id="fileinfo" />').load('ReadTextFileX.aspx',
  4.             {filename:'file.txt'},
  5.             function() {
  6.                 $(this).appendTo('#info');
  7.             }
  8.          );
  9.     },
  10.         function() { $('#info').remove(); }
  11.     });
  12. });
复制代码

After adding the HoverIntent plugin the above code was changed to the following to implement it.  Very simple to implement.
  1. $(function() {
  2.     hiConfig = {
  3.         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
  4.         interval: 200, // number = milliseconds for onMouseOver polling interval
  5.         timeout: 200, // number = milliseconds delay before onMouseOut
  6.         over: function() {
  7.             $('<div id="fileinfo" />').load('ReadTextFileX.aspx', {filename:'file.txt'},
  8.                 function() {
  9.                    $(this).appendTo('#info');
  10.                 }
  11.              );
  12.         }, // function = onMouseOver callback (REQUIRED)
  13.         out: function() { $('#info').remove();  } // function = onMouseOut callback (REQUIRED)
  14.     }
  15.     $('#container a').hoverIntent(hiConfig)
  16. }
复制代码

the hoverIntent plugin for jquery: http://cherne.net/brian/resources/jquery.hoverIntent.htmlIt's absolutely perfect and I've used it on nearly every project that required mouseover activation of menus etc...
There is one gotcha to this approach, some interfaces are devoid of a 'hover' state eg. mobile browsers like safari on the iphone. You may be hiding an important part of the interface or navigation with no way to open it on such a device. You could get round this with device specific CSS.

non-plugin solution
  1. $(function() {
  2.         var timer;

  3.         $('#container a').hover(function() {
  4.                 if(timer) {
  5.                         clearTimeout(timer);
  6.                         timer = null
  7.                 }
  8.                 timer = setTimeout(function() {
  9.                         $('<div id="fileinfo" />').load('ReadTextFileX.aspx',
  10.                                 {filename:'file.txt'},
  11.                                 function() {
  12.                                         $(this).appendTo('#info');
  13.                                 }
  14.                         );
  15.                 }, 500)
  16.     },
  17.     // mouse out
  18.     });
  19. });
复制代码

if you happen to be an unfortunate sod who works on a website with a long and protracted process for approval of jQuery plugins, here's a quick and dirty solution which worked well for me:
  1. $('li.contracted').hover(function () {
  2.     var expanding = $(this);
  3.     var timer = window.setTimeout(function () {
  4.         expanding.data('timerid', null);

  5.             ... do stuff

  6.     }, 300);
  7.     //store ID of newly created timer in DOM object
  8.     expanding.data('timerid', timer);
  9. }, function () {
  10.     var timerid = $(this).data('timerid');
  11.     if (timerid != null) {
  12.         //mouse out, didn't timeout. Kill previously started timer
  13.         window.clearTimeout(timerid);
  14.     }
  15. });
复制代码

This one's just for expanding an <li> if the mouse has been on it for longer than 300ms.
The easy.  Delay open menu if user keeping mouseenter on obj over 300ms:
  1. var sleep = 0;
  2.      $('#category li').mouseenter(function() {
  3.     sleep = 1;
  4.     $('#category li').mouseleave(function() {
  5.         sleep = 0;
  6.     });
  7.     var ob = $(this);
  8.     setTimeout(function() {                        
  9.         if(sleept==1) {

  10.         [...] Example:
  11.         $('#'+ob.attr('rel')).show();

  12.         }
  13.     },300);

  14. });
复制代码

From: stackoverflow

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 02:19 , Processed in 0.011407 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表