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中如何自定义右键菜单详解
Dec 08 Vue.js
Vue实现点击当前行变色
Dec 14 Vue.js
vue3弹出层V3Popup实例详解
Jan 04 Vue.js
vue.js watch经常失效的场景与解决方案
Jan 07 Vue.js
Vue实现多页签组件
Jan 14 Vue.js
vue+echarts实现中国地图流动效果(步骤详解)
Jan 27 Vue.js
Vue SPA 首屏优化方案
Feb 26 Vue.js
vue2实现provide inject传递响应式
May 21 Vue.js
Vue vee-validate插件的简单使用
Jun 22 Vue.js
vue 给数组添加新对象并赋值
Apr 20 Vue.js
vue实现登陆页面开发实践
May 30 Vue.js
vue css 相对路径导入问题级踩坑记录
Jun 05 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
PHP中实现Bloom Filter算法
2015/03/30 PHP
php禁用cookie后session设置方法分析
2016/10/19 PHP
JavaScript 高级篇之函数 (四)
2012/04/07 Javascript
写出高效jquery代码的19条指南
2014/03/19 Javascript
jQuery on()方法示例及jquery on()方法的优点
2015/08/27 Javascript
jQuery根据表单name获取值的方法
2016/05/24 Javascript
BootStrap+Angularjs+NgDialog实现模式对话框
2016/08/24 Javascript
浅谈js常用内置方法和对象
2016/09/24 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
Angular JS 生成动态二维码的方法
2017/02/23 Javascript
基于Bootstrap分页的实例讲解(必看篇)
2017/07/04 Javascript
微信小程序引用公共js里的方法的实例详解
2017/08/17 Javascript
详解基于 Nuxt 的 Vue.js 服务端渲染实践
2017/10/24 Javascript
vue使用echarts图表的详细方法
2018/10/22 Javascript
Vue组件间通信方法总结(父子组件、兄弟组件及祖先后代组件间)
2019/04/17 Javascript
JavaScript生成一个不重复的ID的方法示例
2019/09/16 Javascript
基于JavaScript实现简单抽奖功能代码实例
2020/10/20 Javascript
vue绑定class的三种方法
2020/12/24 Vue.js
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
2014/04/15 Python
python strip() 函数和 split() 函数的详解及实例
2017/02/03 Python
python中利用Future对象回调别的函数示例代码
2017/09/07 Python
Python 模拟购物车的实例讲解
2017/09/11 Python
rabbitmq(中间消息代理)在python中的使用详解
2017/12/14 Python
《Python学习手册》学习总结
2018/01/17 Python
Python3实现的判断回文链表算法示例
2019/03/08 Python
Python 限定函数参数的类型及默认值方式
2019/12/24 Python
Python数据模型与Python对象模型的相关总结
2021/01/26 Python
selenium3.0+python之环境搭建的方法步骤
2021/02/01 Python
H5仿微信界面教程(一)
2017/07/05 HTML / CSS
h5调用摄像头的实现方法
2016/06/01 HTML / CSS
西部世纪.net笔试题面试题
2014/04/03 面试题
校园元旦活动总结
2014/07/09 职场文书
小学生美德少年事迹材料
2014/08/24 职场文书
离职报告范文
2014/11/04 职场文书
初中数学教学反思范文
2016/02/17 职场文书
《勇者辞职不干了》上卷BD发售宣传CM公开
2022/04/08 日漫