jquery.vegas.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // ----------------------------------------------------------------------------
  2. // Vegas - jQuery plugin
  3. // Add awesome fullscreen backgrounds to your webpages.
  4. // v 1.0 beta
  5. // Dual licensed under the MIT and GPL licenses.
  6. // http://vegas.jaysalvat.com/
  7. // ----------------------------------------------------------------------------
  8. // Copyright (C) 2011 Jay Salvat
  9. // http://jaysalvat.com/
  10. // ----------------------------------------------------------------------------
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files ( the "Software" ), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. // ----------------------------------------------------------------------------
  29. ( function( $ ){
  30. var $background = $( '<img />' ).addClass( 'vegas-background' ),
  31. $overlay = $( '<div />' ).addClass( 'vegas-overlay' ),
  32. $loading = $( '<div />' ).addClass( 'vegas-loading' ),
  33. $current,
  34. timer,
  35. step = 0,
  36. methods = {
  37. // Init plugin
  38. init : function( settings ) {
  39. var options = {
  40. src: getBackground(),
  41. align: 'center',
  42. valign: 'center',
  43. fade: 0,
  44. load: function() {},
  45. complete: function() {}
  46. };
  47. $.extend( true, options, settings );
  48. loading();
  49. $new = $background.clone();
  50. $new.css( {
  51. 'position': 'fixed',
  52. 'left': '0px',
  53. 'top': '0px'
  54. })
  55. .load( function() {
  56. $( window ).bind( 'resize.vegas', function( e ) {
  57. resize( $new, options );
  58. });
  59. if ( $current ) {
  60. $current.stop();
  61. $new.hide()
  62. .insertAfter( $current )
  63. .fadeIn( options.fade, function() {
  64. $('.vegas-background')
  65. .not(this)
  66. .remove();
  67. $( 'body' ).trigger( 'backgroundcomplete', [ this ] );
  68. options.complete.apply( $new );
  69. });
  70. } else {
  71. $new.hide()
  72. .prependTo( 'body' )
  73. .fadeIn( options.fade, function() {
  74. $( 'body' ).trigger( 'backgroundcomplete', [ this ] );
  75. options.complete.apply( this );
  76. });
  77. }
  78. $current = $new;
  79. resize( $current, options );
  80. loaded();
  81. $( 'body' ).trigger( 'backgroundload', [ $current.get(0) ] );
  82. options.load.apply( $current.get(0) );
  83. })
  84. .attr( 'src', options.src );
  85. return $.vegas;
  86. },
  87. // Destroy background
  88. destroy: function( what ) {
  89. if ( !what || what == 'background') {
  90. $( '.vegas-background, .vegas-loading' ).remove();
  91. $( window ).unbind( 'resize.vegas' );
  92. $current = null;
  93. }
  94. if ( !what || what == 'overlay') {
  95. $( '.vegas-overlay' ).remove();
  96. }
  97. return $.vegas;
  98. },
  99. overlay:function( settings ) {
  100. var options = {
  101. src: null,
  102. opacity: null
  103. };
  104. $.extend( options, settings );
  105. $overlay.remove();
  106. $overlay
  107. .css( {
  108. 'margin': '0',
  109. 'padding': '0',
  110. 'position': 'fixed',
  111. 'left': '0px',
  112. 'top': '0px',
  113. 'width': '100%',
  114. 'height': '100%'
  115. });
  116. if ( options.src ) {
  117. $overlay.css( 'backgroundImage', 'url(' + options.src + ')' );
  118. }
  119. if ( options.opacity ) {
  120. $overlay.css( 'opacity', options.opacity );
  121. }
  122. $overlay.prependTo( 'body' );
  123. return $.vegas;
  124. },
  125. // Start slideshow
  126. slideshow: function( settings ) {
  127. var options = {
  128. delay: 5000,
  129. backgrounds: []
  130. };
  131. $.extend( options, settings );
  132. clearInterval( timer );
  133. var doSlideshow = function() {
  134. $.vegas( options.backgrounds[ step++ ] );
  135. if ( step >= options.backgrounds.length ) {
  136. step = 0;
  137. }
  138. }
  139. doSlideshow();
  140. timer = setInterval( doSlideshow, options.delay );
  141. return $.vegas;
  142. },
  143. // Stop slideshow
  144. stop: function( settings ) {
  145. step = 0;
  146. clearInterval( timer );
  147. return $.vegas;
  148. },
  149. // Pause SlideShow
  150. pause: function( settings ) {
  151. clearInterval( timer );
  152. return $.vegas;
  153. }
  154. }
  155. // Resize the background
  156. function resize( $img, settings ) {
  157. var options = {
  158. align: 'center',
  159. valign: 'center'
  160. }
  161. $.extend( options, settings );
  162. var ww = $( window ).width(),
  163. wh = $( window ).height(),
  164. iw = $img.width(),
  165. ih = $img.height(),
  166. rw = wh / ww,
  167. ri = ih / iw,
  168. newWidth, newHeight,
  169. newLeft, newTop,
  170. properties;
  171. if ( rw > ri ) {
  172. newWidth = wh / ri;
  173. newHeight = wh;
  174. } else {
  175. newWidth = ww;
  176. newHeight = ww * ri;
  177. }
  178. properties = {
  179. 'width': newWidth + 'px',
  180. 'height': newHeight + 'px',
  181. 'left': ( ww - newWidth ) / 2 + 'px',
  182. 'top': ( wh - newHeight ) / 2 + 'px'
  183. }
  184. if ( options.valign == 'top' ) {
  185. properties[ 'top' ] = 0;
  186. properties[ 'bottom' ] = 'auto';
  187. }
  188. if ( options.valign == 'bottom' ) {
  189. properties[ 'top' ] = 'auto';
  190. properties[ 'bottom' ] = 0;
  191. }
  192. if ( options.align == 'left' ) {
  193. properties[ 'left' ] = 0;
  194. properties[ 'right' ] = 'auto';
  195. }
  196. if ( options.align == 'right' ) {
  197. properties[ 'left' ] = 'auto';
  198. properties[ 'right' ] = 0;
  199. }
  200. $img.css( properties );
  201. }
  202. function loading() {
  203. $loading.prependTo( 'body' ).fadeIn();
  204. }
  205. function loaded() {
  206. $loading.fadeOut( 'fast', function() {
  207. $( this ).remove();
  208. });
  209. }
  210. // Get the background image from the body
  211. function getBackground() {
  212. if ( $( 'body' ).css( 'backgroundImage' ) ) {
  213. return $( 'body' ).css( 'backgroundImage' ).replace( /url\("?(.*?)"?\)/i, '$1' );
  214. }
  215. }
  216. // The plugin
  217. $.vegas = function( method ) {
  218. if ( methods[ method ] ) {
  219. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
  220. } else if ( typeof method === 'object' || !method ) {
  221. return methods.init.apply( this, arguments );
  222. } else {
  223. $.error( 'Method ' + method + ' does not exist' );
  224. }
  225. };
  226. })( jQuery );