jquery.videoBG.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @preserve Copyright 2011 Syd Lawrence ( www.sydlawrence.com ).
  3. * Version: 0.2
  4. *
  5. * Licensed under MIT and GPLv2.
  6. *
  7. * Usage: $('body').videoBG(options);
  8. *
  9. */
  10. (function( $ ){
  11. $.fn.videoBG = function( selector, options ) {
  12. if (options === undefined) {
  13. options = {};
  14. }
  15. if (typeof selector === "object") {
  16. options = $.extend({}, $.fn.videoBG.defaults, selector);
  17. }
  18. else if (!selector) {
  19. options = $.fn.videoBG.defaults;
  20. }
  21. else {
  22. return $(selector).videoBG(options);
  23. }
  24. var container = $(this);
  25. // check if elements available otherwise it will cause issues
  26. if (!container.length) {
  27. return;
  28. }
  29. // container to be at least relative
  30. if (container.css('position') == 'static' || !container.css('position')) {
  31. container.css('position','relative');
  32. }
  33. // we need a width
  34. if (options.width === 0) {
  35. options.width = container.width();
  36. }
  37. // we need a height
  38. if (options.height === 0) {
  39. options.height = container.height();
  40. }
  41. // get the wrapper
  42. var wrap = $.fn.videoBG.wrapper();
  43. wrap.height(options.height)
  44. .width(options.width);
  45. // if is a text replacement
  46. if (options.textReplacement) {
  47. // force sizes
  48. options.scale = true;
  49. // set sizes and forcing text out
  50. container.width(options.width)
  51. .height(options.height)
  52. .css('text-indent','-9999px');
  53. }
  54. else {
  55. // set the wrapper above the video
  56. wrap.css('z-index',options.zIndex+1);
  57. }
  58. // move the contents into the wrapper
  59. wrap.html(container.clone(true));
  60. // get the video
  61. var video = $.fn.videoBG.video(options);
  62. // if we are forcing width / height
  63. if (options.scale) {
  64. // overlay wrapper
  65. wrap.height(options.height)
  66. .width(options.width);
  67. // video
  68. video.height(options.height)
  69. .width(options.width);
  70. }
  71. // add it all to the container
  72. container.html(wrap);
  73. container.append(video);
  74. return video.find("video")[0];
  75. };
  76. // set to fullscreen
  77. $.fn.videoBG.setFullscreen = function($el) {
  78. var windowWidth = $(window).width(),
  79. windowHeight = $(window).height();
  80. $el.css('min-height',0).css('min-width',0);
  81. $el.parent().width(windowWidth).height(windowHeight);
  82. // if by width
  83. var shift = 0;
  84. if (windowWidth / windowHeight > $el.aspectRatio) {
  85. $el.width(windowWidth).height('auto');
  86. // shift the element up
  87. var height = $el.height();
  88. shift = (height - windowHeight) / 2;
  89. if (shift < 0) {
  90. shift = 0;
  91. }
  92. $el.css("top",-shift);
  93. } else {
  94. $el.width('auto').height(windowHeight);
  95. // shift the element left
  96. var width = $el.width();
  97. shift = (width - windowWidth) / 2;
  98. if (shift < 0) {
  99. shift = 0;
  100. }
  101. $el.css("left",-shift);
  102. // this is a hack mainly due to the iphone
  103. if (shift === 0) {
  104. var t = setTimeout(function() {
  105. $.fn.videoBG.setFullscreen($el);
  106. },500);
  107. }
  108. }
  109. $('body > .videoBG_wrapper').width(windowWidth).height(windowHeight);
  110. };
  111. // get the formatted video element
  112. $.fn.videoBG.video = function(options) {
  113. $('html, body').scrollTop(-1);
  114. // video container
  115. var $div = $('<div/>');
  116. $div.addClass('videoBG')
  117. .css('position',options.position)
  118. .css('z-index',options.zIndex)
  119. .css('top',0)
  120. .css('left',0)
  121. .css('height',options.height)
  122. .css('width',options.width)
  123. .css('opacity',options.opacity)
  124. .css('overflow','hidden');
  125. // video element
  126. var $video = $('<video/>');
  127. $video.css('position','absolute')
  128. .css('z-index',options.zIndex)
  129. .attr('poster',options.poster)
  130. .css('top',0)
  131. .css('left',0)
  132. .css('min-width','100%')
  133. .css('min-height','100%');
  134. if (options.autoplay) {
  135. $video.attr('autoplay',options.autoplay);
  136. }
  137. // if fullscreen
  138. if (options.fullscreen) {
  139. $video.bind('canplay',function() {
  140. // set the aspect ratio
  141. $video.aspectRatio = $video.width() / $video.height();
  142. $.fn.videoBG.setFullscreen($video);
  143. });
  144. // listen out for screenresize
  145. var resizeTimeout;
  146. $(window).resize(function() {
  147. clearTimeout(resizeTimeout);
  148. resizeTimeout = setTimeout(function() {
  149. $.fn.videoBG.setFullscreen($video);
  150. },100);
  151. });
  152. $.fn.videoBG.setFullscreen($video);
  153. }
  154. // video standard element
  155. var v = $video[0];
  156. // if meant to loop
  157. if (options.loop) {
  158. loops_left = options.loop;
  159. // cant use the loop attribute as firefox doesnt support it
  160. $video.bind('ended', function(){
  161. // if we have some loops to throw
  162. if (loops_left) {
  163. // replay that bad boy
  164. v.play();
  165. }
  166. // if not forever
  167. if (loops_left !== true) {
  168. // one less loop
  169. loops_left--;
  170. }
  171. });
  172. }
  173. // when can play, play
  174. $video.bind('canplay', function(){
  175. if (options.autoplay) {
  176. // replay that bad boy
  177. v.play();
  178. }
  179. });
  180. // if supports video
  181. if ($.fn.videoBG.supportsVideo()) {
  182. // supports webm
  183. if ($.fn.videoBG.supportType('webm')){
  184. // play webm
  185. $video.attr('src',options.webm);
  186. }
  187. // supports mp4
  188. else if ($.fn.videoBG.supportType('mp4')) {
  189. // play mp4
  190. $video.attr('src',options.mp4);
  191. }
  192. // throw ogv at it then
  193. else {
  194. // play ogv
  195. $video.attr('src',options.ogv);
  196. }
  197. }
  198. // image for those that dont support the video
  199. var $img = $('<img/>');
  200. $img.attr('src',options.poster)
  201. .css('position','absolute')
  202. .css('z-index',options.zIndex)
  203. .css('top',0)
  204. .css('left',0)
  205. .css('min-width','100%')
  206. .css('min-height','100%');
  207. // add the image to the video
  208. // if suuports video
  209. if ($.fn.videoBG.supportsVideo()) {
  210. // add the video to the wrapper
  211. $div.html($video);
  212. }
  213. // nope - whoa old skool
  214. else {
  215. // add the image instead
  216. $div.html($img);
  217. }
  218. // if text replacement
  219. if (options.textReplacement) {
  220. // force the heights and widths
  221. $div.css('min-height',1).css('min-width',1);
  222. $video.css('min-height',1).css('min-width',1);
  223. $img.css('min-height',1).css('min-width',1);
  224. $div.height(options.height).width(options.width);
  225. $video.height(options.height).width(options.width);
  226. $img.height(options.height).width(options.width);
  227. }
  228. if ($.fn.videoBG.supportsVideo()) {
  229. v.play();
  230. }
  231. return $div;
  232. };
  233. // check if suuports video
  234. $.fn.videoBG.supportsVideo = function() {
  235. return (document.createElement('video').canPlayType);
  236. };
  237. // check which type is supported
  238. $.fn.videoBG.supportType = function(str) {
  239. // if not at all supported
  240. if (!$.fn.videoBG.supportsVideo()) {
  241. return false;
  242. }
  243. // create video
  244. var v = document.createElement('video');
  245. // check which?
  246. switch (str) {
  247. case 'webm' :
  248. return (v.canPlayType('video/webm; codecs="vp8, vorbis"'));
  249. case 'mp4' :
  250. return (v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"'));
  251. case 'ogv' :
  252. return (v.canPlayType('video/ogg; codecs="theora, vorbis"'));
  253. }
  254. // nope
  255. return false;
  256. };
  257. // get the overlay wrapper
  258. $.fn.videoBG.wrapper = function() {
  259. var $wrap = $('<div/>');
  260. $wrap.addClass('videoBG_wrapper')
  261. .css('position','absolute')
  262. .css('top',0)
  263. .css('left',0);
  264. return $wrap;
  265. };
  266. // these are the defaults
  267. $.fn.videoBG.defaults = {
  268. mp4:'',
  269. ogv:'',
  270. webm:'',
  271. poster:'',
  272. autoplay:true,
  273. loop:true,
  274. scale:false,
  275. position:"absolute",
  276. opacity:1,
  277. textReplacement:false,
  278. zIndex:0,
  279. width:0,
  280. height:0,
  281. fullscreen:false,
  282. imgFallback:true
  283. };
  284. })( jQuery );