Reputation: 17
I was messing around to find a way to pause or mute a video, if we take off our cursor from it with using HTML5 and CSS3 only.But i was unable to find a way. Is there any way to achieve this ?
Upvotes: 1
Views: 99
Reputation: 171
Your HTML:
<div id="video-wrapper">
<video id="v" src="video.mp4" controls></video>
</div>
Getting video element using jQuery:
var v = $("#v");
Check if video is ready to play:
$(v).on('canplay', function(){
$(v).mouseenter(function(){
$(this).get(0).play();
}).mouseleave(function(){
$(this).get(0).pause();
})
});
Upvotes: 0
Reputation: 2575
If you're using jQuery and HTML5 video, you could do something like:
var vid = $(".myVideo");
vid.mouseleave(function() {
vid.pause()
});
Then, when you want to play again,
vid.mouseenter(function() {
vid.play()
});
The functions vid.play() and vid.pause() are built in so this shouldn't give you any trouble.
Upvotes: 1