jquery.vegas.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // ----------------------------------------------------------------------------
  2. // Vegas - jQuery plugin
  3. // Add awesome fullscreen backgrounds to your webpages.
  4. // v 1.1 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. paused = null,
  35. backgrounds = [],
  36. step = 0,
  37. delay = 5000,
  38. timer,
  39. methods = {
  40. // Init plugin
  41. init : function( settings ) {
  42. var options = {
  43. src: getBackground(),
  44. align: 'center',
  45. valign: 'center',
  46. fade: 0,
  47. loading: true,
  48. load: function() {},
  49. complete: function() {}
  50. }
  51. $.extend( options, $.vegas.defaults.background, settings );
  52. if ( options.loading ) {
  53. loading();
  54. }
  55. var $new = $background.clone();
  56. $new.css( {
  57. 'position': 'fixed',
  58. 'left': '0px',
  59. 'top': '0px'
  60. })
  61. .load( function() {
  62. if ( $new == $current ) {
  63. return;
  64. }
  65. $( window ).bind( 'resize.vegas', function( e ) {
  66. resize( $new, options );
  67. });
  68. if ( $current.is( 'img' ) ) {
  69. $current.stop();
  70. $new.hide()
  71. .insertAfter( $current )
  72. .fadeIn( options.fade, function() {
  73. $('.vegas-background')
  74. .not(this)
  75. .remove();
  76. $( 'body' ).trigger( 'vegascomplete', [ this, step - 1 ] );
  77. options.complete.apply( $new, [ step - 1 ] );
  78. });
  79. } else {
  80. $new.hide()
  81. .prependTo( 'body' )
  82. .fadeIn( options.fade, function() {
  83. $( 'body' ).trigger( 'vegascomplete', [ this, step - 1 ] );
  84. options.complete.apply( this, [ step - 1 ] );
  85. });
  86. }
  87. $current = $new;
  88. resize( $current, options );
  89. if ( options.loading ) {
  90. loaded();
  91. }
  92. $( 'body' ).trigger( 'vegasload', [ $current.get(0), step - 1 ] );
  93. options.load.apply( $current.get(0), [ step - 1 ] );
  94. if ( step ) {
  95. $( 'body' ).trigger( 'vegaswalk', [ $current.get(0), step - 1 ] );
  96. options.walk.apply( $current.get(0), [ step - 1 ] );
  97. }
  98. })
  99. .attr( 'src', options.src );
  100. return $.vegas;
  101. },
  102. // Destroy background and/or overlay
  103. destroy: function( what ) {
  104. if ( !what || what == 'background') {
  105. $( '.vegas-background, .vegas-loading' ).remove();
  106. $( window ).unbind( 'resize.vegas' );
  107. $current = null;
  108. }
  109. if ( what == 'overlay') {
  110. $( '.vegas-overlay' ).remove();
  111. }
  112. return $.vegas;
  113. },
  114. // Display the pattern overlay
  115. overlay: function( settings ) {
  116. var options = {
  117. src: null,
  118. opacity: null
  119. };
  120. $.extend( options, $.vegas.defaults.overlay, settings );
  121. $overlay.remove();
  122. $overlay
  123. .css( {
  124. 'margin': '0',
  125. 'padding': '0',
  126. 'position': 'fixed',
  127. 'left': '0px',
  128. 'top': '0px',
  129. 'width': '100%',
  130. 'height': '100%'
  131. });
  132. if ( options.src ) {
  133. $overlay.css( 'backgroundImage', 'url(' + options.src + ')' );
  134. }
  135. if ( options.opacity ) {
  136. $overlay.css( 'opacity', options.opacity );
  137. }
  138. $overlay.prependTo( 'body' );
  139. return $.vegas;
  140. },
  141. // Start/restart slideshow
  142. slideshow: function( settings, keepPause ) {
  143. var options = {
  144. step: step,
  145. delay: delay,
  146. preload: false,
  147. backgrounds: backgrounds,
  148. walk: function() {}
  149. };
  150. $.extend( options, $.vegas.defaults.slideshow, settings );
  151. if ( options.backgrounds != backgrounds ) {
  152. if ( !settings.step ) {
  153. options.step = 0;
  154. }
  155. if ( options.preload ) {
  156. $.vegas( 'preload', options.backgrounds )
  157. }
  158. }
  159. backgrounds = options.backgrounds;
  160. delay = options.delay;
  161. step = options.step;
  162. clearInterval( timer );
  163. if ( !backgrounds.length ) {
  164. return $.vegas;
  165. }
  166. var doSlideshow = function() {
  167. if ( step < 0 ) {
  168. step = backgrounds.length - 1;
  169. }
  170. if ( step >= backgrounds.length || !backgrounds[ step - 1 ] ) {
  171. step = 0;
  172. }
  173. var settings = backgrounds[ step++ ];
  174. settings.walk = options.walk;
  175. $.vegas( settings );
  176. }
  177. doSlideshow();
  178. if ( !keepPause ) {
  179. paused = false;
  180. $( 'body' ).trigger( 'vegasstart', [ $current.get(0), step - 1 ] );
  181. }
  182. if ( !paused ) {
  183. timer = setInterval( doSlideshow, options.delay );
  184. }
  185. return $.vegas;
  186. },
  187. // Jump to the next background in the current slideshow
  188. next: function() {
  189. var from = step;
  190. if ( step ) {
  191. $.vegas( 'slideshow', { step: step }, true );
  192. $( 'body' ).trigger( 'vegasnext', [ $current.get(0), step - 1, from - 1 ] );
  193. }
  194. return $.vegas;
  195. },
  196. // Jump to the previous background in the current slideshow
  197. previous: function() {
  198. var from = step;
  199. if ( step ) {
  200. $.vegas( 'slideshow', { step: step - 2 }, true );
  201. $( 'body' ).trigger( 'vegasprevious', [ $current.get(0), step - 1, from - 1 ] );
  202. }
  203. return $.vegas;
  204. },
  205. // Jump to a specific background in the current slideshow
  206. jump: function( s ) {
  207. var from = step;
  208. if ( step ) {
  209. $.vegas( 'slideshow', { step: s }, true );
  210. $( 'body' ).trigger( 'vegasjump', [ $current.get(0), step - 1, from - 1 ] );
  211. }
  212. return $.vegas;
  213. },
  214. // Stop slideshow
  215. stop: function() {
  216. var from = step;
  217. step = 0;
  218. paused = null;
  219. clearInterval( timer );
  220. $( 'body' ).trigger( 'vegasstop', [ $current.get(0), from - 1 ] );
  221. return $.vegas;
  222. },
  223. // Pause slideShow
  224. pause: function() {
  225. paused = true;
  226. clearInterval( timer );
  227. $( 'body' ).trigger( 'vegaspause', [ $current.get(0), step - 1 ] );
  228. return $.vegas;
  229. },
  230. // Get some useful values or objects
  231. get: function( what ) {
  232. if ( what == null || what == 'background' ) {
  233. return $current.get(0);
  234. }
  235. if ( what == 'overlay' ) {
  236. return $overlay.get(0);
  237. }
  238. if ( what == 'step' ) {
  239. return step - 1;
  240. }
  241. if ( what == 'paused' ) {
  242. return paused;
  243. }
  244. },
  245. // Preload an array of backgrounds
  246. preload: function( backgrounds ) {
  247. for( var i in backgrounds ) {
  248. if ( backgrounds[ i ].src ) {
  249. $('<img src="' + backgrounds[ i ].src + '">');
  250. }
  251. }
  252. return $.vegas;
  253. }
  254. }
  255. // Resize the background
  256. function resize( $img, settings ) {
  257. var options = {
  258. align: 'center',
  259. valign: 'center'
  260. }
  261. $.extend( options, settings );
  262. var ww = $( window ).width(),
  263. wh = $( window ).height(),
  264. iw = $img.width(),
  265. ih = $img.height(),
  266. rw = wh / ww,
  267. ri = ih / iw,
  268. newWidth, newHeight,
  269. newLeft, newTop,
  270. properties;
  271. if ( rw > ri ) {
  272. newWidth = wh / ri;
  273. newHeight = wh;
  274. } else {
  275. newWidth = ww;
  276. newHeight = ww * ri;
  277. }
  278. properties = {
  279. 'width': newWidth + 'px',
  280. 'height': newHeight + 'px',
  281. 'top': 'auto',
  282. 'bottom': 'auto',
  283. 'left': 'auto',
  284. 'right': 'auto'
  285. }
  286. if ( !isNaN( parseInt( options.valign ) ) ) {
  287. properties[ 'top' ] = ( 0 - ( newHeight - wh ) / 100 * parseInt( options.valign ) ) + 'px';
  288. } else if ( options.valign == 'top' ) {
  289. properties[ 'top' ] = 0;
  290. } else if ( options.valign == 'bottom' ) {
  291. properties[ 'bottom' ] = 0;
  292. } else {
  293. properties[ 'top' ] = ( wh - newHeight ) / 2;
  294. }
  295. if ( !isNaN( parseInt( options.align ) ) ) {
  296. properties[ 'left' ] = ( 0 - ( newWidth - ww ) / 100 * parseInt( options.align ) ) + 'px';
  297. } else if ( options.align == 'left' ) {
  298. properties[ 'left' ] = 0;
  299. } else if ( options.align == 'right' ) {
  300. properties[ 'right' ] = 0;
  301. } else {
  302. properties[ 'left' ] = ( ww - newWidth ) / 2 ;
  303. }
  304. $img.css( properties );
  305. }
  306. // Display the loading indicator
  307. function loading() {
  308. $loading.prependTo( 'body' ).fadeIn();
  309. }
  310. // Hide the loading indicator
  311. function loaded() {
  312. $loading.fadeOut( 'fast', function() {
  313. $( this ).remove();
  314. });
  315. }
  316. // Get the background image from the body
  317. function getBackground() {
  318. if ( $( 'body' ).css( 'backgroundImage' ) ) {
  319. return $( 'body' ).css( 'backgroundImage' ).replace( /url\("?(.*?)"?\)/i, '$1' );
  320. }
  321. }
  322. // The plugin
  323. $.vegas = function( method ) {
  324. if ( methods[ method ] ) {
  325. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
  326. } else if ( typeof method === 'object' || !method ) {
  327. return methods.init.apply( this, arguments );
  328. } else {
  329. $.error( 'Method ' + method + ' does not exist' );
  330. }
  331. };
  332. // Global parameters
  333. $.vegas.defaults = {
  334. background: {
  335. // src: string
  336. // align: string/int
  337. // valign: string/int
  338. // fade: int
  339. // loading bool
  340. // load: function
  341. // complete: function
  342. },
  343. slideshow: {
  344. // step: int
  345. // delay: int
  346. // backgrounds: array
  347. // preload: bool
  348. // walk: function
  349. },
  350. overlay: {
  351. // src: string
  352. // opacity: float
  353. }
  354. }
  355. })( jQuery );