jquery.vegas.js 13 KB

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