How can I play/pause more than one video by mouseover
Based on the answer of Etienne Miret a minimal implementation does not need a specific function at all.
Simply setting onmouseover="this.play()"
and onmouseout="this.pause()"
should do the trick:
<div style="text-align:center">
<video id="video1" width="420" onmouseover="this.play()" onmouseout="this.pause()">
<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
<video id="video2" width="420" onmouseover="this.play()" onmouseout="this.pause()">
<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
</div>
Use the this
keyword:
onmouseover="playPause(this)"
and in your Javascript:
function playPause(video) {
if (video.paused) {
video.play();
} else {
video.pause();
}
}
You need to pass a reference to the video you want to play/pause. f.ex:
<div style="text-align:center">
<video id="video1" width="420" onmouseover="playPause('video1')" onmouseout="playPause('video1')">
<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
<video id="video2" width="420" onmouseover="playPause('video2')" onmouseout="playPause('video2')">
<source src="mov_bbb.mp4" type="video/mp4">
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
</div>
<script>
function playPause(videoID)
{
var myVideo=document.getElementById(videoID);
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
</script>