如何使videojs标记可滑动或可移动

How to make videojs marker slidable or movable

我想移动我的 markers 每当它随着搜索滑动时。我希望我的标记与 jqueryui-slider

完全相同 slidable

问题: 我希望我的 markers(两者)与 jqueryui-range 滑块一样可滑动,如下例中的视频所示:

var player = videojs('example_video_1');

function markplayer(){
var inTimeOutTimeList = [6.333,27.667];
        for(var i = 0; i < inTimeOutTimeList.length; i++){
            player.markers.add([{
            time: inTimeOutTimeList[i],
            text: inTimeOutTimeList[i]
        }]);

        var icon = (i == 0) ? '[' : ']';
        $(".vjs-marker[data-marker-time='"+inTimeOutTimeList[i]+"']").html(icon);
    }
};

player.markers({
   breakOverlay:{
      display: true,
      displayTime: 120,
      style:{
         'width':'100%',
         'height': '30%',
         'background-color': 'rgba(10,10,10,0.6)',
         'color': 'white',
         'font-size': '16px'
      }
   },
   markers: [
      {time:10, startTime:10, endTime:60, text: "this", overlayText: "1", class: "special-blue"},
   ]
});

setTimeout(function(){
 markplayer();    
},2000);

 $( "#slider-range" ).slider({
      range: true,
      min: 0,
      max: 500,
      values: [ 75, 300 ],
      slide: function( event, ui ) {
        $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
      }
    });
.vjs-fluid {
            overflow: hidden;
         }
         #example_video_1 .vjs-control-bar {
            display: block;
         }
         #example_video_1 .vjs-progress-control {
               bottom: 28px;
               left: 0;
               height: 10px;
               width: 100%;
         }
         
    .vjs-default-skin.vjs-has-started .vjs-control-bar {
            display: block !important;
            visibility: visible !important;
            opacity: 1 !important;
         }

.vjs-marker {
            background-color: transparent !important;
            height: 20px !important;
            font-size: 20px !important;
            color: red !important;
            font-weight: bold;
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://vjs.zencdn.net/4.2/video.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs-markers.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="http://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs.markers.min.css" rel="stylesheet"/>

<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>



<video id="example_video_1" width="400" height="210" controls class="video-js vjs-default-skin" data-setup='{ "inactivityTimeout": 0 }'>
   <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
   <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
</video>


<p><b>I want both of my red markers to be movable/slidable like below slider</b></p>


<div id="slider-range"></div>

请帮助我提前谢谢!!!

首先,使用 jQuery UI 的内置方法使您的标记可拖动。

$('body').on('mousedown', '.vjs-marker', function(e) {
    $(e.target).draggable({
      axis: 'x',
      containment: '.vjs-progress-control',
    });
}).on('mouseup', function(e) {
    $('.vjs-marker').css('top','-8px');   
});

由于标记想要向上移动到进度条的顶部边缘,我在 mouseup 上给了它一个 top: -8px

然后,将 onMarkerClick 方法添加到您的 player.markers() 调用以更新时间。

onMarkerClick: function(marker) {    
    marker.time = player.currentTime();
    player.markers.updateTime();
},

而且我注意到您在 10 秒时添加了一个空白标记,所以我将其替换为一个空数组。

markers: []

最后,我添加了一些 Prev 和 Next 按钮,这样您就可以看到标记在滑动后起作用了。

编辑:

查看 后,我意识到我的解决方案缺少一些东西。与范围滑块示例不同,我的标记可以相互交叉。

所以我用draggable对象的drag属性来限制标记的范围

...    
drag: function( e, ui ) {        
  if ( $(e.target).index('.vjs-marker') === 0 ) {
    const outPosition = parseInt( $('.vjs-slider .vjs-marker:eq(1)').css('left') );          
    ui.position.left = Math.min( outPosition, ui.position.left );
  }
  else {
    const inPosition = parseInt( $('.vjs-slider .vjs-marker:eq(0)').css('left') );
    ui.position.left = Math.max( inPosition, ui.position.left );        
  }                
}
...

var player = videojs('example_video_1');

function markplayer() {
  var inTimeOutTimeList = [1.2, 4.2];
  var labelList = ['In Point', 'Out Point'];
  for (var i = 0; i < inTimeOutTimeList.length; i++) {
    player.markers.add([{
      time: inTimeOutTimeList[i],
      text: labelList[i]
    }]);

    var icon = (i == 0) ? '[' : ']';
    $(".vjs-marker[data-marker-time='" + inTimeOutTimeList[i] + "']").html(icon);
  }
};

player.markers({
  breakOverlay: {
    display: true,
    displayTime: 120,
    style: {
      'width': '100%',
      'height': '30%',
      'background-color': 'rgba(10,10,10,0.6)',
      'color': 'white',
      'font-size': '16px'
    }
  },
  onMarkerClick: function(marker, index) {
    marker.time = player.currentTime();
    player.markers.updateTime();
  },
  markers: []
});

setTimeout(function() {
  markplayer();
}, 2000);

$(function() {
  $('body').on('mousedown', '.vjs-marker', function(e) {
    $(e.target).draggable({
      axis: 'x',
      containment: '.vjs-progress-control',
      drag: function(e, ui) {
        if ($(e.target).index('.vjs-marker') === 0) {
          const outPosition = parseInt( $('.vjs-slider .vjs-marker:eq(1)').css('left') );
          ui.position.left = Math.min(outPosition, ui.position.left);
        } else {
          const inPosition = parseInt( $('.vjs-slider .vjs-marker:eq(0)').css('left') );
          ui.position.left = Math.max(inPosition, ui.position.left);
        }
      }
    });
  }).on('mouseup', function(e) {
    $('.vjs-marker').css('top', '-8px');
  });

  $("#prev").click(function() {
    player.markers.prev();
  });
  $("#next").click(function() {
    player.markers.next();
  });
});
body {
  text-align: center;
}

#example_video_1 {
  margin: auto;
}

