I've got an HTML5 video embedded in a page that's set for autoplay on load. When a menu is toggled, it is hidden and a series of images take its place. When the menu is put closed, the video returns. It was recommended that I stop the video while it's hidden and resume it once it's back to conserve resources, which I'd like to do, but stop and restart (instead of resume).
Any suggestions? I know it's a grey area.
Thanks!
HTML:
<div id="content">
<video id="vid_home" width="780" height="520" autoplay="autoplay" loop="loop">
<source src="Video/fernando.m4v" type="video/mp4" />
<source src="Video/fernando.ogg" type="video/ogg" />
Your browser does not support this video's playback.
</video>
<img id="img_home" src="Images/home.jpg" alt="Fernando Garibay />
</div>
Javascript:
// Navigation hover image preview
$('#img_home').css('display', 'none');
$('.nav').hover(function(){
$('#vid_home').fadeOut(600, function(){
$('#img_home').fadeIn(800);
});
});
$('#item1').hover(function(){
$('#img_home').attr('src', 'Images/music.jpg');
});
$('#item2').hover(function(){
$('#img_home').attr('src', 'Images/photos.jpg');
});
$('#item3').hover(function(){
$('#img_home').attr('src', 'Images/biography.jpg');
});
$('#item4').hover(function(){
$('#img_home').attr('src', 'Images/discography.jpg');
});
$('#item5').hover(function(){
$('#img_home').attr('src', 'Images/contact.jpg');
});
$('#item6').hover(function(){
$('#img_home').attr('src', 'Images/blog.png');
});
// Navigation hover image leave
$('#trigger').mouseleave(function(){
$('#img_home').fadeOut(400, function(){
$('#vid_home').fadeIn(400);
});
});
The pause() method halts (pauses) the currently playing audio or video.
The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).
Stopping videos with JavaScript #var stopVideo = function ( element ) { var iframe = element. querySelector( 'iframe'); var video = element. querySelector( 'video' ); if ( iframe !==
You need to call pause
and play
on the DOM elements, which will probably look something like this:
$('.nav').hover(function(){
$('#vid_home').fadeOut(600, function(){
$('#img_home').fadeIn(800);
}).get(0).pause(); // pause before the fade happens
});
and
$('#trigger').mouseleave(function(){
$('#img_home').fadeOut(400, function(){
$('#vid_home').fadeIn(400, function() {
this.play(); // play after the fade is complete
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With