jquery.vegas.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // ----------------------------------------------------------------------------
  2. // Vegas - jQuery plugin
  3. // Add awesome fullscreen backgrounds to your webpages.
  4. // v 1.x
  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. 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. .imagesLoadedForVegas( function() {
  63. if ( $new == $current ) {
  64. return;
  65. }
  66. $( window ).bind( '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 = null;
  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 ( settings.fade > options.delay ) {
  181. settings.fade = options.delay;
  182. }
  183. $.vegas( settings );
  184. }
  185. doSlideshow();
  186. if ( !keepPause ) {
  187. paused = false;
  188. $( 'body' ).trigger( 'vegasstart', [ $current.get(0), step - 1 ] );
  189. }
  190. if ( !paused ) {
  191. timer = setInterval( doSlideshow, options.delay );
  192. }
  193. return $.vegas;
  194. },
  195. // Jump to the next background in the current slideshow
  196. next: function() {
  197. var from = step;
  198. if ( step ) {
  199. $.vegas( 'slideshow', { step: step }, true );
  200. $( 'body' ).trigger( 'vegasnext', [ $current.get(0), step - 1, from - 1 ] );
  201. }
  202. return $.vegas;
  203. },
  204. // Jump to the previous background in the current slideshow
  205. previous: function() {
  206. var from = step;
  207. if ( step ) {
  208. $.vegas( 'slideshow', { step: step - 2 }, true );
  209. $( 'body' ).trigger( 'vegasprevious', [ $current.get(0), step - 1, from - 1 ] );
  210. }
  211. return $.vegas;
  212. },
  213. // Jump to a specific background in the current slideshow
  214. jump: function( s ) {
  215. var from = step;
  216. if ( step ) {
  217. $.vegas( 'slideshow', { step: s }, true );
  218. $( 'body' ).trigger( 'vegasjump', [ $current.get(0), step - 1, from - 1 ] );
  219. }
  220. return $.vegas;
  221. },
  222. // Stop slideshow
  223. stop: function() {
  224. var from = step;
  225. step = 0;
  226. paused = null;
  227. clearInterval( timer );
  228. $( 'body' ).trigger( 'vegasstop', [ $current.get(0), from - 1 ] );
  229. return $.vegas;
  230. },
  231. // Pause slideShow
  232. pause: function() {
  233. paused = true;
  234. clearInterval( timer );
  235. $( 'body' ).trigger( 'vegaspause', [ $current.get(0), step - 1 ] );
  236. return $.vegas;
  237. },
  238. // Get some useful values or objects
  239. get: function( what ) {
  240. if ( what == null || what == 'background' ) {
  241. return $current.get(0);
  242. }
  243. if ( what == 'overlay' ) {
  244. return $overlay.get(0);
  245. }
  246. if ( what == 'step' ) {
  247. return step - 1;
  248. }
  249. if ( what == 'paused' ) {
  250. return paused;
  251. }
  252. },
  253. // Preload an array of backgrounds
  254. preload: function( backgrounds ) {
  255. var cache = [];
  256. for( var i in backgrounds ) {
  257. if ( backgrounds[ i ].src ) {
  258. var cacheImage = document.createElement('img');
  259. cacheImage.src = backgrounds[ i ].src;
  260. cache.push(cacheImage);
  261. }
  262. }
  263. return $.vegas;
  264. }
  265. }
  266. // Resize the background
  267. function resize( $img, settings ) {
  268. var options = {
  269. align: 'center',
  270. valign: 'center'
  271. }
  272. $.extend( options, settings );
  273. var ww = $( window ).width(),
  274. wh = $( window ).height(),
  275. iw = $img.width(),
  276. ih = $img.height(),
  277. rw = wh / ww,
  278. ri = ih / iw,
  279. newWidth, newHeight,
  280. newLeft, newTop,
  281. properties;
  282. if ( rw > ri ) {
  283. newWidth = wh / ri;
  284. newHeight = wh;
  285. } else {
  286. newWidth = ww;
  287. newHeight = ww * ri;
  288. }
  289. properties = {
  290. 'width': newWidth + 'px',
  291. 'height': newHeight + 'px',
  292. 'top': 'auto',
  293. 'bottom': 'auto',
  294. 'left': 'auto',
  295. 'right': 'auto'
  296. }
  297. if ( !isNaN( parseInt( options.valign ) ) ) {
  298. properties[ 'top' ] = ( 0 - ( newHeight - wh ) / 100 * parseInt( options.valign ) ) + 'px';
  299. } else if ( options.valign == 'top' ) {
  300. properties[ 'top' ] = 0;
  301. } else if ( options.valign == 'bottom' ) {
  302. properties[ 'bottom' ] = 0;
  303. } else {
  304. properties[ 'top' ] = ( wh - newHeight ) / 2;
  305. }
  306. if ( !isNaN( parseInt( options.align ) ) ) {
  307. properties[ 'left' ] = ( 0 - ( newWidth - ww ) / 100 * parseInt( options.align ) ) + 'px';
  308. } else if ( options.align == 'left' ) {
  309. properties[ 'left' ] = 0;
  310. } else if ( options.align == 'right' ) {
  311. properties[ 'right' ] = 0;
  312. } else {
  313. properties[ 'left' ] = ( ww - newWidth ) / 2 ;
  314. }
  315. $img.css( properties );
  316. }
  317. // Display the loading indicator
  318. function loading() {
  319. $loading.prependTo( 'body' ).fadeIn();
  320. }
  321. // Hide the loading indicator
  322. function loaded() {
  323. $loading.fadeOut( 'fast', function() {
  324. $( this ).remove();
  325. });
  326. }
  327. // Get the background image from the body
  328. function getBackground() {
  329. if ( $( 'body' ).css( 'backgroundImage' ) ) {
  330. return $( 'body' ).css( 'backgroundImage' ).replace( /url\("?(.*?)"?\)/i, '$1' );
  331. }
  332. }
  333. // The plugin
  334. $.vegas = function( method ) {
  335. if ( methods[ method ] ) {
  336. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
  337. } else if ( typeof method === 'object' || !method ) {
  338. return methods.init.apply( this, arguments );
  339. } else {
  340. $.error( 'Method ' + method + ' does not exist' );
  341. }
  342. };
  343. // Global parameters
  344. $.vegas.defaults = {
  345. background: {
  346. // src: string
  347. // align: string/int
  348. // valign: string/int
  349. // fade: int
  350. // loading bool
  351. // load: function
  352. // complete: function
  353. },
  354. slideshow: {
  355. // step: int
  356. // delay: int
  357. // backgrounds: array
  358. // preload: bool
  359. // walk: function
  360. },
  361. overlay: {
  362. // src: string
  363. // opacity: float
  364. }
  365. }
  366. /*!
  367. * jQuery imagesLoaded plugin v1.0.3
  368. * http://github.com/desandro/imagesloaded
  369. *
  370. * MIT License. by Paul Irish et al.
  371. */
  372. $.fn.imagesLoadedForVegas = function( callback ) {
  373. var $this = this,
  374. $images = $this.find('img').add( $this.filter('img') ),
  375. len = $images.length,
  376. blank = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
  377. function triggerCallback() {
  378. callback.call( $this, $images );
  379. }
  380. function imgLoaded() {
  381. if ( --len <= 0 && this.src !== blank ){
  382. setTimeout( triggerCallback );
  383. $images.unbind( 'load error', imgLoaded );
  384. }
  385. }
  386. if ( !len ) {
  387. triggerCallback();
  388. }
  389. $images.bind( 'load error', imgLoaded ).each( function() {
  390. // cached images don't fire load sometimes, so we reset src.
  391. if (this.complete || this.complete === undefined){
  392. var src = this.src;
  393. // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
  394. // data uri bypasses webkit log warning (thx doug jones)
  395. this.src = blank;
  396. this.src = src;
  397. }
  398. });
  399. return $this;
  400. };
  401. })( jQuery );