自定义可拖拽进度条 ------ 2019-05-28

HTML代码

<!-- 滚动条 -->
  <div class="progress_bar" id="progress_bar">
    <!-- 红线 -->
    <div class="red_line"></div>
    <!-- 小球 -->
    <div class="ball" id="ball"></div>
  </div>

CSS代码

<style>
/* 滚动条样式 */
.progress_bar {
 margin-top: 19px;
 width: 592px;
 height: 3px;
 border-radius: 5px;
 position: relative;
}
/* 小球样式 */
.progress_bar .ball {
 width: 24px;
 height: 24px;
 border-radius: 24px;
 background: #ff4600;
 position: absolute;
 top: 50%;
 left: 26px;
 transform: translatey(-50%);
 animation: animate-positive2 0.5s;
 cursor: pointer;
}
/* 红线样式 */
.progress_bar .red_line {
 width: 26px;
 height: 3px;
 background: #ff4600;
 border-radius: 5px;
 animation: animate-positive 0.5s;
}
/* 动画 */
@-webkit-keyframes animate-positive {
 0% {
   width: 0%;
 }

}

@keyframes animate-positive {
 0% {
   width: 0%;
 }

}

@-webkit-keyframes animate-positive2 {
 0% {
   left: 0%;
 }

}

@keyframes animate-positive2 {
 0% {
   left: 0%;
 }

}
</style>

JS代码

<script>
  /**
   * btn 被拖动元素
   * bar 滚动条
   * title 暂时不用
   * **/
  let scale = function (btn, bar, title) {
    this.btn = document.getElementById(btn);//小球
    this.bar = document.getElementById(bar);//滚动条
    this.title = document.getElementById(title);
    this.step = this.bar.getElementsByTagName("DIV")[0];//滚动条上的红线
    this.init();
  };
  scale.prototype = {
    init: function () {
      var f = this, g = document, b = window, m = Math;
      f.btn.onmousedown = function (e) {//小球的鼠标按下事件
        var x = (e || b.event).clientX;//获取小球的距离屏幕左侧的距离
        console.log('小球的clientX', x);
        var l = this.offsetLeft;// 获取小球到滚动条左侧的距离
        var max = f.bar.offsetWidth - this.offsetWidth;// 滚动条的宽度减去小球的宽度之后剩下的距离
        g.onmousemove = function (e) {
          var thisX = (e || b.event).clientX;// 获取鼠标距离屏幕左侧的距离
          var to = m.min(max, m.max(-2, l + (thisX - x)));
          f.btn.style.left = to + 'px';
          f.ondrag(m.round(m.max(0, to / max) * 100), to);
          b.getSelection ? b.getSelection().removeAllRanges() : g.selection.empty();
        };
        g.onmouseup = new Function('this.onmousemove=null');
      };
    },
    ondrag: function (pos, x) {
      this.step.style.width = Math.max(0, x) + 'px';
    }
  }
  // 参数1:小球,参数2:父元素,
  new scale('ball', 'progress_bar', 'refer_title');
</script>

效果图


可拖拽进度条.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,296评论 1 45
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,692评论 1 92
  • 五一 . 采风 文:薄海岚 (一) 自从领着几个孩子一起写作文,就像得了职业病,每日想的最多的就怎样让孩子的笔下有...
    柳暗花明_5374阅读 1,774评论 2 1
  • 不再见过你 写了一段话给你 却删除在了草稿箱里 想了和你见面的场景 自己也勾起了回忆 你爱吃的零食脆皮 走路搞怪的...
    李封一村一枝花阅读 1,846评论 0 0
  • 这周回家见到阔别N个月的妈妈,不知从何时起,岁月让妈妈满头的青丝变成白发,曾经细嫩的皮肤也变得粗糙不光滑,我...
    天凉先暖心阅读 2,924评论 0 0

友情链接更多精彩内容