Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

84 řádky
3.1 KiB

  1. (function($){
  2. /* hoverIntent by Brian Cherne */
  3. $.fn.hoverIntent = function(f,g) {
  4. // default configuration options
  5. var cfg = {
  6. sensitivity: 7,
  7. interval: 100,
  8. timeout: 0
  9. };
  10. // override configuration options with user supplied object
  11. cfg = $.extend(cfg, g ? { over: f, out: g } : f );
  12. // instantiate variables
  13. // cX, cY = current X and Y position of mouse, updated by mousemove event
  14. // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
  15. var cX, cY, pX, pY;
  16. // A private function for getting mouse position
  17. var track = function(ev) {
  18. cX = ev.pageX;
  19. cY = ev.pageY;
  20. };
  21. // A private function for comparing current and previous mouse position
  22. var compare = function(ev,ob) {
  23. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  24. // compare mouse positions to see if they've crossed the threshold
  25. if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
  26. $(ob).unbind("mousemove",track);
  27. // set hoverIntent state to true (so mouseOut can be called)
  28. ob.hoverIntent_s = 1;
  29. return cfg.over.apply(ob,[ev]);
  30. } else {
  31. // set previous coordinates for next time
  32. pX = cX; pY = cY;
  33. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  34. ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
  35. }
  36. };
  37. // A private function for delaying the mouseOut function
  38. var delay = function(ev,ob) {
  39. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  40. ob.hoverIntent_s = 0;
  41. return cfg.out.apply(ob,[ev]);
  42. };
  43. // A private function for handling mouse 'hovering'
  44. var handleHover = function(e) {
  45. // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
  46. var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
  47. while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
  48. if ( p == this ) { return false; }
  49. // copy objects to be passed into t (required for event object to be passed in IE)
  50. var ev = jQuery.extend({},e);
  51. var ob = this;
  52. // cancel hoverIntent timer if it exists
  53. if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
  54. // else e.type == "onmouseover"
  55. if (e.type == "mouseover") {
  56. // set "previous" X and Y position based on initial entry point
  57. pX = ev.pageX; pY = ev.pageY;
  58. // update "current" X and Y position based on mousemove
  59. $(ob).bind("mousemove",track);
  60. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  61. if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
  62. // else e.type == "onmouseout"
  63. } else {
  64. // unbind expensive mousemove event
  65. $(ob).unbind("mousemove",track);
  66. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  67. if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
  68. }
  69. };
  70. // bind the function to the two event listeners
  71. return this.mouseover(handleHover).mouseout(handleHover);
  72. };
  73. })(jQuery);