Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. /*
  2. NAME: Jet Responsive Megamenu
  3. AUTHOR PAGE: http://codecanyon.net/user/marcoarib
  4. ITEM PAGE: http://codecanyon.net/item/jet-responsive-megamenu/5719593
  5. */
  6.  
  7. jQuery.fn.jetmenu = function(options){
  8. var settings = {
  9. indicator :true // indicator that indicates a submenu
  10. ,speed :300 // submenu speed
  11. ,delay :0 // submenu show delay
  12. ,hideClickOut :true // hide submenus when click outside menu
  13. ,align :"left" // menu alignment (left/right)
  14. ,submenuTrigger :"hover" // defines if submenu appears after hover/click
  15. }
  16. $.extend( settings, options );
  17.  
  18. var menu = $(".jetmenu");
  19. var lastScreenWidth = window.innerWidth;
  20. var bigScreen = false;
  21.  
  22. $(menu).prepend("<li class='showhide'><span class='title'>MENU</span><span class='icon'><em></em><em></em><em></em><em></em></span></li>");
  23.  
  24. if(settings.indicator == true){
  25. $(menu).find("a").each(function(){
  26. if($(this).siblings(".dropdown, .megamenu").length > 0){
  27. $(this).append("<span class='indicator'>+</span>");
  28. }
  29. });
  30. }
  31.  
  32. screenSize();
  33.  
  34. $(window).resize(function() {
  35. if(lastScreenWidth <= 768 && window.innerWidth > 768){
  36. unbindEvents();
  37. hideCollapse();
  38. bindHover();
  39. if(settings.align == "right" && bigScreen == false){
  40. rightAlignMenu();
  41. bigScreen = true;
  42. }
  43. }
  44. if(lastScreenWidth > 768 && window.innerWidth <= 768){
  45. unbindEvents();
  46. showCollapse();
  47. bindClick();
  48. if(bigScreen == true){
  49. rightAlignMenu();
  50. bigScreen = false;
  51. }
  52. }
  53. if(settings.align == "right"){
  54. if(lastScreenWidth > 768 && window.innerWidth > 768)
  55. fixSubmenuRight();
  56. }
  57. else{
  58. if(lastScreenWidth > 768 && window.innerWidth > 768)
  59. fixSubmenuLeft();
  60. }
  61. lastScreenWidth = window.innerWidth;
  62. });
  63.  
  64. function screenSize(){
  65. if(window.outerWidth <= 768){
  66. showCollapse();
  67. bindClick();
  68. if(bigScreen == true){
  69. rightAlignMenu();
  70. bigScreen = false;
  71. }
  72. }
  73. else{
  74. hideCollapse();
  75. bindHover();
  76. if(settings.align == "right"){
  77. rightAlignMenu();
  78. bigScreen = true;
  79. }
  80. else{
  81. fixSubmenuLeft();
  82. }
  83. }
  84. }
  85.  
  86. function bindHover(){
  87. if (navigator.userAgent.match(/Mobi/i) || window.navigator.msMaxTouchPoints > 0 || settings.submenuTrigger == "click"){
  88. $(menu).find("a").on("click touchstart", function(e){
  89. e.stopPropagation();
  90. e.preventDefault();
  91. $(this).parent("li").siblings("li").find(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
  92. if($(this).siblings(".dropdown, .megamenu").css("display") == "none"){
  93. $(this).siblings(".dropdown, .megamenu").stop(true, true).delay(settings.delay).fadeIn(settings.speed);
  94. return false;
  95. }
  96. else{
  97. $(this).siblings(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
  98. $(this).siblings(".dropdown").find(".dropdown").stop(true, true).fadeOut(settings.speed);
  99. }
  100. window.location.href = $(this).attr("href");
  101. });
  102.  
  103. $(menu).find("li").bind("mouseleave", function(){
  104. $(this).children(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
  105. });
  106.  
  107. if(settings.hideClickOut == true){
  108. $(document).bind("click.menu touchstart.menu", function(ev){
  109. if($(ev.target).closest(menu).length == 0){
  110. $(menu).find(".dropdown, .megamenu").fadeOut(settings.speed);
  111. }
  112. });
  113. }
  114. }
  115. else{
  116. $(menu).find("li").bind("mouseenter", function(){
  117. $(this).children(".dropdown, .megamenu").stop(true, true).delay(settings.delay).fadeIn(settings.speed);
  118. }).bind("mouseleave", function(){
  119. $(this).children(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
  120. });
  121. }
  122. }
  123.  
  124. function bindClick(){
  125. $(menu).find("li:not(.showhide)").each(function(){
  126. if($(this).children(".dropdown, .megamenu").length > 0){
  127. $(this).children("a").bind("click", function(e){
  128. if($(this).siblings(".dropdown, .megamenu").css("display") == "none"){
  129. $(this).siblings(".dropdown, .megamenu").delay(settings.delay).slideDown(settings.speed).focus();
  130. $(this).parent("li").siblings("li").find(".dropdown, .megamenu").slideUp(settings.speed);
  131. return false;
  132. }
  133. else{
  134. $(this).siblings(".dropdown, .megamenu").slideUp(settings.speed).focus();
  135. firstItemClick = 1;
  136. }
  137. });
  138. }
  139. });
  140. }
  141.  
  142. function showCollapse(){
  143. $(menu).children("li:not(.showhide)").hide(0);
  144. $(menu).children("li.showhide").show(0);
  145. $(menu).children("li.showhide").bind("click", function(){
  146. if($(menu).children("li").is(":hidden")){
  147. $(menu).children("li").slideDown(settings.speed);
  148. }
  149. else{
  150. $(menu).children("li:not(.showhide)").slideUp(settings.speed);
  151. $(menu).children("li.showhide").show(0);
  152. $(menu).find(".dropdown, .megamenu").hide(settings.speed);
  153. }
  154. });
  155. }
  156.  
  157. function hideCollapse(){
  158. $(menu).children("li").show(0);
  159. $(menu).children("li.showhide").hide(0);
  160. }
  161.  
  162. function rightAlignMenu(){
  163. $(menu).children("li").addClass("jsright");
  164. var items = $(menu).children("li");
  165. $(menu).children("li:not(.showhide)").detach();
  166. for(var i = items.length; i >= 1; i--){
  167. $(menu).append(items[i]);
  168. }
  169. fixSubmenuRight();
  170. }
  171.  
  172. function fixSubmenuRight(){
  173. $(menu).children("li").removeClass("last");
  174. var items = $(menu).children("li");
  175. for(var i = 1; i <= items.length; i++){
  176. if($(items[i]).children(".dropdown, .megamenu").length > 0){
  177. var lastItemsWidth = 0;
  178. for(var y = 1; y <= i; y++){
  179. lastItemsWidth = lastItemsWidth + $(items[y]).outerWidth();
  180. }
  181. if($(items[i]).children(".dropdown, .megamenu").outerWidth() > lastItemsWidth){
  182. $(items[i]).addClass("last");
  183. }
  184. }
  185. }
  186. }
  187.  
  188. function fixSubmenuLeft(){
  189. $(menu).children("li").removeClass("fix-sub");
  190. var items = $(menu).children("li");
  191. var menuWidth = $(menu).outerWidth();
  192. var itemsWidth = 0;
  193. for(var i = 1; i <= items.length; i++){
  194. if($(items[i]).children(".dropdown, .megamenu").length > 0){
  195. if($(items[i]).position().left + $(items[i]).children(".dropdown, .megamenu").outerWidth() > menuWidth){
  196. $(items[i]).addClass("fix-sub");
  197. }
  198. }
  199. }
  200. }
  201.  
  202. function unbindEvents(){
  203. $(menu).find("li, a").unbind();
  204. $(document).unbind("click.menu touchstart.menu");
  205. $(menu).find(".dropdown, .megamenu").hide(0);
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement