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中鼠标滚轮使div左右滚动的方法详解
Dec 14 Vue.js
Vue-router中hash模式与history模式的区别详解
Dec 15 Vue.js
vue3+typeScript穿梭框的实现示例
Dec 29 Vue.js
vue实现简易计算器功能
Jan 20 Vue.js
vue+spring boot实现校验码功能
May 27 Vue.js
vue3中provide && inject的使用
Jul 01 Vue.js
一定要知道的 25 个 Vue 技巧
Nov 02 Vue.js
详细聊聊vue中组件的props属性
Nov 02 Vue.js
Vue3中toRef与toRefs的区别
Mar 24 Vue.js
vue使用refs获取嵌套组件中的值过程
Mar 31 Vue.js
解决vue自定义组件@click点击失效问题
Apr 30 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
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
php 函数中使用static的说明
2012/06/01 PHP
js和php邮箱地址验证的实现方法
2014/01/09 PHP
46 个非常有用的 PHP 代码片段
2016/02/16 PHP
php实现在站点里面添加邮件发送的功能
2020/04/28 PHP
PHP CURL使用详解
2019/03/21 PHP
通过PHP实现用户注册后邮箱验证激活
2020/11/10 PHP
JS的千分位算法实现思路
2013/07/31 Javascript
jQuery 复合选择器应用的几个例子
2014/09/11 Javascript
javascript Promise简单学习使用方法小结
2016/05/17 Javascript
用JS动态改变表单form里的action值属性的两种方法
2016/05/25 Javascript
利用JavaScript实现拖拽改变元素大小
2016/12/14 Javascript
Vue实现数字输入框中分割手机号码的示例
2017/10/10 Javascript
微信小程序实现多宫格抽奖活动
2020/04/15 Javascript
Vue项目引发的「过滤器」使用教程
2019/03/12 Javascript
WebSocket的简单介绍及应用
2019/05/23 Javascript
Python 正则表达式(转义问题)
2014/12/15 Python
python映射列表实例分析
2015/01/26 Python
利用Python中的mock库对Python代码进行模拟测试
2015/04/16 Python
Python爬取网易云音乐热门评论
2017/03/31 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
2017/04/24 Python
Python3使用turtle绘制超立方体图形示例
2018/06/19 Python
python Selenium实现付费音乐批量下载的实现方法
2019/01/24 Python
在python中使用nohup命令说明
2020/04/16 Python
Python实现自动签到脚本的示例代码
2020/08/19 Python
pandas抽取行列数据的几种方法
2020/12/13 Python
html5实现九宫格抽奖可固定抽中某项奖品
2020/06/15 HTML / CSS
Dr. Martens马汀博士官网:马丁靴始祖品牌
2016/10/15 全球购物
法国和欧洲海边和滑雪度假:Pierre & Vacances
2017/01/04 全球购物
eBay爱尔兰站:eBay.ie
2019/08/09 全球购物
幼儿园教研活动方案
2014/01/19 职场文书
农村党员一句话承诺
2014/05/30 职场文书
户外亲子活动总结
2015/05/08 职场文书
回复函范文
2015/07/14 职场文书
成人成长感言如何写?
2019/08/16 职场文书
golang fmt格式“占位符”的实例用法详解
2021/07/04 Golang