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 router传递参数并解决刷新页面参数丢失问题
Dec 02 Vue.js
浅谈Vue使用Elementui修改默认的最快方法
Dec 05 Vue.js
vue 在服务器端直接修改请求的接口地址
Dec 19 Vue.js
vue中实现点击空白区域关闭弹窗的两种方法
Dec 30 Vue.js
vue3弹出层V3Popup实例详解
Jan 04 Vue.js
vue+element table表格实现动态列筛选的示例代码
Jan 14 Vue.js
Vue实现图书管理案例
Jan 20 Vue.js
Vue 实现可视化拖拽页面编辑器
Feb 01 Vue.js
vue常用高阶函数及综合实例
Feb 25 Vue.js
vue前端和Django后端如何查询一定时间段内的数据
Feb 28 Vue.js
vue如何批量引入组件、注册和使用详解
May 12 Vue.js
Vue鼠标滚轮滚动切换路由效果的实现方法
Aug 04 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
星际中的相关伤害
2020/03/04 星际争霸
php二分查找二种实现示例
2014/03/12 PHP
php 防止表单重复提交两种实现方法
2016/11/03 PHP
Laravel框架控制器,视图及模型操作图文详解
2019/12/04 PHP
java script编程起步(第三课)
2007/01/10 Javascript
jQuery 研究心得 取得属性的值
2007/11/30 Javascript
input 高级限制级用法
2009/03/26 Javascript
jQuery EasyUI NumberBox(数字框)的用法
2010/07/08 Javascript
基于jquery的监控数据是否发生改变
2011/04/11 Javascript
JavaScript 类型的包装对象(Typed Wrappers)
2011/10/27 Javascript
js自动生成的元素与页面原有元素发生堆叠的解决方法
2014/09/04 Javascript
window.location的重写及判断location是否被重写
2014/09/04 Javascript
用svg制作富有动态的tooltip
2015/07/17 Javascript
JavaScript实现LI列表数据绑定的方法
2015/08/04 Javascript
JavaScript必知必会(十) call apply bind的用法说明
2016/06/08 Javascript
jackson解析json字符串,首字母大写会自动转为小写的方法
2017/12/22 Javascript
vue环形进度条组件实例应用
2018/10/10 Javascript
js实现动态添加上传文件页面
2018/10/22 Javascript
通过实例了解js函数中参数的传递
2019/06/15 Javascript
Emberjs 通过 axios 下载文件的方法
2019/09/03 Javascript
微信小程序点击view动态添加样式过程解析
2020/01/21 Javascript
Python的gevent框架的入门教程
2015/04/29 Python
python3实现暴力穷举博客园密码
2016/06/19 Python
python3.5 tkinter实现页面跳转
2018/01/30 Python
numpy 计算两个数组重复程度的方法
2018/11/07 Python
python抖音表白程序源代码
2019/04/07 Python
Django利用cookie保存用户登录信息的简单实现方法
2019/05/27 Python
python opencv摄像头的简单应用
2019/06/06 Python
完美解决pycharm导入自己写的py文件爆红问题
2020/02/12 Python
python3 googletrans超时报错问题及翻译工具优化方案 附源码
2020/12/23 Python
法国隐形眼镜网站:VisionDirect.fr
2020/03/03 全球购物
消防器材管理制度
2014/01/28 职场文书
请假条怎么写
2014/04/10 职场文书
升学宴祝酒词
2015/08/11 职场文书
如何书写邀请函?
2019/06/24 职场文书
CSS3 制作的书本翻页特效
2021/04/13 HTML / CSS