自定义视频播放器

html+css: 

<!DOCTYPE html>
<html>
  <head lang="en">
    <meta charset="UTF-8" />
    <title></title>
    <style>
      html,
      body,
      div,
      span,
      applet,
      object,
      iframe,
      h1,
      h2,
      h3,
      h4,
      h5,
      h6,
      p,
      blockquote,
      pre,
      a,
      abbr,
      and,
      address,
      cite,
      code,
      del,
      dfn,
      em,
      img,
      ins,
      kbd,
      q,
      s,
      samp,
      small,
      strike,
      strong,
      sub,
      sup,
      tt,
      var,
      b,
      u,
      i,
      dl,
      dt,
      dd,
      ol,
      ul,
      li,
      fieldset,
      form,
      label,
      legend,
      table,
      caption,
      tbody,
      tfoot,
      thead,
      tr,
      th,
      td,
      article,
      aside,
      canvas,
      details,
      embed,
      figure,
      figcaption,
      footer,
      header,
      hgroup,
      menu,
      nav,
      output,
      ruby,
      section,
      summary,
      time,
      mark,
      audio,
      video,
      input,
      textarea,
      select {
        background: transparent;
        border: 0;
        font-size: 100%;
        margin: 0;
        outline: 0;
        padding: 0;
        vertical-align: baseline;
      }

      article,
      aside,
      details,
      figcaption,
      figure,
      footer,
      header,
      hgroup,
      menu,
      nav,
      section {
        display: block;
      }

      body {
        line-height: 1;
      }

      abbr[title],
      dfn[title] {
        border-bottom: 1px dotted;
        cursor: help;
      }

      blockquote,
      q {
        quotes: none;
      }

      blockquote:before,
      blockquote:after,
      q:before,
      q:after {
        content: none;
      }

      del {
        text-decoration: line-through;
      }

      hr {
        background: transparent;
        border: 0;
        clear: both;
        color: transparent;
        height: 1px;
        margin: 0;
        padding: 0;
      }

      mark {
        background-color: #ffffb3;
        font-style: italic;
      }

      input,
      select {
        vertical-align: middle;
      }

      ins {
        background-color: red;
        color: white;
        text-decoration: none;
      }

      ol,
      ul {
        list-style: none;
      }

      table {
        border-collapse: collapse;
        border-spacing: 0;
      }

      a {
        text-decoration: none;
      }

      body {
        margin: 0;
        padding: 0;
        background: url(images/bg.jpg) repeat;
      }

      .jp-video {
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        position: relative;

        -webkit-box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.3);
        -moz-box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.3);
        box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.3);
        background: #000;
      }

      .jp-video-360p {
        width: 570px;
        margin: 100px auto;
      }

      .jp-video-full {
        width: 480px;
        height: 270px;
        position: static !important;
        position: relative;
      }

      .jp-video-full .jp-jplayer {
        top: 0;
        left: 0;
        position: fixed !important;
        position: relative;
        /* Rules for IE6 (full-screen) */
        overflow: hidden;
        z-index: 1000;
      }

      .jp-video-full .jp-gui {
        position: fixed !important;
        position: static;
        /* Rules for IE6 (full-screen) */
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1000;
      }

      .jp-video-full .jp-interface {
        position: absolute !important;
        position: relative;
        /* Rules for IE6 (full-screen) */
        bottom: 0;
        left: 0;
        z-index: 1000;
      }

      .jp-interface {
        position: relative;
        width: 100%;
        height: 35px;

        background-image: -webkit-linear-gradient(
          top,
          rgb(242, 242, 242),
          rgb(209, 209, 209)
        );
        background-image: -moz-linear-gradient(
          top,
          rgb(242, 242, 242),
          rgb(209, 209, 209)
        );
        background-image: -o-linear-gradient(
          top,
          rgb(242, 242, 242),
          rgb(209, 209, 209)
        );
        background-image: -ms-linear-gradient(
          top,
          rgb(242, 242, 242),
          rgb(209, 209, 209)
        );
        background-image: linear-gradient(
          top,
          rgb(242, 242, 242),
          rgb(209, 209, 209)
        );
        filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, StartColorStr='#f2f2f2', EndColorStr='#d1d1d1');

        -webkit-box-shadow: inset 0px 1px 0px #f7f7f7,
          inset 0px -1px 0px #e2e2e2;
        -moz-box-shadow: inset 0px 1px 0px #f7f7f7, inset 0px -1px 0px #e2e2e2;
        box-shadow: inset 0px 1px 0px #f7f7f7, inset 0px -1px 0px #e2e2e2;
      }

      div.jp-controls-holder {
        clear: both;
        width: 570px;
        margin: 0 auto;

        position: relative;
        overflow: hidden;
      }

      a.jp-play,
      a.jp-pause {
        width: 40px;
        height: 35px;
        float: left;
        text-indent: -9999px;
        outline: none;
      }

      a.jp-play {
        background: url(images/sprite.png) 0 0 no-repeat;
      }

      a.jp-pause {
        background: url(images/sprite.png) -40px 0 no-repeat;
        display: none;
      }

      #separator {
        background-image: url(images/separator.png);
        background-repeat: no-repeat;
        width: 2px;
        height: 35px;
        float: left;
        margin-top: 7px;
        margin-right: 10px;
      }

      .jp-progress {
        background: #706d6d;

        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;

        -webkit-box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);
        -moz-box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);

        width: 280px;
        height: 10px;
        float: left;
        margin-top: 13px;
      }

      .jp-seek-bar {
        width: 0px;
        height: 100%;
        cursor: pointer;
      }

      .jp-seeking-bg {
        background: #575555;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
      }

      .jp-play-bar {
        background: url(images/play-bar.png) left repeat-x;
        width: 0px;
        height: 10px;
        position: relative;
        z-index: 9999;

        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
      }

      .jp-play-bar span {
        position: absolute;
        top: -3px;
        right: -12px;

        background: url(images/handle.png) no-repeat center;
        width: 16px;
        height: 17px;
      }

      .jp-current-time,
      .jp-duration {
        font-size: 11px;
        font-family: Arial;
        color: #444444;
        margin-top: 13px;
        float: left;
      }

      .jp-current-time {
        float: left;
        display: inline;
      }

      .jp-duration {
        float: left;
        display: inline;
        text-align: right;
      }

      .jp-video .jp-current-time {
        margin-left: 10px;
      }

      .jp-video .jp-duration {
        margin-right: 10px;
      }

      .time-sep {
        float: left;
        margin: 13px 3px 0 3px;

        font-size: 11px;
        font-family: Arial;
        color: #444444;
      }

      .jp-video a.jp-mute,
      .jp-video a.jp-unmute {
        text-indent: -9999px;
        float: left;

        height: 35px;
        outline: none;
      }

      .jp-mute {
        float: left;
        background: url(images/sprite.png) -80px 0 no-repeat;
        margin-top: 1px;
        margin-left: -10px;
        width: 35px;
      }

      a.jp-unmute {
        background: url(images/sprite.png) -115px 0 no-repeat;
        margin-top: 1px;
        margin-left: -13px;
        display: none;
        width: 38px;
      }

      .jp-volume-bar {
        float: left;
        margin-top: 13px;
        margin-right: 10px;
        overflow: hidden;
        width: 70px;
        height: 10px;
        cursor: pointer;

        background: #706d6d;

        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;

        -webkit-box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);
        -moz-box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);
        box-shadow: inset 0px 1px 4px rgba(0, 0, 0, 0.4),
          0px 1px 0px rgba(255, 255, 255, 0.4);
      }

      .jp-volume-bar-value {
        background: rgba(112, 109, 109, 1);
        width: 0px;
        height: 10px;
        position: relative;
        min-width: 11px;
      }

      .jp-volume-bar-value span {
        position: absolute;
        top: 0px;
        right: 0px;

        background: url(images/volume.png) no-repeat center;
        width: 11px;
        height: 10px;
      }

      .jp-full-screen {
        background: url(images/sprite.png) -150px 0 no-repeat;
        float: left;
        width: 40px;
        height: 35px;
        text-indent: -9999px;
        margin-left: -10px;
        outline: none;
      }

      .jp-restore-screen {
        background: url(images/sprite.png) -150px 0 no-repeat;
        float: left;
        width: 40px;
        height: 35px;
        text-indent: -9999px;
        margin-left: -15px;
        outline: none;
      }

      .box {
        width: 500px;
        margin: 0 auto;
        position: relative;
      }

      #video {
        width: 100%;
      }
    </style>
  </head>

  <body>
    <div class="box">
      <video id="video">
        <!-- <source src="video/video.webm" /> -->
        <!-- <source src="./video/video.mp4" /> -->
        <source src="./video/zhz.MP4" />
      </video>
      <div class="jp-gui">
        <div class="jp-interface">
          <div class="jp-controls-holder">
            <!--play and pause buttons-->
            <a
              href="javascript:;"
              class="jp-play"
              tabindex="1"
              style="display: block"
              >play</a
            >
            <a
              href="javascript:;"
              class="jp-pause"
              tabindex="1"
              style="display: none"
              >pause</a
            >
            <span class="separator sep-1"></span>

            <!--progress bar-->
            <div class="jp-progress">
              <div class="jp-seek-bar" style="width: 100%">
                <div class="jp-play-bar" style="width: 0%"><span></span></div>
              </div>
            </div>

            <!--time notifications-->
            <div class="jp-current-time" style="padding: 0 2px 0 5px">
              00:00
            </div>
            <span class="time-sep">/</span>

            <div class="jp-duration" style="padding: 0 5px 0 2px">00:00</div>
            <!--<span class="separator sep-2"></span>-->

            <!--mute / unmute toggle-->
            <!--<a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a>-->
            <!--<a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute" style="display: none;">unmute</a>-->

            <!--volume bar-->
            <div class="jp-volume-bar">
              <div class="jp-volume-bar-value" style="width: 90%">
                <span class="handle"></span>
              </div>
            </div>
            <!--<span class="separator sep-2"></span>-->

            <!--full screen toggle-->
            <a
              href="javascript:;"
              class="jp-full-screen"
              tabindex="1"
              title="full screen"
              >full screen</a
            >
            <!--<a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen"-->
            <!--style="display: none;">restore screen</a>-->
          </div>
          <!--end jp-controls-holder-->
        </div>
        <!--end jp-interface-->
      </div>
    </div>
    <script src="./js/my.js"></script>
  </body>
