How to implement event-responsive progress bar component in Vue
This article mainly introduces the example code of Vue's event-responsive progress bar component. It is very good and has reference value. Friends who need it can refer to it
Write it in front
I found a lot of vue progress bar components, but none of them include drag and click events. The input range natively contains input and change events, but if you make a progress bar directly based on the input range, the style Some parts require a lot of adjustments and compatibility processing. Even if it is done, if the appearance needs to be modified in the future, it will be a lot of trouble.
Based on the above two reasons, I made a program that can respond to input and change events (that is, one is to drag the progress bar somewhere, and the other is to click on a certain position of the progress bar to change its value to that position) The Vue component implemented by p not only meets the demand for progress bar events, but also brings the benefit of convenient style modification if the requirements change.
Rendering
The above are some of the effects that can be achieved by using this component. They can respond to both input and change events.
First is the template part
Look at the picture above carefully. How to construct the HTML template still needs some consideration. I also changed it several times and finally settled on this one. structure. First of all, there is a layer of outsourcing that I won’t talk about. Then there is a p with class = 'progress' under the outsourcing p. The p inside this p means that the progress bar has crossed the part (class="left"). The p with class="left" also contains a p indicating that The slider ball that we can drag.
Let’s talk about the benefits. With such a structure and the style created, when checking the elements on the page, you can clearly see that each p overlaps with the part displayed on the page.
If your progress bar represents p of the entire length, p represents the left half, and p represents the slider, it is not a nested structure like mine, but a sibling node relationship, you have to use styles to do relative Position and move the last two sibling nodes up, so that when the element is checked, the boxes of other components below the progress bar will penetrate into the area of the progress bar. Although the user will not check the elements, it will not be convenient for programmers to observe by themselves after a long time, right?
In other words, we all hope that the elements expressed by the HTML structure and the placeholder of each element displayed when checking the element are consistent. This can also be regarded as an evaluation indicator of whether your HTML structure is reasonably constructed.
<template> <p class="progress-wrapper" :style="wrapStyle"> <p class="progress" @mousedown="mousedownHandler" @mouseover="mouseoverHandler" @mousemove="mousemoveHandler" @mouseup="mouseupHandler" :style="pBarStyle"> <p class="left" :style="leftStyle"> <p class="ball" :style="ballStyle"></p> </p> <slot></slot> </p> </p> </template>
js part
For students who now need to use this progress bar with events, take a look at this part to help you modify and improve it. it.
For students who want to try this component first, you can skip reading this part. When you find that the function of this component is insufficient, it will not be too late to look at this part of the code.
export default { name: 'ProgressBar', props: { leftBg: String, bgc: String, ballBgc: String, height: String, width: String, max: { type: Number, default: 100, }, min: { type: Number, default: 0, }, value: { type: Number, default: 36, }, }, data: function () { return { pValue: this.value, pMax: this.max, pMin: this.min, wrapStyle: { 'width': this.width, }, pBarStyle: { 'backgroundColor': this.bgc, 'height': this.height, }, leftStyle: { 'width': this.progressPercent + '%', 'background': this.leftBg, 'height': this.height, }, ballStyle: { 'backgroundColor': this.ballBgc, 'height': this.height, 'width': this.height, 'borderRadius': parseInt(this.height) / 2 + 'px', 'right': - parseInt(this.height) / 2 + 'px', }, // 标记是否按下鼠标 isMouseDownOnBall: false, } }, computed: { progressPercent(){ return (this.pValue - this.pMin) / (this.pMax - this.pMin) * 100; }, progressElement(){ return this.$el.getElementsByClassName('progress')[0]; }, }, methods: { mousedownHandler(e){ if(e.which === 1){ this.isMouseDownOnBall = true; } }, mousemoveHandler(e){ if(this.isMouseDownOnBall === true){ // 修改进度条本身 let decimal = (e.clientX - this.$el.offsetLeft) / this.progressElement.clientWidth; let percent = decimal * 100; this.leftStyle.width = percent + '%'; // 修改value this.pValue = this.pMin + decimal * (this.pMax - this.pMin); this.$emit('pbar-drag', this.pValue, percent); } }, mouseupHandler(e){ if(this.isMouseDownOnBall){ // 修改进度条本身 let decimal = (e.clientX - this.$el.offsetLeft) / this.progressElement.clientWidth; let percent = decimal * 100; this.leftStyle.width = percent + '%'; // 修改value this.pValue = this.pMin + decimal * (this.pMax - this.pMin); this.$emit('pbar-seek', this.pValue, percent); this.isMouseDownOnBall = false; } }, mouseoverHandler(e){ // 没有按左键进入进度条 if(e.which === 0){ this.isMouseDownOnBall = false; } } }, watch: { max(cur, old){ this.pMax = cur; }, min(cur, old){ this.pMin = cur; }, value(cur, old){ this.pValue = cur; }, progressPercent(cur, old){ this.leftStyle.width = cur + '%'; } }, mounted(){ // 数据验证 if(this.max < this.min){ console.error("max can't less than min !"); } // 初始百分比 this.leftStyle.width = (this.pValue - this.pMin) / (this.pMax - this.pMin) * 100 + '%'; }, }
Installation and use
Address
Code library address is installed on GitHub
, use
npm install vue-draggable-progressbar --save import progressBar from 'vue-draggable-progressbar'
Use case:
<progress-bar ref="aa"></progress-bar> <progress-bar width="40%" leftBg="greenyellow" bgc="#ccc" ballBgc="red"></progress-bar> <progress-bar width="60%" leftBg="linear-gradient(to right, yellow, pink)" bgc="#ccc" ballBgc="red"></progress-bar> <progress-bar width="80%" leftBg="yellow" bgc="#ccc" ballBgc="red" height="30px"></progress-bar> <progress-bar leftBg="greenyellow" bgc="#ccc" ballBgc="rgba(255,0,0,0.2)" height="40px"></progress-bar> <progress-bar leftBg="greenyellow" bgc="#ccc" ballBgc="red" :max="max" :value="value" :min="min" @pbar-drag="drag" @pbar-seek="seek"></progress-bar>
Component props
leftBg: Progress The bar has crossed part of the background color
bgc: The progress bar has not crossed part of the background color
ballBgc: Slider background color
width: the percentage of the width of the parent component of the progress bar, pass the percentage value
height: the height of the progress bar, pass the pixel value
max: Maximum value of the progress bar
min: Minimum value
value: Current value
Event
pbar-drag: Triggered when dragging the progress bar, returning value and percentage value
pbar-drag: Triggered when a certain position of the progress bar is clicked, and the value and percentage value are returned.
The above is what I compiled for everyone. Yes, I hope it will be helpful to everyone in the future.
Related articles:
How to use Dom elements in jQuery?
What are the commonly used array functions in js?
About how to use datepicker in vue2.0
The above is the detailed content of How to implement event-responsive progress bar component in Vue. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with <div>. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

In Vue.js, lazy loading allows components or resources to be loaded dynamically as needed, reducing initial page loading time and improving performance. The specific implementation method includes using <keep-alive> and <component is> components. It should be noted that lazy loading can cause FOUC (splash screen) issues and should be used only for components that need lazy loading to avoid unnecessary performance overhead.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.
