So i'm trying to switch some small youtube thumbnail still images so onclick they load the relevant youtube video in main video at top. I've pretty much got it working great. But on switch of video i'd like them to start autoplaying.
Here's a JSFiddle what i got so far: http://jsfiddle.net/jw1sw01v/4/
jQuery:
$(document).on('click', '.ph-video', function (event) {
var change = $(this).find("img").attr("src").split("/");
$(".ph-master-video-section > iframe").attr("src","https://www.youtube.com/embed/" + change[4]);
});
If you click on the thumbs you'll see it's switching perfect. Just like it to auto play on switch.
Any ideas much appreciated?
you need to add to iframe src : ?rel=&autoplay=1
$(document).on('click', '.ph-video', function (event) {
var change = $(this).find("img").attr("src").split("/");
$(".ph-master-video-section > iframe").attr("src", "https://www.youtube.com/embed/" + change[4]+"?rel=&autoplay=1");
});
check this JSFiddle
Related
Here’s an example of what I want it to be like:https://tvplayer.com/watch just more simple as in no restrictions. Click to change video source almost like a YouTube playlist.
First of all - you can't dynamically change page content by HTML and CSS only. You should use a little bit of JS. in our case this one (check out codepen example to see, how it works):
document.addEventListener('click', function(e) {
var source = e.target.dataset.source,
video = document.getElementById('video');
if (source) {
video.src = "https://www.youtube.com/embed/" + source;
}
});
This is not a perfect code, but good start point for you I guess. Good luck :)
Codepen example
I'm working on a kiosk-style web page to display a menu of options. Clicking on a title opens a fullscreen video which then closes back to the menu when the video ends.
To keep the page clean, I'd like to hide the video element until the video is actually called by a click. I did this with a CSS class. The video opens fullscreen and when finished, closes and adds the hide class again.
Working script
$(document).ready(function() {
$('.cell').on('click', function() {
var element = this.getElementsByTagName('video');
var m = element[0].getAttribute('id');
console.log(m);
var v = document.getElementById(m);
if (v.webkitRequestFullscreen) {
v.className = "";
v.webkitRequestFullscreen();
}
v.play();
$("#" + m).on("ended", function() {
this.webkitExitFullscreen();
this.className = "hide";
});
})
})
I'm running into a problem of the video not hiding if the user exits full screen video on their own. I tried using $("#" + m).on("ended" || "resize", function()... based on the HTML5 video API, but that didn't work. I'd also considered disabling clicks or overlaying a full-screen div to grab any clicks and force the video to play all the way through, but that seems shady to me. Any ideas on how to approach this?
Here's a CodePen demo of the content and script
Space separated list:
$("#" + m).on("ended resize"
To make it work you might need this trick: How to detect DIV's dimension changed?
Update: I was able to catch the fullscreen event:
$(document).on('webkitfullscreenchange mozfullscreenchange fullscreenchange', function(e){
console.log("fullscreen change");
});
Trying to call the vimeo api to pause video on a click event to hide it. And, on clicking to reveal the video again, play the video from its paused position.
There are various related questions on here, I can't find an answer to the simple "how to pause". I'm a jquery novice and can't make heads or tails of the froogaloop documentation.
Here's a FIDDLE, and my current jquery script to hide the video
$(document).click(function (event) {
if (!$(event.target).hasClass('click')) {
$('#video').hide();
}
});
which hides it when an element without the "click" class is clicked. But the video plays on in the background. Froogaloop is loaded in the fiddle. Thanks everyone
Here's an updated FIDDLE that makes pause/play work as I'd imagined. Click the image to play the video; click outside or on empty space (you control this with classes) to pause it; click image again to play from paused position. Simple, no buttons, no excess jquery or froogaloop code.
Putting this here for those who might benefit from it. And a +1 to mbrrw for getting me started.
var iframe = $('#video iframe')[0];
var player = $f(iframe);
$('.showvideo').on('click', function(){
$('#video').show();
$('.image').hide();
player.api('play');
});
$(document).click(function (event) {
if (!$(event.target).hasClass('click')) { //if what was clicked does not have the class 'click' (ie. any empty space)
$('#video').hide();
$('.image').show();
player.api('pause');
}
});
Remember to append api=1 to the vimeo link. And the url must be https, not http.
froogaloop can be a pain in the arse.
The code to get you started is here:
https://developer.vimeo.com/player/js-api#universal-with-froogaloop
I've adapted that to get it working i think how you expect here:
https://jsfiddle.net/fLe5xs4v/
Setting it up like so:
var iframe = $('#video iframe')[0];
var player = $f(iframe);
Note that if you change the text in the play and pause buttons you will break this code:
$('button').bind('click', function() {
player.api($(this).text().toLowerCase());
});
Give it a shot it should get you going in the right direction at least. Good luck!
I have put my own custom title absolutely positioned over the top of an Vimeo video embed (you can see the dev site here http://ourcityourstory.com/dev/). When I click on the Vimeo video I want the title absolutely positioned over it to hide.
How do I accomplish this? None of the JS I'm writing is working.
Here is my non-working code:
$(document).click({namespace: this}, function (e) {
var t = e.data.namespace;
if ($(e.target).parents("#video-slider-wrapper iframe").length > 0 || $(e.target).is($("#video-slider-wrapper iframe"))) {
$("#episode h1").hide();
}
});
UPDATE: pimvdb's example listed below does exactly what I need my page to do — however, I keep getting the error "$f is not defined" on my page.
Your click handler does not work because the iframe is cross-domain. However, you can use the dedicated Vimeo API to add a listener when the play event is fired:
var player = $f( $('#player1').get(0) );
player.addEvent('play', function() {
$("h1").hide();
});
I am using the jquery function to toggle a youtube video element on my site. It works great and shows/hides its as needed. However I can't find a way to stop the video playing on hiding the div. I keep reading about destroying the element then recalling it on the toggle but I don't know enough about the code to know how or where to do that within the toggle functions. Any help would be amazing. I've added my code below
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
.vidblock
{
background:#ccc;
width:430px;
height:320px;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.toggle').click(function(){
$('.vidblock').toggle("slow");
});
});
</script>
and
<h3>Click to view a video of how and where to enter <?=$merch_array['Name']?> <?=$merch_array['Term_Used']?>s</h3>
Okay! This one took me a little while to figure out, and when I tell you it's going to make sense.
Make sure you are calling the youtube api:
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>
When hiding your div, use jquery to change the source of the video to the same video, without using autoplay. This stops the video from playing, and also sets it back up.
Another cool thing I found, is when opening my div, I turned it on autoplay.
You can see a demo on this page: http://advantageanywhere.com/index.aspx?sid=250
Click on the play link and it's going to open a lightbox (similar to what you are doing, showing and hiding the div. In it's original state I have it sitting there while hidden as a regular embed link. Once clicked, then it turns to autoplay, then when escape or X is clicked, it will close the lightbox and also change the video back to a normal embed (stops the playing)
We will take for example the following video:
The original embed from youtube [adding AN ID ( youtube ) to it to target in jquery, and the parm for youtube api ( ?enablejsapi=1 ) ]:
That is how you should have the image started off in it's hidden div.
Once the div is opened this is the jquery you need:
$('#youtube').attr("src", "http://www.youtube.com/embed/AAbOWbquDWU?enablejsapi=1&autoplay=1"); /* starts youtube video after lightbox opens */
Now, when the div is being hidden, this is the jquery you need:
$('#youtube').attr("src", "http://www.youtube.com/embed/AAbOWbquDWU?enablejsapi=1"); /* stops video from playing on youtube after hiding div */
Below is the entire script I have setup. I tried throwing this together for you in a fiddle but I don't have the time right now and just stumbled upon your question when I am trying to punch out a deadline.
If you have any other questions I don't mind helping out, please.
$(window).load(function(){
var KEYCODE_ESC = 27;
$(document).keyup(function(e) {
if (e.keyCode == KEYCODE_ESC) { closeLightBox(); }
});
function closeLightBox() {
$("#lb1, #lb4").fadeOut();
$("#featureContent, #videoContent").hide();
$('#youtube').attr("src", "http://www.youtube.com/embed/clJe9U38ids?enablejsapi=1"); /* stops video from playing on youtube after hiding div */
}
/* ------------ Light Box 4 Video ------------- */
var lightBox4 = $('#lb4'),
videoContent = $('#videoContent');
$('#anywhereVideo').click(function() {
lightBox4.fadeIn(function() {
videoContent.show();
$('#youtube').attr("src", "http://www.youtube.com/embed/clJe9U38ids?enablejsapi=1&autoplay=1"); /* starts youtube video after lightbox opens */
});
});
$('#close4').click(function() {
closeLightBox();
});
});
You don't have to destroy the element, just use the YouTube JavaScript API ( http://code.google.com/apis/youtube/js_api_reference.html ) to pause or stop the video.
You give the object a playerapiid, then you can target it with JavaScript.
You could use 'detach' -
$(function() {
var savedNodes;
$('.toggle').toggle(function() {
$('.vidblock').hide('slow', function() {
savedNodes = $('yourplayerid').detach();
});
}, function() {
$('.vidblock').append(savedNodes)
$('.vidblock').show('slow');
});
});
Demo - http://jsfiddle.net/Sf4LT/2/