vue实现拖拽进度条


Posted in Vue.js onMarch 01, 2021

本文实例为大家分享了vue实现拖拽进度条的具体代码,供大家参考,具体内容如下

vue实现拖拽进度条

组件代码:

<template>
 <div>
  <div class="slider" ref="slider">
   <div class="process" :style="{ width }"></div>
   <div class="thunk" ref="trunk" :style="{ left }">
    <div class="block"></div>
    <div class="tips">
     <!-- <span>{{scale*100}}</span> -->
     <i class="fas fa-caret-down"></i>
    </div>
   </div>
  </div>
  <div>
   <button
    @click="
     () => {
      this.per++;
     }
    "
   >
    +</button
   >{{ per }}%<button
    @click="
     () => {
      if (this.per > 0) {
       this.per--;
      }
     }
    "
   >
    -
   </button>
  </div>
 </div>
</template>
<script>
/*
 * min 进度条最小值
 * max 进度条最大值
 * v-model 对当前值进行双向绑定实时显示拖拽进度
 * */
export default {
 props: ["min", "max", "value"],
 data() {
  return {
   slider: null, //滚动条DOM元素
   thunk: null, //拖拽DOM元素
   per: this.value, //当前值
  };
 },
 //渲染到页面的时候
 mounted() {
  this.slider = this.$refs.slider;
  this.thunk = this.$refs.trunk;
  var _this = this;
  this.thunk.onmousedown = function (e) {
   var width = parseInt(_this.width);
   var disX = e.clientX;
   document.onmousemove = function (e) {
    // value, left, width
    // 当value变化的时候,会通过计算属性修改left,width

    // 拖拽的时候获取的新width
    var newWidth = e.clientX - disX + width;
    // 拖拽的时候得到新的百分比
    var scale = newWidth / _this.slider.offsetWidth;
    _this.per = Math.ceil((_this.max - _this.min) * scale + _this.min);
    _this.per = Math.max(_this.per, _this.min);
    _this.per = Math.min(_this.per, _this.max);
    _this.$emit("input", _this.per);
   };
   document.onmouseup = function () {
    document.onmousemove = document.onmouseup = null;
   };
   return false;
  };
 },
 computed: {
  // 设置一个百分比,提供计算slider进度宽度和trunk的left值
  // 对应公式为 当前值-最小值/最大值-最小值 = slider进度width / slider总width
  // trunk left = slider进度width + trunk宽度/2
  scale() {
   return (this.per - this.min) / (this.max - this.min);
  },
  width() {
   if (this.slider) {
    return this.slider.offsetWidth * this.scale + "px";
   } else {
    return 0 + "px";
   }
  },
  left() {
   if (this.slider) {
    return (
     this.slider.offsetWidth * this.scale -
     this.thunk.offsetWidth / 2 +
     "px"
    );
   } else {
    return 0 + "px";
   }
  },
 },
};
</script>
<style>
.box {
 margin: 100px auto 0;
 width: 80%;
}
.clear:after {
 content: "";
 display: block;
 clear: both;
}
.slider {
 user-select: none;
 position: relative;
 margin: 20px 0;
 width: 400px;
 height: 10px;
 background: #e4e7ed;
 border-radius: 5px;
 cursor: pointer;
}
.slider .process {
 position: absolute;
 left: 0;
 top: 0;
 width: 112px;
 height: 10px;
 border-radius: 5px;
 background: #81b159;
}
.slider .thunk {
 position: absolute;
 left: 100px;
 top: -7px;
 width: 20px;
 height: 20px;
}
.slider .block {
 width: 20px;
 height: 20px;
 border-radius: 50%;
 border: 2px solid #409eff;
 background: rgba(255, 255, 255, 1);
 transition: 0.2s all;
}
.slider .tips {
 position: absolute;
 left: -7px;
 bottom: 30px;
 min-width: 15px;
 text-align: center;
 padding: 4px 8px;
 /* background: #000; */
 border-radius: 5px;
 height: 24px;
 color: #fff;
}
.slider .tips i {
 position: absolute;
 margin-left: -5px;
 left: 50%;
 bottom: -9px;
 font-size: 16px;
 color: #000;
}
.slider .block:hover {
 transform: scale(1.1);
 opacity: 0.6;
}
</style>

调用:

