javascript - 响应式嵌入式 youtube

标签 javascript html css

我想在我的网站上放一个响应式的 YouTube 视频。我需要它在桌面和移动设备上看起来不错。 这是我尝试的代码:

.iframe-container{
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  height: 0;
}
.iframe-container iframe{
  position: absolute;
  top:0;
  left: 0;
  width: 100%;
  height: 100%;
}
    <!--Gallery section-->
    <section id="songs" class="gallery main brd-bottom">
        <div class="container">
            <h1>נזילה בגוף - מוראס (אודיו)</h1>
            <div class="iframe-container">
                <iframe width="560" height="315" src="https://www.youtube.com/embed/krhum9lNoXw"
                        frameborder="0"
                        allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
                        allowfullscreen>
                </iframe>
                <h1 class="large list-inline-item">
                    <a href="https://www.youtube.com/watch?v=krhum9lNoXw"
                       class="hover-effect"><i class="icon-youtube"></i> </a>
                </h1>
                <h1 class="large list-inline-item">
                    <a href="https://open.spotify.com/artist/3YX6pjxMfcQ1r2yPJaxpBi"
                       class="hover-effect"><i class="icon-spotify"></i> </a>
                </h1>
            </div>
            <!--End song-->
        </div>
        <!-- End container -->
    </section>
    <!--End gallery section-->

这是它当前查看我的网站的方式: video not responsive 我是 CSS 和 HTML 的新手,所以如果我忘记上传任何东西,请告诉我,我会添加它。 要查看我正在处理的网站,您可以查看:http://nhrnhr0.pythonanywhere.com/ 谢谢!

最佳答案

来自docs : 你应该使用播放器 setSize 方法

如果您希望在调整页面大小时执行此操作,请使用 window.resize 事件(如 MDN 中所述)

简而言之:

window.onresize = function() {
    // make sure the player variable is global or visible in current scope in your code
    player.setSize(window.innerWidth, window.innerHeight); 
}

关于javascript - 响应式嵌入式 youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59599712/

相关文章:

php - 是否可以将使用 php 上传的文件传递给 javascript 作为输入?

javascript - 表列 slider 交替

html - 悬停一个元素时如何影响其他元素

css - 悬停时我可以设置框模型宽度的样式吗?

javascript - 从 jQuery + AJAX 请求中选择时,IE8 返回 NULL

javascript - 将事件类添加到所选项目 react

javascript - 在 Promise 中执行 'fn.apply(null, arguments)' 时无法得到我应该做的事情

php - 将新条件添加到 header if 语句中 - 如果 URI 是某个页面,则不显示在网站上其他任何地方显示的代码

javascript - 如何在 React 中设置单个 <li> 的样式而不是所有 <li> 的样式?

html - css/html,如何缩放和居中 Logo 图像