Home › Forums › JavaScript › Stop YouTube video playing when overlay is closed
- This topic is empty.
-
AuthorPosts
-
December 13, 2017 at 4:11 am #263792grimskiParticipant
I have a simple overlay which works fine but I’ve just added a YouTube embed to it and when you close the overlay, the video continues to play in the background. I’m hoping someone has an idea to stop this happen! The video pausing would be ideal but jumping back to the start is ok if that’s too complicated.
The (inherited) code looks like this:
$('#size-guide a').click(function(e){ e.stopPropagation(); e.preventDefault(); $('.sizeguide-popup-container').addClass('reveal'); $('html,body').addClass('noscroll'); $("header.mainHeader").css("position", "absolute").css("z-index", "100"); }); $(document).on("click", ".sizeguide-popup-close", function() { $(".sizeguide-popup-container").removeClass('reveal'); $('html,body').removeClass('noscroll'); $("header.mainHeader").css("position", "fixed").css("z-index", "9999999"); }); $('.sizeguide-popup-container').on('click', function(e) { if (e.target !== this) return; $('.sizeguide-popup-container').removeClass('reveal'); $('html,body').removeClass('noscroll'); $("header.mainHeader").css("position", "fixed").css("z-index", "9999999"); });
I’ve tried amending the 2nd block to this:
$(document).on("click", ".sizeguide-popup-close", function() { $(".sizeguide-popup-container").removeClass('reveal'); $('html,body').removeClass('noscroll'); $("header.mainHeader").css("position", "fixed").css("z-index", "9999999"); $('.rte__video-wrapper').find('iframe').attr('src',''); });
Which certainly stops the video from playing but obviously it completely removes the videos URL so when you relaunch the overlay there’s a big empty space where the video should be – any ideas?
December 13, 2017 at 9:55 am #263798bclovisParticipantIf your following the youtube API for embeds, you need to fire off player.stopVideo(); on the overlay close event.
December 13, 2017 at 10:57 am #263808grimskiParticipantThe embed is just grabbed off the video page. So it’s just something like this…
EDIT Had to remove
iframe
YouTube embed as it won’t show the code here!Thought it best to keep it that way as a user will need to add videos to content areas in future.
December 13, 2017 at 11:21 am #263820bclovisParticipant$(‘#playerID’).get(0).stopVideo(); would work in that case, but if there will be several different videos, a way to get the proper ID would be needed.
December 13, 2017 at 1:32 pm #263824grimskiParticipantSo would something like the following be ok or does it need to be more specific (ID) than that?
$(document).on("click", ".sizeguide-popup-close", function() { $(".sizeguide-popup-container").removeClass('reveal'); $('html,body').removeClass('noscroll'); $("header.mainHeader").css("position", "fixed").css("z-index", "9999999"); $('.rte__video-wrapper iframe').get(0).stopVideo(); });
Don’t have access at home so thought I’d ask. Trying to get access now!
December 13, 2017 at 8:40 pm #263869MelisaParticipantThank you for this post it helped me too.
December 14, 2017 at 1:19 am #263872ShikkedielParticipantFrom a quick search, it looks like this is the most recent approach (
stopVideo
might be deprecated):https://codepen.io/briangelhaus/pen/meeLRO?editors=1010
Edit – hmm, should still work. Seeing some conflicting messages.
December 14, 2017 at 1:57 pm #263887grimskiParticipantYeah that’s what I found when I did a Google too. I couldn’t find a solution that worked really. I tried the code I put above but that didn’t seem to do anything.
December 14, 2017 at 5:25 pm #263906bclovisParticipantYes sorry, lucky for me for the most part I haven’t had to deal with youtube videos in a while, but here is a quick one using the API:
https://codepen.io/bwclovis/pen/WdbWwd
The idea is to have the video ID in the button that gets dynamically loaded when the button is clicked, so you can have multiple buttons with the multiple IDs. There would have to be an each loop there but, this should get you in the right direction.
December 14, 2017 at 7:43 pm #263910namphan1998ParticipantThank you so much for the replies.
It really helps me.
December 15, 2017 at 4:22 am #263923 -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.