Last active
February 9, 2018 15:37
-
-
Save charleslouis/1f7101f19ce9696d72b7 to your computer and use it in GitHub Desktop.
Youtube iframe ajax auto player with poster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// je déclare mes fonctions | |
function playTheVideoOnClick(idWrapper, posterClass, idVideoAttr){ | |
// lancement de la video si clic sur button play sur cover image | |
$(idWrapper).click(function(){ | |
var idVideo = $(this).attr(idVideoAttr); | |
var videoIframe = '<iframe width="560" height="315" src="https://www.youtube.com/embed/' + idVideo + '?rel=0&showinfo=0&autoplay=1" frameborder="0" allowfullscreen></iframe>'; | |
$(this).append(videoIframe); | |
$(this).find(posterClass).fadeOut(); | |
}); | |
} | |
// on document ready ... | |
$( document ).ready( function() { | |
playTheVideoOnClick('.videoWrapper', '.video-poster', 'data-youtube-id'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<div class="videoWrapper" id="videoWrapper" data-youtube-id="3ZUA5krsNwU"> | |
<i aria-hidden="true" class="video-poster" id="video-poster"> | |
<i aria-hidden="true" class="video-button-play" id="video-button-play"> | |
</i> | |
</i> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.videoWrapper { | |
position: relative; | |
padding-bottom: 56.25%; /* 16:9 */ | |
margin: 30px 0; | |
height: 0; | |
cursor: pointer; | |
} | |
.videoWrapper iframe { | |
z-index: 1; | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
} | |
.video-poster { | |
position: absolute; | |
z-index: 2; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
background-image: url('../media/img/cover-video.png'); | |
background-position: center; | |
background-repeat: no-repeat; | |
-webkit-background-size: cover; | |
-moz-background-size: cover; | |
background-size: cover; | |
} | |
.video-button-play { | |
position: absolute; | |
z-index: 3; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
background-image: url('../media/img/button-play-video.png'); | |
background-position: center; | |
background-repeat: no-repeat; | |
-webkit-background-size: 15%; | |
-moz-background-size: 15%; | |
background-size: 15%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment