如何在图像上播放视频?

6

我有以下图像:

输入图像描述

现在,我想在此图像上放置一个具有响应式视图的 Vimeo 视频:

为此,我正在使用以下 CSS 和 HTML 代码:

HTML 代码:

<div class="vimeo">
<article>
    <figure>
        <iframe src="https://player.vimeo.com/video/211148482?autoplay=1&title=0&byline=0&portrait=0" width="568" height="453" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>       
    </figure>        
</article>
</div>

CSS代码:

.vimeo { 
    font: 16px/1.4 Georgia, Serif;
    width: 45%; 
    margin: 1px auto;   
    background: url(images/bg.jpg); 
    background-size: contain;
    background-repeat: no-repeat;   
    position: relative;
}

pre, article style, article script { 
    white-space: pre; 
    display: block;         
    overflow-x: auto; 

}

img { 
    max-width: 100%;
}
figure { 
    display: block; 
}
figcaption { 
    display: block; text-align: center; orphans: 2;
}

现在它看起来是这个图像: enter image description here 我想把它放在灰色的背景上,应该怎么做?

https://dev59.com/1mgv5IYBdhLWcg3wDsjZ - Sachith Wickramaarachchi
可能是HTML:在图像内播放视频的重复问题。 - Rob
2个回答

1
检查代码,虽然需要一些微调。

$(function() {

    var $allVideos = $("iframe[src^='//player.vimeo.com'], iframe[src^='//www.youtube.com'], object, embed"),
    $fluidEl = $("figure");

 $allVideos.each(function() {

   $(this)
     // jQuery .data does not work on object/embed elements
     .attr('data-aspectRatio', this.height / this.width)
     .removeAttr('height')
     .removeAttr('width');

 });

 $(window).resize(function() {

   var newWidth = $fluidEl.width();
   $allVideos.each(function() {

     var $el = $(this);
     $el
         .width(newWidth)
         .height(newWidth * $el.attr('data-aspectRatio'));

   });

 }).resize();

});
figure {
    display: block;
    background: url(https://istack.dev59.com/JQiLu.webp); 
    background-size: contain;
    background-repeat: no-repeat;  
    padding: 7%;
}

figcaption {
  position: relative;
  top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<figure>
  <iframe src="//player.vimeo.com/video/211148482?autoplay=0&title=0&byline=0&portrait=0" width="400" height="225" frameborder="0"></iframe>
  <figcaption>Caption</figcaption>
 </figure>

https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php致敬。


哇,太棒了,它正在工作,但为什么视频的左右之间有黑色背景? - Shibbir
好的,这是关于视频本身的背景。你需要设置视频帧的初始大小,然后它将保持该比例。例如,将初始宽度缩小并增加左右填充。 - faster

0

您可以避免使用JavaScript,而只使用CSS。

除了灰色之外,您可以设置透明颜色并使用PNG图像格式。您需要CSS声明background-color: transparent来在透明颜色上方显示视频。此外,您需要CSS声明position: absolute来重叠图像和视频。

请参见代码片段。(请注意,您可以改进PNG图像)

希望我能有所帮助。

<div style="position: absolute; width: 882px; height: 590px;">
<img style="position: absolute; background-color: transparent; width: 100%; height: 100%;" src="https://istack.dev59.com/GsgXg.webp">
<iframe src="https://player.vimeo.com/video/211148482?autoplay=1&title=0&byline=0&portrait=0" width="100%" height="100%" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接