vue+elementUI el-progress实现可拖拽进度条
在Vue中使用Element UI的el-progress
组件实现可拖拽进度条,可以通过绑定鼠标事件来实现。以下是一个简单的实现示例:
<template>
<div class="progress-container" @mousedown="handleMouseDown" @mouseup="handleMouseUp">
<el-progress :percentage="progress" :format="formatProgress"></el-progress>
</div>
</template>
<script>
export default {
data() {
return {
isDragging: false,
progress: 0,
};
},
methods: {
handleMouseDown(event) {
this.isDragging = true;
this.updateProgress(event);
},
handleMouseMove(event) {
if (this.isDragging) {
this.updateProgress(event);
}
},
handleMouseUp() {
this.isDragging = false;
},
updateProgress(event) {
const containerWidth = this.$el.offsetWidth;
const progressWidth = event.clientX - this.$el.getBoundingClientRect().left;
const percentage = Math.round((progressWidth / containerWidth) * 100);
this.progress = Math.max(0, Math.min(percentage, 100));
},
formatProgress(percentage) {
return `${percentage}%`;
},
},
mounted() {
document.addEventListener('mousemove', this.handleMouseMove);
},
beforeDestroy() {
document.removeEventListener('mousemove', this.handleMouseMove);
},
};
</script>
<style scoped>
.progress-container {
position: relative;
display: inline-block;
cursor: e-resize;
}
</style>
在这个示例中,我们创建了一个可拖拽的进度条。当用户按下鼠标并在进度条上移动时,进度条的宽度会随之变化,并且progress
数据属性会更新以反映新的进度百分比。当用户释放鼠标按钮时,停止更新进度。这个示例还包括了格式化进度条文本的功能,以显示百分比。
评论已关闭