Jquery hide/show divs

Jquery hide/show divs

Hi there. I'm using the following code and desperately wondering how to get the initial box to display on page load rather than when someone clicks on a menu link. Is there a simpler way to do this?

  1. <script type="text/javascript">
  2. $(function() {
  3. var $menu = $('.menu'),
  4. $menuItems = $menu.children('a'),
  5. $mbWrapper = $('#content_wrapper'),
  6. $mbClose = $mbWrapper.children('.close'),
  7. $mbContentItems = $mbWrapper.children('.content'),
  8. $mbContentInnerItems= $mbContentItems.children('.content_inner');
  9. $mbPattern = $('#pattern'),
  10. $works = $('#imagelist > li'),
  11. $bgimage = $('#background > img'),
  12. Menu = (function(){
  13. var init = function() {
  14. preloadImages();
  15. initPlugins();
  16. initPattern();
  17. initEventsHandler();
  18. },
  19. //preloads the images for the work area (data-bgimg attr)
  20. preloadImages = function() {
  21. $works.each(function(i) {
  22. $('<img/>').attr('src' , $(this).children('img').data('bgimg'));
  23. });
  24. },
  25. //initialise the jScollPane (scroll plugin)
  26. initPlugins = function() {
  27. $mbContentInnerItems.jScrollPane({
  28. verticalDragMinHeight: 40,
  29. verticalDragMaxHeight: 40
  30. });
  31. },
  32. /*
  33. draws 16 boxes on a specific area of the page.
  34. we randomly calculate the top, left, and rotation angle for each one of them
  35. */
  36. initPattern = function() {
  37. for(var i = 0; i < 16 ; ++i) {
  38. //random opacity, top, left and angle
  39. var o = 0.1,
  40. t = Math.floor(Math.random()*196) + 5, // between 5 and 200
  41. l = Math.floor(Math.random()*696) + 5, // between 5 and 700
  42. a = Math.floor(Math.random()*101) - 50; // between -50 and 50
  43. $el = $('<div>').css({
  44. opacity : o,
  45. top : t + 'px',
  46. left : l + 'px'
  47. });
  48. if (!$.browser.msie)
  49. $el.transform({'rotate' : a + 'deg'});
  50. $el.appendTo($mbPattern);
  51. }
  52. $mbPattern.children().draggable(); //just for fun
  53. },
  54. /*
  55. when the User closes a content item, we move the boxes back to the original place,
  56. with new random values for top, left and angle though
  57. */
  58. disperse = function() {
  59. $mbPattern.children().each(function(i) {
  60. //random opacity, top, left and angle
  61. var o = 0.1,
  62. t = Math.floor(Math.random()*196) + 5, // between 5 and 200
  63. l = Math.floor(Math.random()*696) + 5, // between 5 and 700
  64. a = Math.floor(Math.random()*101) - 50; // between -50 and 50
  65. $el = $(this),
  66. param = {
  67. width : '50px',
  68. height : '50px',
  69. opacity : o,
  70. top : t + 'px',
  71. left : l + 'px'
  72. };
  73. if (!$.browser.msie)
  74. param.rotate = a + 'deg';
  75. $el.animate(param, 1000, 'easeOutExpo');
  76. });
  77. },
  78. initEventsHandler = function() {
  79. /*
  80. click a link in the menu
  81. */
  82. $menuItems.bind('click', function(e) {
  83. var $this = $(this),
  84. pos = $this.index(),
  85. speed = $this.data('speed'),
  86. easing = $this.data('easing');
  87. //if an item is not yet shown
  88. if(!$menu.data('open')){
  89. //if current animating return
  90. if($menu.data('moving')) return false;
  91. $menu.data('moving', true);
  92. $.when(openItem(pos, speed, easing)).done(function(){
  93. $menu.data({
  94. open : true,
  95. moving : false
  96. });
  97. showContentItem(pos);
  98. $mbPattern.children().fadeOut(500);
  99. });
  100. }
  101. else
  102. showContentItem(pos);
  103. return false;
  104. });
  105. /*
  106. click close makes the boxes animate to the top of the page
  107. */
  108. $mbClose.bind('click', function(e) {
  109. $menu.data('open', false);
  110. /*
  111. if we would want to show the default image when we close:
  112. changeBGImage('images/default.jpg');
  113. */
  114. $mbPattern.children().fadeIn(500, function() {
  115. $mbContentItems.hide();
  116. $mbWrapper.hide();
  117. });
  118. disperse();
  119. return false;
  120. });
  121. /*
  122. click an image from "Works" content item,
  123. displays the image on the background
  124. */
  125. $works.bind('click', function(e) {
  126. var source = $(this).children('img').data('bgimg');
  127. changeBGImage(source);
  128. return false;
  129. });
  130. },
  131. /*
  132. changes the background image
  133. */
  134. changeBGImage = function(img) {
  135. //if its the current one return
  136. if($bgimage.attr('src') === img || $bgimage.siblings('img').length > 0)
  137. return false;
  138. var $itemImage = $('<img src="'+img+'" alt="Background" class="bgimage" style="display:none;"/>');
  139. $itemImage.insertBefore($bgimage);
  140. $bgimage.fadeOut(1000, function() {
  141. $(this).remove();
  142. $bgimage = $itemImage;
  143. });
  144. $itemImage.fadeIn(1000);
  145. },
  146. /*
  147. This shows a content item when there is already one shown:
  148. */
  149. showContentItem = function(pos) {
  150. $mbContentItems.hide();
  151. $mbWrapper.show();
  152. $mbContentItems.eq(pos).show().children('.content_inner').jScrollPane();
  153. },
  154. /*
  155. moves the boxes from the top to the center of the page,
  156. and shows the respective content item
  157. */
  158. openItem = function(pos, speed, easing) {
  159. return $.Deferred(
  160. function(dfd) {
  161. $mbPattern.children().each(function(i) {
  162. var $el = $(this),
  163. param = {
  164. width : '100px',
  165. height : '100px',
  166. top : 154 + 100 * Math.floor(i/4),
  167. left : 200 + 100 * (i%4),
  168. opacity : 1
  169. };
  170. if (!$.browser.msie)
  171. param.rotate = '0deg';
  172. $el.animate(param, speed, easing, dfd.resolve);
  173. });
  174. }
  175. ).promise();
  176. };
  177. return {
  178. init : init
  179. };
  180. })();
  181. /*
  182. call the init method of Menu
  183. */
  184. Menu.init();
  185. });
  186. </script>