Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery stop HTML5 video when hidden, restart when visibile

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);
        });
    });
like image 335
technopeasant Avatar asked Jan 14 '11 00:01

technopeasant


People also ask

How do I stop video playing in HTML?

The pause() method halts (pauses) the currently playing audio or video.

How to stop jQuery execution?

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).

How do you stop a video in JavaScript?

Stopping videos with JavaScript #var stopVideo = function ( element ) { var iframe = element. querySelector( 'iframe'); var video = element. querySelector( 'video' ); if ( iframe !==


1 Answers

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
        });
    });
});
like image 63
lonesomeday Avatar answered Sep 18 '22 06:09

lonesomeday