当前位置:首页 > JavaScript

js 实现垂直标尺

2026-03-15 21:50:06JavaScript

实现垂直标尺的 JavaScript 方法

使用 Canvas 绘制垂直标尺
通过 HTML5 Canvas 可以动态绘制标尺刻度,适合需要自定义样式的场景。

const canvas = document.getElementById('rulerCanvas');
const ctx = canvas.getContext('2d');
const height = 500; // 标尺高度
const unit = 10;    // 刻度单位(像素)

canvas.width = 40;  // 标尺宽度
canvas.height = height;

ctx.fillStyle = '#f0f0f0';
ctx.fillRect(0, 0, canvas.width, canvas.height);

// 绘制刻度线
for (let y = 0; y <= height; y += unit) {
  const lineWidth = y % 50 === 0 ? 15 : (y % 10 === 0 ? 10 : 5);
  ctx.beginPath();
  ctx.moveTo(canvas.width - lineWidth, y);
  ctx.lineTo(canvas.width, y);
  ctx.stroke();

  // 添加数字标签(每50像素)
  if (y % 50 === 0) {
    ctx.font = '10px Arial';
    ctx.fillText(y.toString(), 5, y + 3);
  }
}

使用 CSS 和 DOM 元素模拟标尺
通过动态生成 div 元素实现,适合需要响应式布局的场景。

<div id="verticalRuler" style="position: relative; height: 500px; width: 30px; background: #f0f0f0;"></div>

<script>
  const ruler = document.getElementById('verticalRuler');
  const height = 500;
  const unit = 10;

  for (let y = 0; y <= height; y += unit) {
    const tick = document.createElement('div');
    tick.style.position = 'absolute';
    tick.style.right = '0';
    tick.style.top = `${y}px`;
    tick.style.height = '1px';
    tick.style.backgroundColor = '#000';

    // 长刻度(每50像素)
    if (y % 50 === 0) {
      tick.style.width = '15px';
      const label = document.createElement('div');
      label.textContent = y;
      label.style.position = 'absolute';
      label.style.right = '20px';
      label.style.top = `${y - 6}px`;
      ruler.appendChild(label);
    } else if (y % 10 === 0) {
      tick.style.width = '10px';
    } else {
      tick.style.width = '5px';
    }
    ruler.appendChild(tick);
  }
</script>

使用 SVG 实现矢量标尺
适合需要缩放且保持清晰度的场景。

js 实现垂直标尺

<svg id="svgRuler" width="40" height="500" style="background: #f0f0f0;"></svg>

<script>
  const svg = document.getElementById('svgRuler');
  const height = 500;
  const unit = 10;

  for (let y = 0; y <= height; y += unit) {
    const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    line.setAttribute('x1', y % 50 === 0 ? 5 : (y % 10 === 0 ? 10 : 15));
    line.setAttribute('x2', '40');
    line.setAttribute('y1', y);
    line.setAttribute('y2', y);
    line.setAttribute('stroke', '#000');

    // 添加文本标签
    if (y % 50 === 0) {
      const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
      text.setAttribute('x', '0');
      text.setAttribute('y', y + 3);
      text.textContent = y;
      text.setAttribute('font-size', '10px');
      svg.appendChild(text);
    }
    svg.appendChild(line);
  }
</script>

关键注意事项

  • 动态调整:监听窗口大小变化事件(resize),重新计算标尺刻度位置。
  • 性能优化:对于高频更新的场景,推荐使用 Canvas 或 SVG 而非 DOM 操作。
  • 交互功能:可通过事件监听实现标尺拖拽或点击定位功能。

标签: 标尺js
分享给朋友:

相关文章

js实现

js实现

JavaScript 实现方法 JavaScript 是一种广泛使用的编程语言,适用于网页开发、服务器端编程以及移动应用开发。以下是几种常见的 JavaScript 实现方法: 网页交互功能 使用…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

css制作标尺

css制作标尺

使用 CSS 制作标尺的方法 方法一:使用线性渐变和伪元素 通过 linear-gradient 和伪元素实现刻度线,适合简单的水平或垂直标尺。 .ruler { position: rela…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…