I want to embed a video from YouTube that is not mine (so I can not change it at YouTube). The video has a thumbnail that is not representative for the video (I refer to the
Using the concept from waldyrious's answer, I've created the following solution that also addresses the issue of the video playing behind the image on tab restore or using the browser's back button to come back to the page with the video.
<div class="js-video-lead">
<img class="hide" src="link/to/lead/image.jpg" />
<iframe frameborder="0" height="240" src="https://www.youtube.com/embed/<code here>" width="426"></iframe>
</div>
The "hide" class is from Bootstrap and simply applies display: none;
so that the image is not visible on page load if JavaScript is disabled.
function video_lead_play_state(element, active)
{
var $active = $(element).closest(".js-video-lead").find(".btn-play-active");
var $default = $(element).closest(".js-video-lead").find(".btn-play-default");
if (active) {
$active.show();
$default.hide();
} else {
$active.hide();
$default.show();
}
}
$(document).ready(function () {
// hide the videos and show the images
var $videos = $(".js-video-lead iframe");
$videos.hide();
$(".js-video-lead > img").not(".btn-play").show();
// position the video holders
$(".js-video-lead").css("position", "relative");
// prevent autoplay on load and add the play button
$videos.each(function (index, video) {
var $video = $(video);
// prevent autoplay due to normal navigation
var url = $video.attr("src");
if (url.indexOf("&autoplay") > -1) {
url = url.replace("&autoplay=1", "");
} else {
url = url.replace("?autoplay=1", "");
}
$video.attr("src", url).removeClass(
"js-video-lead-autoplay"
);
// add and position the play button
var top = parseInt(parseFloat($video.css("height")) / 2) - 15;
var left = parseInt(parseFloat($video.css("width")) / 2) - 21;
var $btn_default = $("<img />").attr("src", "play-default.png").css({
"position": "absolute",
"top": top + "px",
"left": left + "px",
"z-index": 100
}).addClass("btn-play btn-play-default");
var $btn_active = $("<img />").attr("src", "play-active.png").css({
"display": "none",
"position": "absolute",
"top": top + "px",
"left": left + "px",
"z-index": 110
}).addClass("btn-play btn-play-active");
$(".js-video-lead").append($btn_default).append($btn_active);
});
$(".js-video-lead img").on("click", function (event) {
var $holder = $(this).closest(".js-video-lead");
var $video = $holder.find("iframe");
var url = $video.attr("src");
url += (url.indexOf("?") > -1) ? "&" : "?";
url += "autoplay=1";
$video.addClass("js-video-lead-autoplay").attr("src", url);
$holder.find("img").remove();
$video.show();
});
$(".js-video-lead > img").on("mouseenter", function (event) {
video_lead_play_state(this, true);
});
$(".js-video-lead > img").not(".btn-play").on("mouseleave", function (event) {
video_lead_play_state(this, false);
});
});
jQuery is required for this solution and it should work with multiple embedded videos (with different lead images) on the same page.
The code utilizes two images play-default.png
and play-active.png
which are small (42 x 30) images of the YouTube play button. play-default.png
is black with some transparency and is displayed initially. play-active.png
is red and is displayed when the user moves the mouse over the image. This mimic's the expected behavior that a normal embedded YouTube video exhibits.
It's possible using jQuery it depends on your site load time you can adjust your timeout. It can be your custom image or you can use youtube image maxres1.jpg, maxres2.jpg or maxres3.jpg
var newImage = 'http://i.ytimg.com/vi/[Video_ID]/maxres1.jpg';
window.setTimeout(function() {
jQuery('div > div.video-container-thumb > div > a > img').attr('src',newImage );
}, 300);
This tool gave me following results which helps me achieve the task as following code.
<div onclick="play();" id="vidwrap" style="height:315px;width:560px;background: black url('http://example.com/image.jpg') no-repeat center;overflow:hidden;cursor:pointer;"></div>
<script type="text/javascript">
function play(){
document.getElementById('vidwrap').innerHTML = '<iframe width="560" height="315" src="http://www.youtube.com/embed/xxxxxxxxx?autoplay=1" frameborder="0"></iframe>';
}
</script>
This solution will play the video upon clicking. You'll need to edit your picture to add a button image yourself.
You're going to need the URL of your picture and the YouTube video ID. The YouTube video id is the part of the URL after the v=
parameter, so for https://www.youtube.com/watch?v=DODLEX4zzLQ the ID would be DODLEX4zzLQ
.
<div width="560px" height="315px" style="position: static; clear: both; width: 560px; height: 315px;"> <div style="position: relative"><img id="vidimg" width="560px" height="315px" src="URL_TO_PICTURE" style="position: absolute; top: 0; left: 0; cursor: pointer; pointer-events: none; z-index: 2;" /><iframe id="unlocked-video" style="position: absolute; top: 0; left: 0; z-index: 1;" src="https://www.youtube.com/embed/YOUTUBE_VIDEO_ID" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></div></div>
<script type="application/javascript">
// Adapted from https://stackoverflow.com/a/32138108
var monitor = setInterval(function(){
var elem = document.activeElement;
if(elem && elem.id == 'unlocked-video'){
document.getElementById('vidimg').style.display='none';
clearInterval(monitor);
}
}, 100);
</script>
Be sure to replace URL_TO_PICTURE and YOUTUBE_VIDEO_ID in the above snippet.
To clarify what's going on here, this displays the image on top of the video, but allows clicks to pass through the image. The script monitors for clicks in the video iframe, and then hides the image if a click occurs. You may not need the float: clear
.
I haven't compared this to the other answers here, but this is what I have used.
This solution is HTML and CSS based using z-index and hover, which works if JS is disabled or the video isn't yours (since you can add a thumbnail in YouTube).
<style>
.videoWrapper {
position: relative;
padding-bottom: 56.25%;
}
.videoWrapper .video-modal-poster img {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 10;
}
.videoWrapper .video-modal-poster:hover img {
z-index:0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
</style>
<div class="videoWrapper">
<a href="#" class="video-modal-poster">
<img alt="" src="" width="353" height="199" />
<iframe width="353" height="199" src="https://www.youtube.com/embed/" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
</a>
</div>
No. Most YouTube videos only have one pre-generated "poster" thumbnail (480x360). They usually have several other lower resolution thumbnails (120x90). So even if there were an embedding parameter to use an alternate poster image (which there isn't), it's result wouldn't be acceptable.
You can theoretically use the Player API to seek the video to whatever location you want, but this would be a major hack for a minor result.