完成播放后如何卸载视频?

时间:2016-11-23 21:22:05

标签: javascript jquery css

我正在尝试在完成播放后卸载视频。到目前为止没有运气。有人可以帮忙吗? JSFIDDLE example here

$(document).ready(function(){
    $('#video').on('ended',function(){
      $('#video').get(0).unload();
    });
});

1 个答案:

答案 0 :(得分:1)

我已将解决方案包含在jsfiddle和下面的代码段中。请注意,我使用.toggle()而不是.unload(),因为.unload()意味着在窗口事件上触发:

https://jsfiddle.net/firelite/h4aqkgo1/



$('#videoButton').on('click', function() {
  var videoDiv = $('#videoDiv').toggle();

  if (videoDiv.is(':visible')) {
    $('#video').get(0).load();
    $('#video').get(0).play();
  } else {
    $('#video').get(0).pause();
  }
});

$(document).ready(function() {
  $('#video').on('ended', function() {
    $('#video').get(0).pause();
    $('#videoDiv').toggle();
  });
});

#videoDiv {
  width: 100%;
  height: 360px;
  position: relative;
}
#videoBlock,
#videoMessage {
  width: 100%;
  height: 360px;
  position: absolute;
  top: 0;
  left: 0;
}
.videoClick {
  text-align: center
}
.videoClick a {
  color: white;
  background-color: rgba(241, 241, 241, 0.25);
  font-size: 1.7em;
  cursor: pointer;
  cursor: hand
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="videoButton">Toggle video</button>
<div id="videoDiv" style="display:none">
  <div id="videoBlock">
    <video preload="preload" id="video">
      <source src="https://static.webshopapp.com/shops/054833/files/093143183/fotel-photography-course-tour-workshop-video-4.mp4" type="video/mp4">
    </video>
  </div>
</div>
&#13;
&#13;
&#13;