Vue implements drag progress bar

Vue implements drag progress bar

This article example shares the specific code of Vue to realize the drag progress bar for your reference. The specific content is as follows

Component code:

<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 minimum value of progress bar* max maximum value of progress bar* v-model performs two-way binding on the current value to display the drag progress in real time* */
export default {
 props: ["min", "max", "value"],
 data() {
  return {
   slider: null, //Scroll bar DOM element thunk: null, //Drag DOM element per: this.value, //Current value};
 },
 //When rendering to the page 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
    // When the value changes, the left and width will be modified through the calculated properties

    // Get the new width when dragging
    var newWidth = e.clientX - disX + width;
    // Get the new percentage when dragging 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: {
  // Set a percentage to calculate the slider progress width and the left value of the trunk // The corresponding formula is current value - minimum value / maximum value - minimum value = slider progress width / slider total width
  // trunk left = slider progress width + trunk width / 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>

Call:

<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>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of implementing circular progress bar in Vue
  • vue.js+ElementUI realizes the effect of progress bar prompting password strength
  • Progress bar function when vue page is loading (example code)
  • Circular loading progress bar encapsulation (Vue plug-in version and native js version)
  • Vue configures nprogress to implement the progress bar at the top of the page
  • How to use NProgress progress bar in Vue
  • Vue project implements file download progress bar function

<<:  How to install mysql database in deepin 2014 system

>>:  Three ways to configure Nginx virtual hosts (based on domain names)

Recommend

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

About deploying a web project to Alibaba Cloud Server (5 steps to do it)

1. First log in to the Alibaba Cloud website to r...

Three ways to align div horizontal layout on both sides

This article mainly introduces three methods of i...

How to create a new user in CentOS and enable key login

Table of contents Create a new user Authorize new...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

Detailed graphic explanation of how to use svg in vue3+vite project

Today, in the practice of vue3+vite project, when...

MySQL DML statement summary

DML operations refer to operations on table recor...

TimePicker in element disables part of the time (disabled to minutes)

The project requirements are: select date and tim...

MySQL data table partitioning strategy and advantages and disadvantages analysis

Table of contents Why do we need partitions? Part...

MySQL slow query pitfalls

Table of contents 1. Slow query configuration 1-1...

Apache Log4j2 reports a nuclear-level vulnerability and a quick fix

Apache Log4j2 reported a nuclear-level vulnerabil...

MySQL replication detailed explanation and simple example

MySQL replication detailed explanation and simple...