js制作自定义控件——进度条的拖动和点击

就只写进度条了

HTML部分

<div class="progress">
  <div class="progress_bg">
    <div class="video_progress"></div>
  </div>
  <div class="progress_btn"></div>
</div>

CSS部分(用less写的)

// 进度条
.progress {
  position: relative;
  bottom: 65px;
  width: 1200px;
  .progress_bg {
    height: 3px;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 0.3);
    transform: scale(1);
    transition: 0.2s;
    .video_progress {
      background-color: #31c27c;
      width: 0;
      // height: 3px;
      height: 100%;
    }
  }
  .progress_btn {
    width: 13px;
    height: 13px;
    position: absolute;
    background-color: #fff;
    left: 0px;
    top: -4px;
    margin-left: -9px;
    cursor: pointer;
    border-radius: 50%;
    transform: scale(0);
    // transition: 0.2s;
  }
}

JS部分

(function () {
    $(".progress").on("click", function (e) {
      e.stopPropagation();
    })
    $(".player_mask").on("click",function (e) {
      e.stopPropagation();
    })
    // 进度条的hover效果
    $(".progress").hover(function () {
      $(".progress_bg").css({
        "transform": "scaleY(2)"
      });
      $(".progress_btn").fadeIn();
      $(".progress_btn").css("transform", "scale(1)");
    }, function () {
      $(".progress_bg").css("transform", "scaleY(1)");
      $(".progress_btn").css("transform", "scale(0)")
    })
  })();


$(function () {
  var tag = false,
    ox = 0,
    left = 0,
    bgleft = 0;
  $('.progress_btn').mousedown(function (e) {
    ox = e.pageX - left;
    console.log(ox);
    tag = true;
  });
  $(document).mouseup(function () {
    tag = false;
  });
  $('.progress').mousemove(function (e) { //鼠标移动
    if (tag) {
      left = e.pageX - ox;
      if (left <= 0) {
        left = 0;
      } else if (left > 1200) {
        left = 1200;
      }
      console.log(left);
      $('.progress_btn').css('left', left);
      $('.video_progress').width(left);
      $("video")[0].currentTime = $("video")[0].duration * left / 1200;
      $('.text').html(parseInt((left / 1200) * 100) + '%');
    }
  });
  $('.progress_bg').click(function (e) { //鼠标点击
    if (!tag) {
      bgleft = $('.progress_bg').offset().left;
      left = e.pageX - bgleft;
      console.log(left);
      if (left <= 0) {
        left = 0;
      } else if (left > 1200) {
        left = 1200;
      }
      $('.progress_btn').css('left', left);
      $('.video_progress').animate({
        width: left
      }, 0);
      $('.text').html(parseInt((left / 1200) * 100) + '%');
      $("video")[0].currentTime = $("video")[0].duration * left / 1200;
    }
  });
});

最后的效果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值