use*_*853 10 video html5 google-chrome html5-video media-queries
使用以下内容,Chrome不会尊重媒体查询,以根据设备宽度显示正确的视频源.Chrome正在播放它找到的第一个来源,你可以在这里看到:http://homeglobal.ch/.我怎样才能解决这个问题?
<video id="intro-video" poster="img/poster.png" controls>
<source src="videos/intro.mp4" type="video/mp4" media="all and (min-device-width:1600px)">
<source src="videos/intro.webm" type="video/webm" media="all and (min-device-width:1600px)">
<source src="videos/intro-1600.mp4" type="video/mp4" media="all and (min-device-width:1100px)">
<source src="videos/intro-1600.webm" type="video/webm" media="all and (min-device-width:1100px)">
<source src="videos/intro-1100.mp4" type="video/mp4" media="all and (min-device-width:481px)">
<source src="videos/intro-1100.webm" type="video/webm" media="all and (min-device-width:481px)">
<source src="videos/intro-480.mp4" type="video/mp4">
<source src="videos/intro-480.webm" type="video/webm">
</video>
Run Code Online (Sandbox Code Playgroud)
Fid*_*zco 10
不幸的是,Chrome不支持视频html 5标签的媒体查询.解决这个问题的方法是使用普通的Javascript或Jquery.它不漂亮,但即使在chrome中也能正常工作.
var video = $('#myvideo');
var WindowWidth = $(window).width();
if (WindowWidth < 1200) {
//It is a small screen
video.append("<source src='/img/homepage/640/splash.m4v' type='video/mp4' >");
} else {
//It is a big screen or desktop
video.append("<source src='/img/homepage/1080/uimain-1080.mp4' type='video/mp4' >");
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我在 Vanilla JS 上的实现。
在video标签中,作为data-src属性,指定默认视频的路径。在标签内部video,在标签中,我们指定和source两个属性的值。data-srcdata-mw
data-src- 是视频文件的路径。
data-mw- 是显示该视频的最大屏幕宽度。
当屏幕宽度改变时,该解决方案会自动工作。这是使用该resize()函数完成的。
<video id="intro-video" data-src="/path/default.mp4" poster="img/poster.png" controls>
<source data-src="/path/1600.mp4" data-mw="1600">
<source data-src="/path/900.mp4" data-mw="900">
<source data-src="/path/480.mp4" data-mw="480">
</video>
Run Code Online (Sandbox Code Playgroud)
class VideoResponser {
constructor(selector) {
const $video = document.querySelector(selector);
this.options = {
selector,
breakpoints: { default: { src: $video.getAttribute('data-src') } }
};
// get a list of video switching points and links to the videos themselves
$video.querySelectorAll('[data-src]').forEach(element => this.options.breakpoints[element.getAttribute('data-mw')] = { src: element.getAttribute('data-src') });
$video.innerHTML = ''; // we clean up so that there is nothing superfluous
// run the handler and track the change in screen width
this.responseVideo(this.options);
this.resizer();
}
/** Function runs on resize */
resizer() {
window.addEventListener("resize", () => this.responseVideo(this.options));
}
/**
* Change src value of video link to fit screen width
*
* @param {Object} options object with options
*/
responseVideo(options) {
const {selector, breakpoints} = options; // get options
let $video = document.querySelector(selector);
const widthNow = $video.getAttribute('data-width-now') || null;
const maxBreakpoint = Math.max.apply(Math, Object.keys(breakpoints).filter(key => key <= document.body.clientWidth).map(Number));
const nowBreakpoint = maxBreakpoint || 'default'; // choose either the maximum value, if not, then the default
if(widthNow && widthNow == nowBreakpoint) return; // check if the video needs to be changed
$video.setAttribute('data-width-now', nowBreakpoint);
$video.src = breakpoints[nowBreakpoint].src;
}
}
new VideoResponser("#intro-video");
Run Code Online (Sandbox Code Playgroud)