如何在流体设计中定义嵌入播放器高度以保持纵横比? (当宽度为100%时)
有没有办法将嵌入播放器视为流体设计中的图像?
我通常将图像的宽度设置为100%,并让浏览器计算高度(现代浏览器以原始宽高比缩放图片)。
元素的情况并非如此。我意识到没有给出视频信息,但它应该以某种方式可用,没有固定的像素值。在大多数情况下,仅使用 16:9 和 4:3 的宽高比。如果可以添加这两个值,那将是一个很大的帮助。
使用 width="100%"
和/或 style="width: 100%;"
嵌入的视频会正确(水平)填充容器。 高度应该是(宽度/4*3)或(宽度/16*9),但据我所知,没有办法计算它。
我更喜欢没有内联样式的解决方案,即使高度和宽度属性是必需的/推荐的,我也不喜欢在 HTML 中添加任何指定的长度值。 CSS 解决方案或嵌入视频的“秘密”参数将是理想的选择。如果有一个特定于 YouTube 的解决方案,那对我来说就足够了。
Is there a way to treat embed players like images in a fluid design?
I usually set the width of images to 100%, and let the browser calculate the height (modern browsers scale pictures with the original aspect ratio).
That's not the case with the <embed>
element. I realize that the video information isn't given, but it should be available somehow, without fixed pixel values. In most cases only 16:9 and 4:3 aspect-ratios are used. If just those two values could be added, it would be a great help.
With width="100%"
and/or style="width: 100%;"
the embedded video fills the container properly (horizontally). The height should be (width/4*3) or (width/16*9) but as far as I know there's no way to calculate it.
I'd prefer a solution without inline styles, and even though the height and width attributes are required/recommended I don't like to add any specified length values in HTML. A CSS solution or a "secret" parameter for the embedded video would be ideal. If there's a youtube-specific solution, that's enough for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个解决方案对我来说非常有效。
http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
^ CSS 仅适用于液体 YouTube 嵌入尺寸。
这样你就可以完全删除 iframe 的高度和宽度属性。它将自动适应其父容器的宽度。
This solution worked perfectly for me.
http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
^ CSS only liquid youtube embed dimensions.
With this you can completely remove the height and width attributes of the iframe. It will automatically fit it's parent container's width.
你可以用 jQuery 来做到这一点。像这样的东西:
HTML:
JS:
但是,如果您想要该功能,您也必须在 window.resize 上执行此操作。
You could do it with jQuery. Something like this:
HTML:
JS:
You would, however have to do it on window.resize aswell, if you want that functionality.
另外,请查看 über-guru Chris Coyer 设计的这个小 jQuery 插件:http://fitvidsjs.com
also, check out this little jQuery plugin by über-guru Chris Coyer: http://fitvidsjs.com
我只需要为一个项目执行此操作,其中我有一个嵌入的 YouTube 视频,位于背景图像顶部的页面中心,该视频设置为显示 100vh x 100vh 减去标题和粘性导航栏的一点空间。重要的是,YouTube 视频不仅要缩放,而且要与图像底部保持精确的 30 像素边距。
因此,我在页面加载时设置相对于窗口大小的背景图像的大小,并在 onYouTubeIframeAPIReady 函数中定义 iframe 的尺寸,该函数仅在加载框架后由 Youtube API 调用。我在窗口调整大小方面也有相同的功能,这样如果您调整浏览器窗口的大小,它就会流畅地缩放。
由于垂直边距是一个重要问题,因此我从高度开始,根据背景图像的高度减去边距所需的空间来设置 iframe 的高度。然后,我将高度除以 9 再乘以 16,手动计算出宽度的 16:9 纵横比。
I just had to do this for a project in which I had an embedded youtube video centered on the page on top of a background image which was set to display 100vh x 100vh minus a bit of space for a header and stickynav bar. It was important that the youtube video not only scale but maintain a precise 30 pixel margin from the bottom of the image.
So I set the size of the background image on page load relative to the size of the window and define the dimensions of the iframe within the onYouTubeIframeAPIReady function which is called by the Youtube API only after the frame is loaded. I've also got the same function on window resize so that it will scale fluidly if you resize the browser window.
Since the vertical margin was an important issue I started with the height, setting the height of the iframe based on the height of the background image minus the space needed for margins. I then manually calculated the 16:9 aspect ratio for the width by dividing the height by 9 and multiplying by 16.