#buttons {
  margin-top: 1em;
}

.vjs-fluid {
  overflow: hidden;
}

#example_video_1 .vjs-control-bar {
  display: block;
}

#example_video_1 .vjs-progress-control {
  bottom: 28px;
  left: 0;
  height: 10px;
  width: 100%;
}

.vjs-default-skin.vjs-has-started .vjs-control-bar {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

.vjs-marker {
  background-color: transparent !important;
  height: 20px !important;
  font-size: 20px !important;
  color: red !important;
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://vjs.zencdn.net/4.2/video.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs-markers.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs.markers.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />

<video id="example_video_1" width="400" height="210" controls class="video-js vjs-default-skin" data-setup='{ "inactivityTimeout": 0 }'>
    <source src="https://interactive-examples.mdn.mozilla.net/media/examples/flower.mp4" type="video/mp4">
    <source src="https://interactive-examples.mdn.mozilla.net/media/examples/flower.webm" type="video/webm">
 </video>

<div id="buttons">
  <button id="prev">Previous</button> | <button id="next">Next</button>
</div>

我刚刚在你之前的 slide 事件中添加了几行,我使用了这个事件的 .values() 来获取开始和结束值,然后做了一个 marker.reset() 来添加新标记

...
slide: function twsr(event, ui) {
  // $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
  player.markers.reset([
    {
      time: ui.values[0],
      text: "this",
      overlayText: "1",
      class: "special-blue"
    },
    {
      time: ui.values[1],
      text: "this",
      overlayText: "1",
      class: "special-blue"
    }
  ]);
  for (var i = 0; i < ui.values.length; i++) {
    var icon = i == 0 ? "[" : "]";
    $(".vjs-marker[data-marker-time='" + ui.values[i] + "']").html(icon);
  }
}
...

注意:我对setTimeOut感觉不太好,以后有空再去看看。

如果我答对了你的问题,这就是你正在寻找的实现,工作片段:

$(document).ready(function() {
  var player = videojs("example_video_1");

  function markplayer() {
    $("#slider-range").slider({
      range: true,
      min: 0,
      max: player.duration(),
      values: [6.333, 27.667],
      slide: function(event, ui) {
        // $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
        player.markers.reset([
          {
            time: ui.values[0],
            text: "this",
            overlayText: "1",
            class: "special-blue"
          },
          {
            time: ui.values[1],
            text: "this",
            overlayText: "1",
            class: "special-blue"
          }
        ]);
        for (var i = 0; i < ui.values.length; i++) {
          var icon = i == 0 ? "[" : "]";
          $(".vjs-marker[data-marker-time='" + ui.values[i] + "']").html(icon);
        }
      }
    });

    var inTimeOutTimeList = [6.333, 27.667];
    for (var i = 0; i < inTimeOutTimeList.length; i++) {
      player.markers.add([
        {
          time: inTimeOutTimeList[i],
          text: inTimeOutTimeList[i]
        }
      ]);

      var icon = i == 0 ? "[" : "]";
      $(".vjs-marker[data-marker-time='" + inTimeOutTimeList[i] + "']").html(
        icon
      );
    }
  }

  player.markers({
    breakOverlay: {
      display: true,
      displayTime: player.duration(),
      style: {
        width: "100%",
        height: "30%",
        "background-color": "rgba(10,10,10,0.6)",
        color: "white",
        "font-size": "16px"
      }
    },
    markers: [
      {
        time: 10,
        startTime: 10,
        endTime: 60,
        text: "this",
        overlayText: "1",
        class: "special-blue"
      }
    ]
  });

  setTimeout(function() {
    markplayer();
  }, 100);
});
.vjs-fluid {
  overflow: hidden;
}
#example_video_1 .vjs-control-bar {
  display: block;
}
#example_video_1 .vjs-progress-control {
  bottom: 28px;
  left: 0;
  height: 10px;
  width: 100%;
}

