Vue实现一个简易进度条

本文介绍了如何使用Vue.js创建一个可拖动的滑块进度条组件,用户可以通过点击按钮或拖动滑块来调整百分比值,展示了数据绑定、计算属性和事件处理的方法实现。
摘要由CSDN通过智能技术生成
<template>
  <div>
    <div class="progress-bar" @mousedown="startDrag" @mouseup="stopDrag" @mousemove="dragging" ref="progressBar">
      <div class="slider" :style="{ left: progress + '%' }"></div>
      <div class="progress" :style="{ width: progress + '%' }"></div>
      <div class="progress-value">{{ currentProgress }}</div> 
    </div>
    <div>
      <button @click="decreaseProgress">-</button>
      <button @click="increaseProgress">+</button>
    </div>
  </div>
</template>
  
  <script>
export default {
  name: 'SlideBar',
  data() {
    return {
      isDragging: false,
      progress: 50
    }
  },
  computed: {
    currentProgress() {
      return Math.round(this.progress)
    }
  },
  methods: {
    startDrag() {
      this.isDragging = true
    },
    stopDrag() {
      this.isDragging = false
    },
    dragging(event) {
      if (this.isDragging) {
        const progressBar = this.$refs.progressBar
        const rect = progressBar.getBoundingClientRect()
        const offsetX = event.clientX - rect.left
        const percentage = (offsetX / rect.width) * 100
        this.progress = Math.min(100, Math.max(0, percentage))
      }
    },
    increaseProgress() {
      this.progress = Math.min(100, this.progress + 1)
    },
    decreaseProgress() {
      this.progress = Math.max(0, this.progress - 1)
    }
  }
}
</script>
  
  <style lang="scss">
.progress-bar {
  display: flex;
  align-items: center;
  width: 257px;
  height: 2px;
  background: #dcdbdb;
  position: relative;
  cursor: pointer;
}

.slider {
  position: absolute;
  width: 18px;
  height: 14px;
  background: url('../../assets/images/editor-drag/icon_progress.png') no-repeat;
  background-size: 18px 14px;
}

.progress {
  position: absolute;
  height: 100%;
  /* background: #00bcd4; */
}


.div-input {
    position: relative;
    align-items: center;
    justify-content: space-between;
    ::v-deep .el-input__inner {
      width: 62px;
      height: 34px;
    }

    .unit {
      position: absolute;
      right: 10px;
      width: 9px;
      font-family: SourceHanSansCN-Medium;
      font-weight: 500;
      font-size: 14px;
      color: #7b7e91;
      text-align: right;
    }
  }

.progress-value {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}
</style>
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值