</html>

js:

// 获取整个视频
var video = document.getElementById("video");
// 获取播放按钮
var jpPlay = document.querySelector(".jp-play");
// 获取暂停按钮
var jpPause = document.querySelector(".jp-pause");
// 获取整个视频进度条
var jpSeekBar = document.querySelector(".jp-seek-bar");
// 获取视频进度条的按钮
var jpPlayBar = document.querySelector(".jp-play-bar");
// 获取视频已经播放的时间
var jpCurrentTime = document.querySelector(".jp-current-time");
// 获取视频的总时间
var jpDuration = document.querySelector(".jp-duration");
// 获取整个音量进度条
var jpVolumeBar = document.querySelector(".jp-volume-bar");
// 获取音量进度条的按钮
var jpVolumeBarValue = document.querySelector(".jp-volume-bar-value");
// 获取全屏按钮
var jpFullScreen = document.querySelector(".jp-full-screen");

// 视频点击播放
jpPlay.addEventListener("click", function () {
  video.play();
  jpPlay.style.display = "none";
  jpPause.style.display = "block";
});

// 视频点击暂停
jpPause.addEventListener("click", function () {
  video.pause();
  jpPause.style.display = "none";
  jpPlay.style.display = "block";
});

video.addEventListener("canplay", function () {
  // 视频总的时间
  jpDuration.innerHTML = timeFormatting(video.duration);
});

