【发布时间】:2014-09-13 12:12:14
【问题描述】:
我已经做了几个带有视频背景的项目,它们在加载页面时自动播放,并且仅在设备不是移动浏览器时加载(那些不接受 HTML5 视频的自动播放属性,在这种情况下只是一个图像已加载)。
为了检测这一点,我使用了 jQuery,如下所示:
var isMobile = {
Android: function() { return navigator.userAgent.match(/Android/i); },
BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); },
iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); },
Opera: function() { return navigator.userAgent.match(/Opera Mini/i); },
Windows: function() { return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i); },
any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); }};
$(document).ready(function(){
if( isMobile.any() ) {
$('.backgroundVideoC').append("<img alt= src='media/germannoload.jpg'/>");}
else{
$('.backgroundVideoC').append("<video preload='auto' muted class='indexVideo' id='indexVideo'><source src='media/interactvid.webm' type='video/webm'><source src='media/interactvid.mp4' type='video/mp4'></video>");
$('#indexVideo').get(0).pause();}
这在大多数情况下都可以正常工作,但我发现某些黑莓设备仍会加载视频,即使它们是移动浏览器且不接受自动播放。
是否推荐切换到 Modernizr 来检测是否自动播放可用,而不是通过 jQuery 进行检查? jQuery 方法与 Modernizr 自动播放检测相比有什么优势吗?
【问题讨论】:
标签: jquery html html5-video modernizr