javascript - Start/Pause HTML5 Video When Other Element Hovered -
just trying video play when "overlay" div hovered. background on issue, have several videos on page , when hovered, play.
all works fine until i've added div lays above video. once i've added second "overlay" div on top of video, code below no longer works because i'm not technically hovered on video anymore.
i'm using overlay div on top of video put information it.
<script type="text/javascript"> var figure = $('.movies'); var vid = $("video"); var cover = $(".video-overlay"); [].foreach.call(vid, function (item) { item.addeventlistener('mouseover', hovervideo, false); item.addeventlistener('mouseout', hidevideo, false); }); function hovervideo(e) { this.play(); } function hidevideo(e) { this.pause(); } </script> how can adjust "if hovered on div class .video-overlay, start video. if not hovered, pause."
solution in javascript:
var vid = document.getelementbyid("myvideo"); function playvid() { vid.play(); } function pausevid() { vid.pause(); } then, in jquery this:
$('.myhoveredelement').hover(function(){ // happen when .myhoveredelement hovered vid.play(); }, function(){ // happen when .myhoveredelement not hovered anymore (mouseleave) vid.pause(); }); here documentation of jquery hover element
if want work on every video has element hover it, have give class every video's container overlay want react on hover. example, html this:
<div class="video-container"> <video ...> </video> <div class="video-overlay"></div> </div> then, you'd this:
$('.video-container').hover(function(){ //happens when you're hover (mouseenter) $(this).children('video').get(0).play(); }, function(){ // happens when mouse leaves video container $(this).children('video').get(0).stop(); }) this way, work when hover video has container named .video-container on hover.
hope answers questions
Comments
Post a Comment