我有一个YouTube视频想要放在我的网页上。
我希望将视频缩放到适合用户浏览器的百分比大小,并保持纵横比。
我尝试了以下代码:
<iframe width="87%" height="315" src="http://www.youtube-nocookie.com/embed/dU6OLsnmz7o" frameborder="0" allowfullscreen></iframe>
但这只会让播放器变宽,而不会变高。
我是否必须使用JavaScript(或非标准CSS)?
我认为这是最好的CSS解决方案。
.auto-resizable-iframe {
max-width: 420px;
margin: 0px auto;
}
.auto-resizable-iframe > div {
position: relative;
padding-bottom: 75%;
height: 0px;
}
.auto-resizable-iframe iframe {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
<div class="auto-resizable-iframe">
<div>
<iframe frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/_OBlgSz8sSM"></iframe>
</div>
</div>
在开发一些响应式CSS时,我的网站遇到了类似的问题。我希望任何嵌入的YouTube对象在从桌面CSS切换到较小的样式(我使用媒体查询重新渲染移动设备内容)时可以按比例缩放。
我采用的解决方案是基于CSS和标记的。基本上,在我的CSS中有三个视频类:
.video640 {width: 640px; height: 385px}
.video560 {width: 560px; height: 340px}
.video480 {width: 480px; height: 385px}
根据原始大小(您可能需要更多的类,我只选择了最常见的大小),我会为包含的Youtube内容分配其中一个类。
在用于较小设备的媒体查询CSS中,这些相同的类只是被重新说明,如下所示:
.video640 {width: 230px; height: 197px}
.video560 {width: 230px; height: 170px}
.video480 {width: 240px; height: 193px}
我很欣赏在HTML中包含视频时需要一些标记“前置”(例如添加类),但如果您不想使用Javascript,这个方法非常有效--您可以为所需的不同尺寸重复声明视频类。以下是Youtube标记的示例:
<object class="video640" type="application/x-shockwave-flash" value="YOUTUBE URL">
<param name="movie" value="YOUTUBE URL"></param>
</object>
CSS中引入了aspect-ratio
属性,现在可以很简单地缩放YouTube视频而不需要使用CSS hack或JS。
示例:
iframe {
aspect-ratio: 16 / 9;
height: auto;
width: 100%;
}
aspect-ratio
属性得到大多数浏览器的广泛支持,适用于绝大多数网站:https://caniuse.com/mdn-css_properties_aspect-ratio
使用一些JavaScript很容易实现。
jQuery(function() {
function setAspectRatio() {
jQuery('iframe').each(function() {
jQuery(this).css('height', jQuery(this).width() * 9/16);
});
}
setAspectRatio();
jQuery(window).resize(setAspectRatio);
});
这个jQuery插件最近很流行,它叫做FitVids,可以根据浏览器大小调整视频尺寸,并保持纵横比。
这些东西没有JS也能正常运作。对于单个播放器和列表播放器都是响应式的,从某个地方修改而来,不确定来源,抱歉无法署名。在一个容器div中加载你的iframe Youtube播放器,iframe样式设置了播放器特定的大小,100%会填充容器到任意大小,src = your-youtube-ID,添加自己的播放器选项。 https://jsfiddle.net/jcb01/04sf3byz/
<div style=" position: relative; padding-bottom: 56.25%;">
<!--- load iframe Youtube player inside this div -->
<iframe
style="border: 1; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
src="https://www.youtube-nocookie.com/embed/?
list=PL590L5WQmH8fmto8QIHxA9oU7PLVa3ntk;
&autoplay=0&enablejsapi=1&index=0&
listType=playlist&loop=1&modestbranding=1"
allowfullscreen scrolling="no"
allow="encrypted-media; accelerometer;
gyroscope; picture-in-picture">
</iframe>
</div>
虽然这个问题比较老,但我认为使用 @media CSS 3 标签可以在这种情况下有所帮助。
以下是我解决类似问题时采用的CSS:
@media only screen and (min-width: 769px) {
.yVid {
width: 640px;
height: 360px;
margin: 0 auto;
}
}
@media only screen and (max-width: 768px) {
.yVid {
width: 560px;
height: 315px;
margin: 0 auto;
}
}
HTML:
<div class="yVid">
<iframe width="100%" height="100%" frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/_OBlgSz8sSM"></iframe>
</div>
.video-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.27198%;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
HTML代码:
<div class="video-container">
<iframe width="560px" height="315px" src="https://www.youtube.com/embed/XXXXxxxx?&theme=dark&autohide=2&iv_load_policy=3"><iframe>
</div>
<div class="content-wrapper">
<div class="iframe-wrapper res-16by9">
<iframe src="https://www.youtube.com/embed/pHsYFURtzzY" frameborder="0" allowfullscreen></iframe>
</div>
</div>
.content-wrapper{
max-width: 800px;
margin: 0 auto;
background-color: #fff;
}
.iframe-wrapper{
width: 100%;
position: relative;
}
.res-4by3{
padding-bottom: 75%;
}
.res-16by9{
padding-bottom: 56.25%;
}
.iframe-wrapper iframe{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
padding-bottom
设为 60%。 - Daniel Hanrahan