当前位置:首页 > VUE

vue 实现尺子

2026-01-13 02:26:56VUE

实现思路

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

基础实现代码

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

vue 实现尺子

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

垂直尺子实现

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

vue 实现尺子

<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中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…