如何通过鼠标悬停播放/暂停多个视频

时间:2013-10-03 13:00:19

标签: javascript html5

我有一个包含许多视频并希望播放的页面  每个视频都在mouseOver上,暂停在mouseOut

它正在使用video1,但我想使用video2等等。

<!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>

4 个答案:

答案 0 :(得分:8)

根据Etienne Miret的回答,最小的实现根本不需要特定的功能。

只需设置onmouseover="this.play()"onmouseout="this.pause()"即可:

<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> 

答案 1 :(得分:5)

使用this关键字:

onmouseover="playPause(this)"

并在您的Javascript中:

function playPause(video) {
    if (video.paused) {
        video.play();
    } else {
        video.pause();
    }
}

答案 2 :(得分:2)

您需要传递对要播放/暂停的视频的引用。 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> 

答案 3 :(得分:-1)

使用此代码并享受! ////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;
  }

//脚本

// 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")
}