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项目利用axios请求接口下载excel
Nov 17 Vue.js
深入了解Vue3模板编译原理
Nov 19 Vue.js
实用的 vue tags 创建缓存导航的过程实现
Dec 03 Vue.js
Vue实现小购物车功能
Dec 21 Vue.js
Vue看了就会的8个小技巧
Jan 21 Vue.js
vue-router懒加载的3种方式汇总
Feb 28 Vue.js
一定要知道的 25 个 Vue 技巧
Nov 02 Vue.js
vue使用refs获取嵌套组件中的值过程
Mar 31 Vue.js
vue动态绑定style样式
Apr 20 Vue.js
vue实现省市区联动 element-china-area-data插件
Apr 22 Vue.js
vue 把二维或多维数组转一维数组
Apr 24 Vue.js
vue递归实现树形组件
Jul 15 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程序实现支持页面后退的两种方法
2008/06/30 PHP
js查错流程归纳
2012/05/04 Javascript
jquery 如何动态添加、删除class样式方法介绍
2012/11/07 Javascript
浏览器加载、渲染和解析过程黑箱简析
2012/11/29 Javascript
JS中获取数据库中的值的方法
2013/07/14 Javascript
Jquery 例外被抛出且未被接住原因介绍
2013/09/04 Javascript
jquery获取颜色在ie和ff下的区别示例介绍
2014/03/28 Javascript
javascript实现加载xml文件的方法
2015/11/24 Javascript
基于JavaScript代码实现微信扫一扫下载APP
2015/12/30 Javascript
JS实现简易刻度时钟示例代码
2017/03/11 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
JS实现自定义状态栏动画文字效果示例
2017/10/12 Javascript
Node.js的Koa实现JWT用户认证方法
2018/05/05 Javascript
微信小程序中进行地图导航功能的实现方法
2018/06/29 Javascript
在vue中更换字体,本地存储字体非引用在线字体库的方法
2018/09/28 Javascript
Vue+Express实现登录状态权限验证的示例代码
2019/05/05 Javascript
layui复选框的全选与取消实现方法
2019/09/02 Javascript
[37:03]完美世界DOTA2联赛PWL S3 INK ICE vs GXR 第二场 12.16
2020/12/18 DOTA
Python cookbook(数据结构与算法)实现查找两个字典相同点的方法
2018/02/18 Python
使用Python进行QQ批量登录的实例代码
2018/06/11 Python
opencv实现图片模糊和锐化操作
2018/11/19 Python
Python 从一个文件中调用另一个文件的类方法
2019/01/10 Python
详解Python3中setuptools、Pip安装教程
2019/06/18 Python
python中bs4.BeautifulSoup的基本用法
2019/07/27 Python
python flask中动态URL规则详解
2019/11/22 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
Java的类与C++的类有什么不同
2014/01/18 面试题
结构工程个人自荐信范文
2013/11/30 职场文书
记帐员岗位责任制
2014/02/08 职场文书
不打扫卫生检讨书
2014/02/12 职场文书
马智宇结婚主持词
2014/04/01 职场文书
煤矿安全演讲稿
2014/05/09 职场文书
企业党员一句话承诺
2014/05/30 职场文书
公司庆典欢迎词
2015/01/26 职场文书
故意杀人案辩护词
2015/05/21 职场文书
热血教师观后感
2015/06/10 职场文书