我制作了3个版本的网站背景视频,以适合移动设备/平板电脑/台式机。我想通过屏幕宽度选择相关的src。 (不需要听众)我在这个论坛上找到了两个效果很好的脚本!但是所有解决方案都是针对2个版本而不是3个版本(如果> else)也许对javascript有一定了解的人可以向我展示正确编辑代码的方法或为第三个src选项使用其他代码。
这是我发现的JS,它很简单,非常适合2个版本:
var video = document.getElementById("video");
if (window.screen.width > 480) {
//video source for resolution greater than 480p
video.src = 'http://techslides.com/demos/sample-videos/small.mp4';
}else {
//video source for resolution less than 480p
video.src = 'http://media.w3.org/2010/05/sintel/trailer.mp4';
}
var video = document.getElementById("video");
if (window.screen.width > 480) {
//video source for resolution greater than 480p
video.src = 'http://techslides.com/demos/sample-videos/small.mp4';
}else if(window.screen.width < 480) {
//video source for resolution less than 480p
video.src = 'http://media.w3.org/2010/05/sintel/trailer.mp4';
} else {
// Your logic here for 3rd scenario
}