当前位置:首页 > VUE

vue 实现尺子

2026-01-13 02:26:56VUE

实现思路

在Vue中实现尺子功能,可以通过动态生成刻度线和数字标签,结合CSS样式和交互逻辑完成。核心是利用v-for循环渲染刻度线,并通过计算属性动态调整刻度间距和数值显示。

基础实现代码

以下是一个水平尺子的实现示例,包含刻度线和数字标签:

<template>
  <div class="ruler-container">
    <div class="ruler" ref="ruler">
      <div 
        v-for="(tick, index) in ticks" 
        :key="index" 
        class="tick" 
        :style="{ left: `${tick.position}px`, height: `${tick.height}px` }"
      >
        <span v-if="tick.isMajor" class="tick-label">{{ tick.value }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      scale: 1, // 缩放比例
      startValue: 0, // 起始值
      endValue: 100, // 结束值
      majorInterval: 10, // 主刻度间隔
      minorInterval: 2 // 次刻度间隔
    }
  },
  computed: {
    ticks() {
      const ticks = [];
      const totalTicks = (this.endValue - this.startValue) / this.minorInterval;

      for (let i = 0; i <= totalTicks; i++) {
        const value = this.startValue + i * this.minorInterval;
        const isMajor = value % this.majorInterval === 0;

        ticks.push({
          value,
          position: i * this.minorInterval * 10 * this.scale,
          height: isMajor ? 30 : 15,
          isMajor
        });
      }

      return ticks;
    }
  }
}
</script>

<style scoped>
.ruler-container {
  overflow-x: auto;
  padding-top: 20px;
}

.ruler {
  position: relative;
  height: 40px;
  border-bottom: 1px solid #333;
}

.tick {
  position: absolute;
  bottom: 0;
  width: 1px;
  background-color: #333;
}

.tick-label {
  position: absolute;
  left: -10px;
  top: 100%;
  font-size: 12px;
}
</style>

垂直尺子实现

若需要垂直尺子,调整样式和计算逻辑:

<template>
  <div class="vertical-ruler">
    <div 
      v-for="(tick, index) in ticks" 
      :key="index" 
      class="tick" 
      :style="{ top: `${tick.position}px`, width: `${tick.width}px` }"
    >
      <span v-if="tick.isMajor" class="tick-label">{{ tick.value }}</span>
    </div>
  </div>
</template>

<style scoped>
.vertical-ruler {
  position: relative;
  width: 40px;
  border-right: 1px solid #333;
}

.tick {
  position: absolute;
  right: 0;
  height: 1px;
  background-color: #333;
}

.tick-label {
  position: absolute;
  right: 100%;
  transform: translateY(-50%);
  font-size: 12px;
}
</style>

交互功能增强

添加拖动和缩放功能,需结合事件处理:

methods: {
  handleWheel(e) {
    e.preventDefault();
    this.scale += e.deltaY * -0.01;
    this.scale = Math.max(0.5, Math.min(2, this.scale));
  },
  startDrag(e) {
    this.dragStartX = e.clientX;
    this.dragStartScroll = this.$refs.ruler.scrollLeft;
    document.addEventListener('mousemove', this.onDrag);
    document.addEventListener('mouseup', this.stopDrag);
  },
  onDrag(e) {
    const dx = e.clientX - this.dragStartX;
    this.$refs.ruler.scrollLeft = this.dragStartScroll - dx;
  }
}

动态响应式调整

mounted中添加窗口大小监听:

mounted() {
  window.addEventListener('resize', this.calculateRulerLength);
},
beforeDestroy() {
  window.removeEventListener('resize', this.calculateRulerLength);
}

注意事项

  1. 刻度密度需根据容器宽度动态计算,避免渲染过多DOM节点
  2. 对于长尺子考虑使用虚拟滚动技术优化性能
  3. 触摸设备需额外添加触摸事件支持
  4. 数值显示格式可通过过滤器自定义

以上实现可根据实际需求调整刻度间隔、样式和交互行为。对于专业设计工具级别的尺子,可能需要结合Canvas或SVG实现更高性能的渲染。

vue 实现尺子

标签: 尺子vue
分享给朋友:

相关文章

vue实现点击样式

vue实现点击样式

Vue 实现点击样式的方法 在 Vue 中实现点击样式可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-bind:class 动态绑定类名 通过数据驱动的方式动态切换类名,结合 CS…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…