How to put image behind embedded youtube iframe

天涯浪子 提交于 2019-12-13 03:37:05

问题


i'd like to put an image behind my youtube iframe. The image will act as a visual frame around the youtube window. I've tried different methods, but either both elements where placed after each other, or the iframe was positioned relative to the window size of the browser. Both elements will sit in a table. I want the youtube window to sit about in the center of the background image and nothing should put both elements apart (like changing the window size of the browser etc.) In addition it would be pretty nice to have a custom preview picture for the video instead of the youtube window until it gets clicked (and possible put the custom picture back in place when the youtube video has been watched).


回答1:


Set background-image behind your Youtube iframe and use JavaScript for custom preview picture.

TRY - DEMO

HTML:

<div id="background-video">
    <div onclick="play();" id="vidwrap"></div>
</div>

<!-- <iframe width="480" height="360" src="[YouTube Video URL]?autoplay=1" frameborder="0"> -->

<script type="text/javascript">function play(){document.getElementById('vidwrap').innerHTML = '<iframe width="480" height="360" src="http://www.youtube.com/embed/7-7knsP2n5w?autoplay=1" frameborder="0"></iframe>';}</script>

CSS:

#background-video {

    /* Your background image */

    background-image: url('http://placehold.it/580x460');

    background-repeat: no-repeat;
    padding: 50px;
}

#vidwrap {

    /* Your thumbnail image */

    background: url('http://www.edu.uwo.ca/img/click_to_play.png') no-repeat center;

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    overflow:hidden;
    background-repeat: no-repeat;
    width:480px;
    height:360px;
    cursor:pointer;
}



回答2:


Have you already tried to have your youtube-iframe in a container and set the image as background-image of this container? Like this:

<div class="youtube">
 <-- youtube iframe or target here -->
</div>

and css:

div.youtube {
 background: url('url-of-your-image.png')
 padding: 50px; // adjust as needed
} 


来源:https://stackoverflow.com/questions/25582694/how-to-put-image-behind-embedded-youtube-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!