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的mixin策略
Nov 19 Vue.js
vue实现滚动鼠标滚轮切换页面
Dec 13 Vue.js
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
Dec 25 Vue.js
Vue实现简易购物车页面
Dec 30 Vue.js
SpringBoot+Vue 前后端合并部署的配置方法
Dec 30 Vue.js
vue3弹出层V3Popup实例详解
Jan 04 Vue.js
vue使用vue-quill-editor富文本编辑器且将图片上传到服务器的功能
Jan 13 Vue.js
如何在vue中使用HTML 5 拖放API
Jan 14 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
如何优化vue打包文件过大
Apr 13 Vue.js
详解Vue3使用axios的配置教程
Apr 29 Vue.js
vue如何清除浏览器历史栈
May 25 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
Netflix将与CLAMP、乙一以及冲方丁等6名知名制作人合伙展开原创动画计划!
2020/03/06 日漫
全国FM电台频率大全 - 19 广东省
2020/03/11 无线电
网络资源
2006/10/09 PHP
laravel数据库查询结果自动转数组修改实例
2021/02/27 PHP
Extjs列表详细信息窗口新建后自动加载解决方法
2010/04/02 Javascript
Safari5中alert的无限循环BUG
2011/04/07 Javascript
JavaScript在XHTML中的用法详解
2013/04/11 Javascript
js中单引号与双引号冲突问题解决方法
2013/10/04 Javascript
JavaScript 学习笔记之基础中的基础
2015/01/13 Javascript
利用AngularJs实现京东首页轮播图效果
2016/09/08 Javascript
ionic实现底部分享功能
2017/05/11 Javascript
使用AngularJS编写多选按钮选中时触发指定方法的指令代码详解
2017/07/24 Javascript
vue.js实现简单轮播图效果
2017/10/10 Javascript
BACKBONE.JS 简单入门范例
2017/10/17 Javascript
纯js代码生成可搜索选择下拉列表的实例
2018/01/11 Javascript
Vue实现侧边菜单栏手风琴效果实例代码
2018/05/31 Javascript
echarts实现晶体球面投影的实例教程
2020/10/10 Javascript
Python最长公共子串算法实例
2015/03/07 Python
使用IPython来操作Docker容器的入门指引
2015/04/08 Python
Python 类的继承实例详解
2017/03/25 Python
Python实现的文本编辑器功能示例
2017/06/30 Python
python merge、concat合并数据集的实例讲解
2018/04/12 Python
pandas将numpy数组写入到csv的实例
2018/07/04 Python
浅析python中numpy包中的argsort函数的使用
2018/08/30 Python
python GUI库图形界面开发之PyQt5 UI主线程与耗时线程分离详细方法实例
2020/02/26 Python
Python txt文件如何转换成字典
2020/11/03 Python
选购国际女性时装设计师品牌:IFCHIC(支持中文)
2018/04/12 全球购物
Groupon荷兰官方网站:高达70%的折扣
2019/11/01 全球购物
C++是不是类型安全的
2014/02/18 面试题
工程造价与管理专业应届生求职信
2013/11/23 职场文书
保护母亲河倡议书
2014/04/14 职场文书
国家奖学金获奖感言
2014/08/16 职场文书
党的群众路线教育实践活动查摆剖析材料
2014/10/10 职场文书
保研推荐信范文
2015/03/25 职场文书
2015年信息中心工作总结
2015/05/25 职场文书
捐书仪式主持词
2015/07/04 职场文书