我正在使用以下解决方案使 Tumblr 通过响应式视频帖子类型嵌入 YouTube 视频:
.video-wrapper {
position: relative;
padding-bottom: 56,25%;
overflow: hidden;
width: 100% !important;
height: 0 !important;
}
.video-wrapper iframe {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
连同这个 JS 片段
// get all video wrappers
var videoWrappers = document.getElementsByClassName('video-wrapper');
// loop over each of them
[].forEach.call( videoWrappers, function (el, i) {
// cache width and height values of the iframe
var width = el.getElementsByTagName('iframe')[0].width,
height = el.getElementsByTagName('iframe')[0].height;
// apply the padding to the video wrapper
el.style.paddingBottom = height / width * 100 + '%';
});
当帖子类型为视频时,这可以正常工作 - 但是,当视频嵌入标准文本帖子中时,视频不再响应(请参阅blog.paulyb.me以查看此操作)。
我试图弄清楚如何调整代码来解决这个问题,有什么建议吗?