当前位置:首页 > VUE

vue实现游标尺

2026-02-20 07:00:19VUE

Vue 实现游标尺的方法

使用 SVG 绘制游标尺

在 Vue 中可以通过 SVG 实现游标尺,利用 SVG 的线条和文本元素绘制刻度。以下是一个简单的实现方式:

<template>
  <div class="ruler-container">
    <svg width="100%" height="50" class="ruler">
      <line x1="0" y1="0" x2="100%" y2="0" stroke="black" stroke-width="2" />
      <template v-for="i in ticks">
        <line 
          :x1="i * 10" 
          :y1="0" 
          :x2="i * 10" 
          :y2="i % 10 === 0 ? 20 : 10" 
          stroke="black" 
          stroke-width="1" 
        />
        <text 
          v-if="i % 10 === 0" 
          :x="i * 10" 
          y="35" 
          text-anchor="middle"
        >
          {{ i }}
        </text>
      </template>
    </svg>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ticks: Array.from({ length: 101 }, (_, i) => i)
    }
  }
}
</script>

<style>
.ruler-container {
  width: 100%;
  overflow-x: auto;
}
.ruler {
  min-width: 1000px;
}
</style>

使用 CSS 和动态样式

另一种方法是使用 CSS 和动态样式生成游标尺:

<template>
  <div class="ruler">
    <div 
      v-for="i in ticks" 
      :key="i" 
      class="tick" 
      :style="{ left: `${i * 10}px` }"
      :class="{ major: i % 10 === 0 }"
    >
      <span v-if="i % 10 === 0" class="label">{{ i }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ticks: Array.from({ length: 101 }, (_, i) => i)
    }
  }
}
</script>

<style>
.ruler {
  position: relative;
  height: 30px;
  border-bottom: 2px solid #000;
  width: 1000px;
  overflow-x: auto;
}
.tick {
  position: absolute;
  bottom: 0;
  width: 1px;
  height: 10px;
  background: #000;
}
.tick.major {
  height: 20px;
}
.label {
  position: absolute;
  top: 22px;
  left: -10px;
  font-size: 12px;
}
</style>

添加交互功能

要实现可拖动的游标,可以添加鼠标事件处理:

<template>
  <div class="ruler-container" @mousemove="handleMouseMove">
    <div class="ruler">
      <!-- 刻度代码同上 -->
    </div>
    <div class="cursor" :style="{ left: cursorPosition + 'px' }"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cursorPosition: 0,
      ticks: Array.from({ length: 101 }, (_, i) => i)
    }
  },
  methods: {
    handleMouseMove(event) {
      const rect = this.$el.getBoundingClientRect()
      this.cursorPosition = event.clientX - rect.left
    }
  }
}
</script>

<style>
.cursor {
  position: absolute;
  top: 0;
  width: 2px;
  height: 30px;
  background: red;
  pointer-events: none;
}
</style>

响应式设计

确保游标尺在不同屏幕尺寸下正常工作:

vue实现游标尺

// 在组件中添加
mounted() {
  window.addEventListener('resize', this.updateRulerWidth)
},
beforeDestroy() {
  window.removeEventListener('resize', this.updateRulerWidth)
},
methods: {
  updateRulerWidth() {
    // 根据容器宽度调整刻度密度
  }
}

这些方法提供了从基础到交互式的游标尺实现方案,可以根据具体需求进行调整和扩展。

标签: 游标vue
分享给朋友:

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个数…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue交互实现

vue交互实现

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

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…