jquery.vegas.js 13 KB

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