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 24 Vue.js
Vue实现省市区三级联动
Dec 27 Vue.js
Vue 事件的$event参数=事件的值案例
Jan 29 Vue.js
vue 动态添加的路由页面刷新时失效的原因及解决方案
Feb 26 Vue.js
vue路由实现登录拦截
Mar 24 Vue.js
vue前端工程的搭建
Mar 31 Vue.js
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
Apr 17 Vue.js
vue实现简单数据双向绑定
Apr 28 Vue.js
vue3使用vue-router的完整步骤记录
Jun 20 Vue.js
Vue3中的Refs和Ref详情
Nov 11 Vue.js
详解Vue中$props、$attrs和$listeners的使用方法
Feb 18 Vue.js
Vue.js中v-for指令的用法介绍
Mar 13 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的FTP学习(四)
2006/10/09 PHP
php基础知识:类与对象(3) 构造函数和析构函数
2006/12/13 PHP
php下实现伪 url 的超简单方法[转]
2007/09/24 PHP
ThinkPHP实现一键清除缓存方法
2014/06/26 PHP
ucenter通信原理分析
2015/01/09 PHP
深入讲解PHP的对象注入(Object Injection)
2017/03/01 PHP
PHP实现发送微博消息功能完整示例
2019/12/04 PHP
JavaScript 动态添加表格行 使用模板、标记
2009/10/24 Javascript
使用 JScript 创建 .exe 或 .dll 文件的方法
2011/07/13 Javascript
不使用浏览器运行javascript代码的方法
2013/07/24 Javascript
js实现网页随机切换背景图片的方法
2014/11/01 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
JS实现Ajax的方法分析
2016/12/20 Javascript
JavaScript字符串对象(string)基本用法示例
2017/01/18 Javascript
jQuery插件echarts去掉垂直网格线用法示例
2017/03/03 Javascript
JS实现给json数组动态赋值的方法示例
2020/03/19 Javascript
vue之延时刷新实例
2019/11/14 Javascript
深入理解Django的自定义过滤器
2017/10/17 Python
django框架实现模板中获取request 的各种信息示例
2019/07/01 Python
django中账号密码验证登陆功能的实现方法
2019/07/15 Python
python多线程同步实例教程
2019/08/11 Python
Numpy的简单用法小结
2019/08/28 Python
python实现图片二值化及灰度处理方式
2019/12/07 Python
Python 解决OPEN读文件报错 ,路径以及r的问题
2019/12/19 Python
python实现五子棋游戏(pygame版)
2020/01/19 Python
css3动画效果小结(推荐)
2016/07/25 HTML / CSS
Silk’n激光脱毛器官网:silkn.com
2016/10/06 全球购物
食品安全工作方案
2014/05/07 职场文书
大学生就业自我推荐信
2014/05/10 职场文书
会计求职自荐信
2014/06/20 职场文书
党员弘扬焦裕禄精神思想汇报
2014/09/10 职场文书
2014最新自愿离婚协议书范本
2014/11/19 职场文书
幼儿园大班毕业评语
2014/12/31 职场文书
2015年项目工作总结
2015/04/29 职场文书
python设置 matplotlib 正确显示中文的四种方式
2021/05/10 Python
Js类的构建与继承案例详解
2021/09/15 Javascript