admin管理员组文章数量:1432449
As you can see I have a question about playing/stoping video on hover. The user would hover the mouse on video and it would start playing and when he move the mouse away video would stop playing. Is it possible and how? I have video tag here
<video id="video" controls="" loop="" src="/wp-content/uploads/2016/06/vid/elipsiniai-treniruokliai.mp4" width="auto" height="auto" alt=""></video>
As you can see I have a question about playing/stoping video on hover. The user would hover the mouse on video and it would start playing and when he move the mouse away video would stop playing. Is it possible and how? I have video tag here
<video id="video" controls="" loop="" src="/wp-content/uploads/2016/06/vid/elipsiniai-treniruokliai.mp4" width="auto" height="auto" alt=""></video>
Share
Improve this question
asked Nov 19, 2017 at 20:43
Neimantas JociusNeimantas Jocius
1091 gold badge1 silver badge7 bronze badges
4 Answers
Reset to default 6Update
A fix is added for the error that was thrown in the console when hovering over the videos (tested in chrome)
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
adding muted="muted"
attribute to the video tag fixes the problem. For details see this answer
Here you go
$(document).ready(function() {
$(".myvideos").on("mouseover", function(event) {
this.play();
}).on('mouseout', function(event) {
this.pause();
});
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video class="myvideos" controls="" loop="" src="http://vjs.zencdn/v/oceans.mp4" width="auto" height="auto" alt="" muted="muted"></video>
<video class="myvideos" controls="" loop="" src="http://vjs.zencdn/v/oceans.mp4" width="auto" height="auto" alt="" muted="muted"></video>
<video class="myvideos" controls="" loop="" src="http://vjs.zencdn/v/oceans.mp4" width="auto" height="auto" alt="" muted="muted"></video>
<video class="myvideos" controls="" loop="" src="http://vjs.zencdn/v/oceans.mp4" width="auto" height="auto" alt="" muted="muted"></video>
Try:
$("#video").hover(
()=>{ $(this).get(0).play(); },
()=>{ $(this).get(0).pause(); }
);
For this you have to use javascript. Easiest would be some kind of implementation with jQuery/javascript implementation.
You could also find this easily on the internet, this took me 3 seconds just by typing the "video on hover" in google: https://codepen.io/gil--/pen/bNxZWg
Html:
<div id="videosList">
<div class="video">
<video class="thevideo" loop preload="none">
<source src="https://giant.gfycat./VerifiableTerrificHind.mp4" type="video/mp4">
</video>
</div>
</div>
You need to watch for hover event on video tag.
var figure = $(".video").hover( hoverVideo, hideVideo );
Now you add a function that plays video on hover:
function hoverVideo(e) {
$('video', this).get(0).play();
}
And when user leaves the video you add another function:
function hideVideo(e) {
$('video', this).get(0).pause();
}
To make a video play/pause on hover, try this:
JS:
var figure = $(".video").hover(hoverVideo, hideVideo);
function hoverVideo(e) {
$('video', this).get(0).play();
}
function hideVideo(e) {
$('video', this).get(0).pause();
}
CSS:
video::-webkit-media-controls {
display:none !important;
}
本文标签: JavascriptHTMLJS How to playstop video on hoverStack Overflow
版权声明:本文标题:javascript - HTML, JS How to playstop video on hover - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741054244a2331478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论