【问题标题】:How to disable default video controls and show custom seekbar如何禁用默认视频控件并显示自定义搜索栏
【发布时间】:2018-06-14 06:38:12
【问题描述】:

我在这里处理视频我想使用 jquery 隐藏默认视频控件并显示我的自定义搜索栏。我试图喜欢这个controls=false,但如果我禁用它,我的自定义栏也会禁用。我想定制酒吧和播放按钮。我不想使用任何插件。 谁能建议我正确的方法?

var vid = document.getElementById("video");
vid.ontimeupdate = function(){
  var percentage = ( vid.currentTime / vid.duration ) * 100;
  $("#custom-seekbar span").css("width", percentage+"%");
};

$("#custom-seekbar").on("click", function(e){
    var offset = $(this).offset();
    var left = (e.pageX - offset.left);
    var totalWidth = $("#custom-seekbar").width();
    var percentage = ( left / totalWidth );
    var vidTime = vid.duration * percentage;
    vid.currentTime = vidTime;
});//click()
#custom-seekbar
{  
  cursor: pointer;
  height: 10px;
  margin-bottom: 10px;
  outline: thin solid orange;
  overflow: hidden;
  position: relative;
  width: 400px;
}
#custom-seekbar span
{
  background-color: orange;
  position: absolute;
  top: 0;
  left: 0;
  height: 10px;
  width: 0px;
}

/* following rule is for hiding Stack Overflow's console  */
.as-console-wrapper{ display: none !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="custom-seekbar">
  <span></span>
</div>
<video id="video" width="400" controls autoplay>
    <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>

【问题讨论】:

    标签: javascript jquery html5-video


    【解决方案1】:

    出现此错误是因为您没有在一般部分定义percentage 变量。见下面的代码sn-p:

    var percentage = 0;
    
    video.ontimeupdate = function(){
      percentage = ( video.currentTime / video.duration ) * 100;
      $( '#custom-seekbar span' ).css( 'width', percentage + '%' )
    }
    
    $( '#custom-seekbar' ).on( 'click', function( e ){
      var offset = $( this ).offset(),
          left = ( e.pageX - offset.left ),
          totalWidth = $( '#custom-seekbar' ).width(),
          percentage = ( left / totalWidth );
    
      video.currentTime = video.duration * percentage;
    })
    #custom-seekbar {
      cursor: pointer;
      width: 400px;
      height: 10px;
      margin-bottom: 5px;
      border: thin solid orange;
      overflow: hidden;
      position: relative;
      border-radius: 5px
    }
    #custom-seekbar span {
      background-color: orange;
      position: absolute;
      top: 0;
      left: 0;
      height: 10px;
      width: 0;
      transition-duration: 0.2s
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="custom-seekbar">
        <span></span>
    </div>
    <video id="video" width="400" autoplay>
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
    </video>

    更新:添加播放、暂停和重复按钮。

    var percentage = 0;
    
    video.ontimeupdate = function(){
      percentage = ( video.currentTime / video.duration ) * 100;
      $( '#custom-seekbar span' ).css( 'width', percentage + '%' );
    
      if ( percentage >= 100 ) $( '#play_button' ).html( '⥀' ) /* Repeat */
    }
    
    $( '#custom-seekbar' ).on( 'click', function( e ){
      var offset = $( this ).offset(),
          left = ( e.pageX - offset.left ),
          totalWidth = $( this ).width(),
          percentage = ( left / totalWidth );
    
      video.currentTime = video.duration * percentage;
    })
    
    $( '#play_button' ).on( 'click', function() {
      if ( video.paused ) {
        video.play();
        $( this ).html( '&#10074;&#10074;' ) /* Pause */
      } else {
        video.pause();
        $( this ).html( '&#9658;' ) /* Play */
      }
    } )
    .player {
      position: relative;
      width: 400px;
      margin: 0 auto
    }
    #video {
        width: 100%
    }
    #custom-seekbar {
      position: absolute;
      top: 0;
      cursor: pointer;
      height: 7px;
      width: 100%;
      background-color: rgba(0, 0, 0, .2);
    }
    #custom-seekbar span {
      position: absolute;
      top: 0;
      left: 0;
      height: 7px;
      width: 0;
      background-color: rgba(255, 165, 0, .8);
      transition-duration: 0.2s
    }
    #play_button {
      position: absolute;
      width: 25px;
      height: 25px;
      left: 6px;
      top: 10px;
      text-align: center;
      line-height: 25px;
      background-color: rgba(255, 165, 0, .8);
      border: none;
      color: #555;
      cursor: pointer;
      border-radius: 5px;
      transition-duration: 0.3s
    }
    #play_button:hover,
    #custom-seekbar:hover span {
        background-color: #ff8605
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="player">
      <video id="video">
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
      </video>
      <div id="custom-seekbar">
        <span></span>
      </div>
      <button type="button" id="play_button" title="Play / Pause">&#9658;</button>
    </div>

    【讨论】:

    • 这就是我想要的,用简单的js你就实现了。
    • 重启符号是怎么写的?
    猜你喜欢
    • 2016-09-04
    • 2017-09-22
    • 1970-01-01
    • 2017-06-16
    • 2012-12-15
    • 2012-09-10
    • 2021-11-06
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多