superfish.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Superfish v1.4.8 - jQuery menu widget
  3. * Copyright (c) 2008 Joel Birch
  4. *
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. *
  9. * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
  10. */
  11. /*
  12. * PLEASE READ THE FOLLOWING BEFORE PLAYING AROUND WITH ANYTHING. KTHNX.
  13. * Dev copy. Antechinus - 20th October 2011.
  14. * Code has been tweaked to remove stuff we do not need (IE7 fix, etc).
  15. * Remaining code appears to be essential for full functionality.
  16. * If contemplating changes, testing for full functionality is essential or a11y will be degraded.
  17. * Since a11y is the whole point of this system, degradation is not at all desirable regardless of personal preferences.
  18. * If you do not understand the a11y advantages of this system, please ask before making changes.
  19. *
  20. * Full functionality means:
  21. * 1/ hoverIntent plugin functions so that drop menus do NOT open or close instantly when cursor touches first level anchor.
  22. * 2/ The drop menus should only open when the cursor actually stops on the first level anchor, or is moving very slowly.
  23. * 3/ There should be a delay before the drop menus close on mouseout, for people with less than perfect tracking ability.
  24. * 4/ The drop menus must remain fully accessible via keyboard navigation (eg: the Tab key).
  25. */
  26. ;(function($){
  27. $.fn.superfish = function(op){
  28. var sf = $.fn.superfish,
  29. c = sf.c,
  30. over = function(){
  31. var $$ = $(this), menu = getMenu($$);
  32. clearTimeout(menu.sfTimer);
  33. $$.showSuperfishUl().siblings().hideSuperfishUl();
  34. },
  35. out = function(){
  36. var $$ = $(this), menu = getMenu($$), o = sf.op;
  37. clearTimeout(menu.sfTimer);
  38. menu.sfTimer=setTimeout(function(){
  39. o.retainPath=($.inArray($$[0],o.$path)>-1);
  40. $$.hideSuperfishUl();
  41. if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
  42. },o.delay);
  43. },
  44. getMenu = function($menu){
  45. var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
  46. sf.op = sf.o[menu.serial];
  47. return menu;
  48. },
  49. // This next line is essential, despite the other code for arrows being removed.
  50. // Changing the next line WILL break hoverIntent functionality. Very bad.
  51. addArrow = function($a){$a.addClass(c.anchorClass)};
  52. return this.each(function() {
  53. var s = this.serial = sf.o.length;
  54. var o = $.extend({},sf.defaults,op);
  55. o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
  56. $(this).addClass([o.hoverClass,c.bcClass].join(' '))
  57. .filter('li:has(ul)').removeClass(o.pathClass);
  58. });
  59. sf.o[s] = sf.op = o;
  60. $('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {})
  61. .not('.'+c.bcClass)
  62. .hideSuperfishUl();
  63. var $a = $('a',this);
  64. $a.each(function(i){
  65. var $li = $a.eq(i).parents('li');
  66. $a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
  67. });
  68. o.onInit.call(this);
  69. }).each(function() {
  70. var menuClasses = [c.menuClass];
  71. $(this).addClass(menuClasses.join(' '));
  72. });
  73. };
  74. var sf = $.fn.superfish;
  75. sf.o = [];
  76. sf.op = {};
  77. sf.c = {
  78. bcClass : 'sf-breadcrumb',
  79. menuClass : 'sf-js-enabled',
  80. anchorClass : 'sf-with-ul',
  81. };
  82. sf.defaults = {
  83. hoverClass : 'sfhover',
  84. pathClass : 'current',
  85. pathLevels : 1,
  86. delay : 700,
  87. animation : {opacity:'show'},
  88. speed : 400,
  89. disableHI : false, // Leave as false. True disables hoverIntent detection (not good).
  90. onInit : function(){}, // callback functions
  91. onBeforeShow: function(){},
  92. onShow : function(){},
  93. onHide : function(){}
  94. };
  95. $.fn.extend({
  96. hideSuperfishUl : function(){
  97. var o = sf.op,
  98. not = (o.retainPath===true) ? o.$path : '';
  99. o.retainPath = false;
  100. var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
  101. .find('>ul').hide().css('opacity','0');
  102. o.onHide.call($ul);
  103. return this;
  104. },
  105. showSuperfishUl : function(){
  106. var o = sf.op,
  107. sh = sf.c,
  108. $ul = this.addClass(o.hoverClass)
  109. .find('>ul:hidden').css('opacity','1');
  110. o.onBeforeShow.call($ul);
  111. $ul.animate(o.animation,o.speed,function(){o.onShow.call($ul);});
  112. return this;
  113. }
  114. });
  115. })(jQuery);