videobg.js 7.4 KB

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