<template>
 <slider :min="0" :max="100" v-model="per"></slider>
</template>

<script>
import slider from "@/components/slider";
export default {
 data() {
  return {};
 },
 computed: {
  per: {
   get() {
    return 0;
   },
   set(val) {
    console.log(val);
   },
  },
 },
 components: { slider },
 mounted() {},
 methods: {},
};
</script>

<style >
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
VUE项目实现主题切换的多种方法
Nov 26 Vue.js
VUE中鼠标滚轮使div左右滚动的方法详解
Dec 14 Vue.js
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
Dec 25 Vue.js
vue中父子组件的参数传递和应用示例
Jan 04 Vue.js
vue3.0中友好使用antdv示例详解
Jan 05 Vue.js
Vue中使用wangeditor富文本编辑的问题
Feb 07 Vue.js
vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件
Feb 20 Vue.js
Vue项目中如何封装axios(统一管理http请求)
May 02 Vue.js
一文带你理解vue创建一个后台管理系统流程(Vue+Element)
May 18 Vue.js
Vue过滤器(filter)实现及应用场景详解
Jun 15 Vue.js
如何优化vue打包文件过大
Apr 13 Vue.js
vue里使用create, mounted调用方法
Apr 26 Vue.js
vue 使用 v-model 双向绑定父子组件的值遇见的问题及解决方案
Mar 01 #Vue.js
vue前端和Django后端如何查询一定时间段内的数据
Feb 28 #Vue.js
vue-router路由懒加载及实现的3种方式
Feb 28 #Vue.js
vue-router懒加载的3种方式汇总
Feb 28 #Vue.js
Vue SPA 首屏优化方案
Feb 26 #Vue.js
vue 动态添加的路由页面刷新时失效的原因及解决方案
Feb 26 #Vue.js
vue项目配置 webpack-obfuscator 进行代码加密混淆的实现
Feb 26 #Vue.js
You might like
[原创]CI(CodeIgniter)简单统计访问人数实现方法
2016/01/19 PHP
php集成开发环境详解
2019/09/24 PHP
php的无刷新操作实现方法分析
2020/02/28 PHP
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
Jquery解析json字符串及json数组的方法
2015/05/29 Javascript
javacript获取当前屏幕大小
2016/06/04 Javascript
js注入 黑客之路必备!
2016/09/14 Javascript
利用jQuery对无序列表排序的简单方法
2016/10/16 Javascript
js仿京东轮播效果 选项卡套选项卡使用
2017/01/12 Javascript
详解node HTTP请求客户端 - Request
2017/05/05 Javascript
JS实现点击Radio动态更新table数据
2017/07/18 Javascript
angularjs实现简单的购物车功能
2017/09/21 Javascript
详解webpack 打包文件体积过大解决方案(code splitting)
2018/04/10 Javascript
React styled-components设置组件属性的方法
2018/08/07 Javascript
详解easyui 切换主题皮肤
2019/04/04 Javascript
Vue+Spring Boot简单用户登录(附Demo)
2020/11/12 Javascript
Python的Django框架中TEMPLATES项的设置教程
2015/05/29 Python
EM算法的python实现的方法步骤
2018/01/02 Python
python绘制立方体的方法
2018/07/02 Python
python读csv文件时指定行为表头或无表头的方法
2019/06/26 Python
Python简单处理坐标排序问题示例
2019/07/11 Python
Django项目使用CircleCI的方法示例
2019/07/14 Python
解决python中的幂函数、指数函数问题
2019/11/25 Python
耐克美国官网:Nike.com
2016/08/01 全球购物
了解AppleShare protocol(AppleShare协议)吗
2015/08/28 面试题
中专生学习生活的自我评价分享
2013/10/27 职场文书
三年级语文教学反思
2014/02/01 职场文书
关于期中考试的反思
2014/02/02 职场文书
篮球比赛拉拉队口号
2014/06/10 职场文书
大二学生自我检讨书
2014/10/23 职场文书
乡镇2014法制宣传日活动总结
2014/11/01 职场文书
廉洁自律证明
2015/06/24 职场文书
小学英语教学随笔
2015/08/14 职场文书
自愿离婚协议书范本2016
2016/03/18 职场文书
JS setTimeout与setInterval的区别
2022/04/20 Javascript
Windows Server 2012配置DNS服务器的方法
2022/04/29 Servers