How to make custom video player applicable to multiple videos?

≡放荡痞女 提交于 2019-12-06 15:30:08

You can try something like this :

  /* Get Our Elements */

  $('.player').each(function() {
    var player = $(this).get(0);
    var video = player.querySelector('.viewer');
    var progress = player.querySelector('.progress');
    var progressBar = player.querySelector('.progress__filled');
    var toggle = player.querySelector('.toggle');
    var skipButtons = player.querySelectorAll('[data-skip]');
    var ranges = player.querySelectorAll('.player__slider');

    /* Build out functions */
    function togglePlay() {
      const method = video.paused ? 'play' : 'pause';
      video[method]();
    }

    function updateButton() {
      const icon = this.paused ? '►' : '❚❚';
      toggle.textContent = icon;
    }

    function skip() {
      video.currentTime += parseFloat(this.dataset.skip);
    }

    function handleRangeUpdate() {
      video[this.name] = this.value;
    }

    function handleProgress() {
      const percent = (video.currentTime / video.duration) * 100;
      progressBar.style.flexBasis = `${percent}%`;
    }

    function scrub(e) {
      const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
      video.currentTime = scrubTime;
    }

    /* Hook up the event listners */
    video.addEventListener('click', togglePlay);
    video.addEventListener('play', updateButton);
    video.addEventListener('pause', updateButton);
    video.addEventListener('timeupdate', handleProgress);

    toggle.addEventListener('click', togglePlay);
    skipButtons.forEach(button => button.addEventListener('click', skip));
    ranges.forEach(range => range.addEventListener('change', handleRangeUpdate));
    ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate));

    let mousedown = false;
    progress.addEventListener('click', scrub);
    progress.addEventListener('mousemove', (e) => mousedown && scrub(e));
    progress.addEventListener('mousedown', () => mousedown = true);
    progress.addEventListener('mouseup', () => mousedown = false);


    $('video').on('ended', function() {
      $.fn.fullpage.moveSlideRight();
    });
  });

https://jsfiddle.net/kq5hdw0m/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!