Skip to content

Instantly share code, notes, and snippets.

@robertcedwards
Created November 3, 2014 21:35
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save robertcedwards/fcec06dc0904a438c6a8 to your computer and use it in GitHub Desktop.
Save robertcedwards/fcec06dc0904a438c6a8 to your computer and use it in GitHub Desktop.
onmouseover html5 video play, mouseout pause
<html>
<head>
</head>
<body>
<div style="text-align:center" onmouseover="Play()" onmouseout="Pause()">
<video id="video1" width="480">
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/mp4" />
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.ogg" type="video/ogg" />
Your browser does not support HTML5 video.
</video>
</div>
<script type="text/javascript">
var myVideo=document.getElementById("video1");
function Pause()
{
myVideo.pause();
}
function Play()
{
if (myVideo.paused)
myVideo.play();
}
</script>
</body>
</html>
@codrinalex1994
Copy link

Nice , it works well. One thing can be improved: If you put this code line: onmouseover="Play()" onmouseout="Pause()" inside video tag and not inside div tag, it's better. The video will only starts if you have your cursor on it.

Copy link

ghost commented Mar 17, 2018

I tried this with more than one video, on my site and with the example. I never got it to work with both videos. I don't know javascript, so no big surprise.

I tried changing the id tag for the second one to video2 and adding a second script with the relevant change in the var but no luck.

How would I do this?

@aadityagoel
Copy link

I tried but it doesn't work. I also try to write id in the div section and it doesn't work.

@RaffaeleCrocco
Copy link

It works but you have to click inside the page at least one time before over the video, than it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment