superfish-1.4.1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Superfish v1.4.1 - 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. (function($){
  12. $.superfish = {};
  13. $.superfish.o = [];
  14. $.superfish.op = {};
  15. $.superfish.defaults = {
  16. hoverClass : 'sfHover',
  17. pathClass : 'overideThisToUse',
  18. delay : 800,
  19. animation : {opacity:'show'},
  20. speed : 'normal',
  21. oldJquery : false, /* set to true if using jQuery version below 1.2 */
  22. disableHI : false, /* set to true to disable hoverIntent usage */
  23. // callback functions:
  24. onInit : function(){},
  25. onBeforeShow: function(){},
  26. onShow : function(){}, /* note this name changed ('onshow' to 'onShow') from version 1.4 onward */
  27. onHide : function(){}
  28. };
  29. $.fn.superfish = function(op){
  30. var bcClass = 'sfbreadcrumb',
  31. over = function(){
  32. var $$ = $(this), menu = getMenu($$);
  33. getOpts(menu,true);
  34. clearTimeout(menu.sfTimer);
  35. $$.showSuperfishUl().siblings().hideSuperfishUl();
  36. },
  37. out = function(){
  38. var $$ = $(this), menu = getMenu($$);
  39. var o = getOpts(menu,true);
  40. clearTimeout(menu.sfTimer);
  41. if ( !$$.is('.'+bcClass) ) {
  42. menu.sfTimer=setTimeout(function(){
  43. $$.hideSuperfishUl();
  44. if (o.$path.length){over.call(o.$path);}
  45. },o.delay);
  46. }
  47. },
  48. getMenu = function($el){ return $el.parents('ul.superfish:first')[0]; },
  49. getOpts = function(el,menuFound){ el = menuFound ? el : getMenu(el); return $.superfish.op = $.superfish.o[el.serial]; },
  50. hasUl = function(){ return $.superfish.op.oldJquery ? 'li[ul]' : 'li:has(ul)'; };
  51. return this.each(function() {
  52. var s = this.serial = $.superfish.o.length;
  53. var o = $.extend({},$.superfish.defaults,op);
  54. o.$path = $('li.'+o.pathClass,this).each(function(){
  55. $(this).addClass(o.hoverClass+' '+bcClass)
  56. .filter(hasUl()).removeClass(o.pathClass);
  57. });
  58. $.superfish.o[s] = $.superfish.op = o;
  59. $(hasUl(),this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out)
  60. .not('.'+bcClass)
  61. .hideSuperfishUl();
  62. var $a = $('a',this);
  63. $a.each(function(i){
  64. var $li = $a.eq(i).parents('li');
  65. $a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
  66. });
  67. o.onInit.call(this);
  68. }).addClass('superfish');
  69. };
  70. $.fn.extend({
  71. hideSuperfishUl : function(){
  72. var o = $.superfish.op,
  73. $ul = $('li.'+o.hoverClass,this).add(this).removeClass(o.hoverClass)
  74. .find('>ul').hide().css('visibility','hidden');
  75. o.onHide.call($ul);
  76. return this;
  77. },
  78. showSuperfishUl : function(){
  79. var o = $.superfish.op,
  80. $ul = this.addClass(o.hoverClass)
  81. .find('>ul:hidden').css('visibility','visible');
  82. o.onBeforeShow.call($ul);
  83. $ul.animate(o.animation,o.speed,function(){ o.onShow.call(this); });
  84. return this;
  85. }
  86. });
  87. $(window).unload(function(){
  88. $('ul.superfish').each(function(){
  89. $('li',this).unbind('mouseover','mouseout','mouseenter','mouseleave');
  90. });
  91. });
  92. })(jQuery);