// 视频的时间比例
var timeScale;
video.addEventListener("timeupdate", function () {
  // 视频已经播放的时间
  jpCurrentTime.innerHTML = timeFormatting(video.currentTime);

  // 视频的时间比例
  timeScale = video.currentTime / video.duration;
  // 视频的播放按钮发生改变
  jpPlayBar.style.width = timeScale * 100 + "%";
});

// 点击进度条
jpSeekBar.addEventListener("click", function (e) {
  e = e || window.event;
  // 视频的播放比例
  var playScale = e.offsetX / jpSeekBar.offsetWidth;
  // 视频的播放按钮发生改变
  jpPlayBar.style.width = playScale * 100 + "%";
  // 视频的播放时间发生改变
  timeScale = playScale;
  video.currentTime = timeScale * video.duration;
});

// 点击音量
jpVolumeBar.addEventListener("click", function (e) {
  e = e || window.event;

  // 音量的比例
  volumeScale = e.offsetX / jpVolumeBar.offsetWidth;
  jpVolumeBarValue.style.width = volumeScale * 100 + "%";
  // 设置音量
  video.volume = volumeScale;
});

// 全屏和取消全屏
// 时间点击数值
var jpFullScreenNum = 0;
jpFullScreen.addEventListener("click", function () {
  jpFullScreenNum++;
  if (jpFullScreenNum % 2) {
    video.webkitRequestFullscreen();
  } else {
    video.webkitExitFullscreen();
  }
});

// 封装时间格式化函数
function timeFormatting(time) {
  // 分钟
  minutes = Math.floor(time / 60);
  minutes = minutes < 10 ? "0" + minutes : minutes;
  // 秒
  seconds = Math.floor(time % 60);
  seconds = seconds < 10 ? "0" + seconds : seconds;

  return minutes + ":" + seconds;
}

效果:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值