最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I playpause more than one video by mouseover - Stack Overflow

programmeradmin6浏览0评论

I have a page with many videos and wish to play each video on mouseOver and pause on mouseOut.

It is working with video1, but I want to work with video2 and so on.

<!DOCTYPE html> 
<html> 
<body> 

<div style="text-align:center"> 
  <video id="video1" width="420" onmouseover="playPause()" onmouseout="playPause()">
    <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()" onmouseout="playPause()">
    <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> 
var myVideo=document.getElementById("video1"); 

function playPause()
{ 
if (myVideo.paused) 
  myVideo.play(); 
else 
  myVideo.pause(); 
} 
</script> 

</body> 
</html>

I have a page with many videos and wish to play each video on mouseOver and pause on mouseOut.

It is working with video1, but I want to work with video2 and so on.

<!DOCTYPE html> 
<html> 
<body> 

<div style="text-align:center"> 
  <video id="video1" width="420" onmouseover="playPause()" onmouseout="playPause()">
    <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()" onmouseout="playPause()">
    <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> 
var myVideo=document.getElementById("video1"); 

function playPause()
{ 
if (myVideo.paused) 
  myVideo.play(); 
else 
  myVideo.pause(); 
} 
</script> 

</body> 
</html>
Share Improve this question edited Oct 3, 2013 at 13:06 user529758 asked Oct 3, 2013 at 13:00 EyesisEyesis 1911 gold badge1 silver badge5 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

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> 

Use this code and enjoy! ////HTML

    <div class="hov">
        <div id="video1" class="sel">
            <video width=640  src="assets/videos/ve.mp4"></video>
            <div class="controls" style="display:none">
              <button>Play</button>
            </div>
        </div>
    </div>

//style.css

.sel {
  position:relative;
  display:inline-block;
  font-size:16px;
  z-index:0;
  }

  .sel > .controls:hover {
  position:absolute;
  width:100%;
  background:rgba(255,255,255,0.3);
  padding:7px;
  box-sizing:content-box;
  z-index:10000;
  }

  .sel > .controls{
  position:absolute;
  bottom:0;
  width:100%;
  background:rgba(255,255,255,0.3);
  padding:7px;
  box-sizing:content-box;
  z-index:10000;
  top: 150px;
  }

//script

// self hosted video controler//
var $sel = $("#video1");
var $video = $sel.children("video"), video = $video[0]
var $controls = $sel.children(".controls");
var $play = $controls.children("button");

// control visibility
$sel.on("mouseover mouseout", function(e) {
    $controls.css("display", e.type === "mouseout" && video.paused ? "none" : "block");
    $controls.css("display", e.type === "mouseover" && video.play ? "block" : "none");
});

// play or pause
$play.on("click", toggle);
$video.on("click", toggle);

function toggle() {
    video[video.paused ? "play" : "pause"]();
}

// todo: cover more events (seeked, error etc.)
$video.on("play pause ended", updateUI);

// update control UI elements (todo: update time/progress etc.)
function updateUI() {
    $play.text(video.paused ? "Play" : "Pause")
}
发布评论

评论列表(0)

  1. 暂无评论