2017-02-20 68 views
0

我试图在使用jquery加载视频时尝试添加preloader。问题是,它在除Safari之外的每个浏览器都能正常工作。HTML5视频中的加载程序不能在Safari中工作

JSFIDDLE

这是代码。

$('#video_id').on('loadstart', function(event) { 
 
    $(this).addClass('loading'); 
 
}); 
 
$('#video_id').on('canplay', function(event) { 
 
    $(this).removeClass('loading'); 
 
    $(this).attr('poster', ''); 
 
});
video.loading { 
 
    background: black url(http://www.drivethrurpg.com/shared_images/ajax-loader.gif) center center no-repeat; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<video controls="" poster="http://www.drivethrurpg.com/shared_images/ajax-loader.gif"> 
 
    <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
 

 
</video>

回答

0

首先,video - 元素犯规有#video_id -id。其次,你正在使用加载器的海报,这意味着如果占位符显示在其上方,则不会看到背景图像。我在Safari浏览器中看到加载海报。

我用视频上的正确ID更新了代码。

$('#video_id').on('loadstart', function(event) { 
 
    $(this).addClass('loading'); 
 
}); 
 
$('#video_id').on('canplay', function(event) { 
 
    $(this).removeClass('loading'); 
 
    $(this).attr('poster', ''); 
 
});
video.loading { 
 
    background: black url(http://www.drivethrurpg.com/shared_images/ajax-loader.gif) center center no-repeat; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<video id="video_id" controls="" poster="http://www.drivethrurpg.com/shared_images/ajax-loader.gif"> 
 
    <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
 

 
</video>

+0

感谢您的答复和纠正我的错误,但仍然代码没有在Safari –

+0

工作什么不工作?海报和背景图片都适合我。我正在使用'Safari 10.0.3'。你在谈论iOS或macOS上的Safari吗? –

+0

IOS中的Safari 5.1.7。 –