开发者

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 i开发者_Python百科t'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);
        });
    });


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
        });
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