PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/jquery.droppy.js

https://github.com/browep/pumpdump
JavaScript | 200 lines | 114 code | 30 blank | 56 comment | 22 complexity | 38d3a1188e2496f6dfae38b3b76edfd7 MD5 | raw file
  1. /**
  2. * hoverIntent is similar to jQuery's built-in "hover" function except that
  3. * instead of firing the onMouseOver event immediately, hoverIntent checks
  4. * to see if the user's mouse has slowed down (beneath the sensitivity
  5. * threshold) before firing the onMouseOver event.
  6. *
  7. * hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
  8. * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
  9. *
  10. * hoverIntent is currently available for use in all personal or commercial
  11. * projects under both MIT and GPL licenses. This means that you can choose
  12. * the license that best suits your project, and use it accordingly.
  13. *
  14. * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
  15. * $("ul li").hoverIntent( showNav , hideNav );
  16. *
  17. * // advanced usage receives configuration object only
  18. * $("ul li").hoverIntent({
  19. * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
  20. * interval: 100, // number = milliseconds of polling interval
  21. * over: showNav, // function = onMouseOver callback (required)
  22. * timeout: 0, // number = milliseconds delay before onMouseOut function call
  23. * out: hideNav // function = onMouseOut callback (required)
  24. * });
  25. *
  26. * @param f onMouseOver function || An object with configuration options
  27. * @param g onMouseOut function || Nothing (use configuration options object)
  28. * @author Brian Cherne brian(at)cherne(dot)net
  29. */
  30. (function($) {
  31. $.fn.hoverIntent = function(f,g) {
  32. // default configuration options
  33. var cfg = {
  34. sensitivity: 15,
  35. interval: 100,
  36. timeout: 0
  37. };
  38. // override configuration options with user supplied object
  39. cfg = $.extend(cfg, g ? { over: f, out: g } : f );
  40. // instantiate variables
  41. // cX, cY = current X and Y position of mouse, updated by mousemove event
  42. // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
  43. var cX, cY, pX, pY;
  44. // A private function for getting mouse position
  45. var track = function(ev) {
  46. cX = ev.pageX;
  47. cY = ev.pageY;
  48. };
  49. // A private function for comparing current and previous mouse position
  50. var compare = function(ev,ob) {
  51. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  52. // compare mouse positions to see if they've crossed the threshold
  53. if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
  54. $(ob).unbind("mousemove",track);
  55. // set hoverIntent state to true (so mouseOut can be called)
  56. ob.hoverIntent_s = 1;
  57. return cfg.over.apply(ob,[ev]);
  58. } else {
  59. // set previous coordinates for next time
  60. pX = cX; pY = cY;
  61. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  62. ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
  63. }
  64. };
  65. // A private function for delaying the mouseOut function
  66. var delay = function(ev,ob) {
  67. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  68. ob.hoverIntent_s = 0;
  69. return cfg.out.apply(ob,[ev]);
  70. };
  71. // A private function for handling mouse 'hovering'
  72. var handleHover = function(e) {
  73. // copy objects to be passed into t (required for event object to be passed in IE)
  74. var ev = jQuery.extend({},e);
  75. var ob = this;
  76. // cancel hoverIntent timer if it exists
  77. if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
  78. // if e.type == "mouseenter"
  79. if (e.type == "mouseenter") {
  80. // set "previous" X and Y position based on initial entry point
  81. pX = ev.pageX; pY = ev.pageY;
  82. // update "current" X and Y position based on mousemove
  83. $(ob).bind("mousemove",track);
  84. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  85. if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
  86. // else e.type == "mouseleave"
  87. } else {
  88. // unbind expensive mousemove event
  89. $(ob).unbind("mousemove",track);
  90. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  91. if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
  92. }
  93. };
  94. // bind the function to the two event listeners
  95. return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover);
  96. };
  97. })(jQuery);
  98. /*
  99. * Droppy 0.1.2
  100. * (c) 2008 Jason Frame (jason@onehackoranother.com)
  101. */
  102. $(function() {
  103. $('#nav ul>li>a').hover(function() { //mouse in
  104. $(this).animate({ paddingLeft: '15px' }, 200);
  105. }, function() { //mouse out
  106. $(this).stop().animate({ paddingLeft: '6px' }, 200);
  107. });
  108. });
  109. $(function() {
  110. $('.widget-list>li>a').hover(function() { //mouse in
  111. $(this).animate({ paddingLeft: '10px' }, 200);
  112. }, function() { //mouse out
  113. $(this).stop().animate({ paddingLeft: '0px' }, 200);
  114. });
  115. $('.selected>a').hover(function() { //mouse in
  116. $(this).animate({ paddingLeft: '20px' }, 200);
  117. }, function() { //mouse out
  118. $(this).stop().animate({ paddingLeft: '10px' }, 200);
  119. });
  120. });
  121. $(function() {
  122. $('#nav').droppy();
  123. });
  124. $.fn.droppy = function(options) {
  125. options = $.extend({speed: 100}, options || {});
  126. this.each(function() {
  127. var root = this, zIndex = 1000;
  128. function getSubnav(ele) {
  129. if (ele.nodeName.toLowerCase() == 'li') {
  130. var subnav = $('> ul', ele);
  131. return subnav.length ? subnav[0] : null;
  132. } else {
  133. return ele;
  134. }
  135. }
  136. function getActuator(ele) {
  137. if (ele.nodeName.toLowerCase() == 'ul') {
  138. return $(ele).parents('li')[0];
  139. } else {
  140. return ele;
  141. }
  142. }
  143. function hide() {
  144. var subnav = getSubnav(this);
  145. if (!subnav) return;
  146. $.data(subnav, 'cancelHide', false);
  147. setTimeout(function() {
  148. if (!$.data(subnav, 'cancelHide')) {
  149. $(subnav).slideUp(options.speed);
  150. }
  151. }, 200);
  152. }
  153. function show() {
  154. var subnav = getSubnav(this);
  155. if (!subnav) return;
  156. $.data(subnav, 'cancelHide', true);
  157. $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
  158. if (this.nodeName.toLowerCase() == 'ul') {
  159. var li = getActuator(this);
  160. $(li).addClass('hover');
  161. $('> a', li).addClass('hover');
  162. }
  163. }
  164. $('ul, li', this).hoverIntent(show, hide);
  165. $('li', this).hover(
  166. function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
  167. function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
  168. );
  169. });
  170. };