jquery.vegas.js 14 KB

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