.vjs-default-skin.vjs-has-started .vjs-control-bar {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

.vjs-marker {
  background-color: transparent !important;
  height: 20px !important;
  font-size: 20px !important;
  color: red !important;
  font-weight: bold;
}
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs.markers.min.css" rel="stylesheet" />

    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
    <style>

    </style>

</head>

<body>
    <video id="example_video_1" width="400" height="210" controls class="video-js vjs-default-skin" data-setup='{ "inactivityTimeout": 0 }'>
        <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
        <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
    </video>

    <p><b>I want both of my red markers to be movable/slidable like below slider</b></p>

    <div id="slider-range"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://vjs.zencdn.net/4.2/video.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs-markers.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</body>

</html>

我真的很喜欢 所以我决定对其进行调整以满足问题的要求。

我只是将范围滑块直接移到进度条上并给了它 opacity: 0;

$(document).ready(function() {
  var player = videojs("example_video_1");

  function markplayer() {
    $("#slider-range").slider({
      range: true,
      min: 0,
      max: player.duration(),
      values: [6.333, 27.667],
      slide: function(event, ui) {
        // $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
        player.markers.reset([{
            time: ui.values[0],
            text: "this",
            overlayText: "1",
            class: "special-blue"
          },
          {
            time: ui.values[1],
            text: "this",
            overlayText: "1",
            class: "special-blue"
          }
        ]);
        for (var i = 0; i < ui.values.length; i++) {
          var icon = i == 0 ? "[" : "]";
          $(".vjs-marker[data-marker-time='" + ui.values[i] + "']").html(icon);
        }
      }
    });

    var inTimeOutTimeList = [6.333, 27.667];
    for (var i = 0; i < inTimeOutTimeList.length; i++) {
      player.markers.add([{
        time: inTimeOutTimeList[i],
        text: inTimeOutTimeList[i]
      }]);

      var icon = i == 0 ? "[" : "]";
      $(".vjs-marker[data-marker-time='" + inTimeOutTimeList[i] + "']").html(
        icon
      );
    }
  }

  player.markers({
    breakOverlay: {
      display: true,
      displayTime: player.duration(),
      style: {
        width: "100%",
        height: "30%",
        "background-color": "rgba(10,10,10,0.6)",
        color: "white",
        "font-size": "16px"
      }
    },
    markers: [{
      time: 10,
      startTime: 10,
      endTime: 60,
      text: "this",
      overlayText: "1",
      class: "special-blue"
    }]
  });

  setTimeout(function() {
    $('#slider-range').appendTo('#example_video_1');
    markplayer();
  }, 100);
});
.vjs-fluid {
  overflow: hidden;
}

#example_video_1 .vjs-control-bar {
  display: block;
}

#example_video_1 .vjs-progress-control {
  bottom: 28px;
  left: 0;
  height: 10px;
  width: 100%;
}

#example_video_1 #slider-range {
  bottom: 29px;
  position: absolute;
  width: 100%;
  z-index: 101;
  opacity: 0;
}

.vjs-default-skin.vjs-has-started .vjs-control-bar {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

.vjs-marker {
  background-color: transparent !important;
  height: 20px !important;
  font-size: 20px !important;
  color: red !important;
  font-weight: bold;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link href="http://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet" />
  <link href="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs.markers.min.css" rel="stylesheet" />

  <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
  <style>

  </style>

</head>

<body>
  <video id="example_video_1" width="400" height="210" controls class="video-js vjs-default-skin" data-setup='{ "inactivityTimeout": 0 }'>
        <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
        <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm">
    </video>


  <div id="slider-range"></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="http://vjs.zencdn.net/4.2/video.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-markers/0.7.0/videojs-markers.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</body>

</html>