如何在图像上播放视频?

时间:2017-04-02 08:04:08

标签: html css

我有以下图片:

enter image description here

现在我想在此图像上放置一个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

我想在图像灰色背景上设置它,我该怎么做?

2 个答案:

答案 0 :(得分:1)

检查代码,但需要进行一些微调。

&#13;
&#13;
$(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();

});
&#13;
figure {
    display: block;
    background: url(https://i.stack.imgur.com/JQiLu.jpg); 
    background-size: contain;
    background-repeat: no-repeat;  
    padding: 7%;
}

figcaption {
  position: relative;
  top: 50px;
}
&#13;
<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>
&#13;
&#13;
&#13;

感谢 https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php

答案 1 :(得分: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://i.stack.imgur.com/GsgXg.png">
<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>