当前位置:首页 > JavaScript

js 实现沙漏

2026-04-06 21:03:05JavaScript

实现沙漏的 JavaScript 方法

使用 JavaScript 实现沙漏效果可以通过 CSS 动画和 JavaScript 定时器结合完成。以下是一个简单的实现方法。

js 实现沙漏

创建 HTML 结构

<div class="hourglass">
  <div class="top"></div>
  <div class="bottom"></div>
</div>

添加 CSS 样式

.hourglass {
  width: 100px;
  height: 200px;
  position: relative;
}

.top, .bottom {
  width: 100%;
  height: 50%;
  background-color: #f0f0f0;
  position: absolute;
}

.top {
  top: 0;
  border-bottom: 2px solid #333;
}

.bottom {
  bottom: 0;
  border-top: 2px solid #333;
}

JavaScript 控制动画

const topPart = document.querySelector('.top');
const bottomPart = document.querySelector('.bottom');
let sandHeight = 0;

function animateHourglass() {
  sandHeight += 1;
  if (sandHeight <= 100) {
    topPart.style.height = `${50 - sandHeight / 2}%`;
    bottomPart.style.height = `${50 + sandHeight / 2}%`;
    requestAnimationFrame(animateHourglass);
  }
}

animateHourglass();

使用 Canvas 绘制沙漏

如果需要更复杂的沙漏效果,可以使用 Canvas 绘制。

js 实现沙漏

HTML 设置

<canvas id="hourglass" width="200" height="400"></canvas>

JavaScript 绘制逻辑

const canvas = document.getElementById('hourglass');
const ctx = canvas.getContext('2d');
let sandLevel = 0;

function drawHourglass() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // 绘制沙漏框架
  ctx.beginPath();
  ctx.moveTo(50, 50);
  ctx.lineTo(150, 50);
  ctx.lineTo(100, 200);
  ctx.lineTo(150, 350);
  ctx.lineTo(50, 350);
  ctx.lineTo(100, 200);
  ctx.closePath();
  ctx.stroke();

  // 绘制上部沙子
  ctx.fillStyle = '#d4a017';
  ctx.beginPath();
  ctx.moveTo(50, 50);
  ctx.lineTo(150, 50);
  ctx.lineTo(100, 200 - sandLevel);
  ctx.lineTo(50, 50);
  ctx.fill();

  // 绘制下部沙子
  ctx.beginPath();
  ctx.moveTo(100, 200 + sandLevel);
  ctx.lineTo(150, 350);
  ctx.lineTo(50, 350);
  ctx.lineTo(100, 200 + sandLevel);
  ctx.fill();

  sandLevel += 1;
  if (sandLevel <= 200) {
    requestAnimationFrame(drawHourglass);
  }
}

drawHourglass();

使用 CSS 动画实现沙漏

纯 CSS 也可以实现沙漏效果,但交互性较弱。

HTML 结构

<div class="hourglass">
  <div class="sand-top"></div>
  <div class="sand-bottom"></div>
</div>

CSS 动画

.hourglass {
  width: 100px;
  height: 200px;
  position: relative;
  border: 2px solid #333;
}

.sand-top, .sand-bottom {
  width: 100%;
  position: absolute;
  background-color: #d4a017;
}

.sand-top {
  top: 0;
  height: 100%;
  animation: drain 5s linear forwards;
}

.sand-bottom {
  bottom: 0;
  height: 0%;
  animation: fill 5s linear forwards;
}

@keyframes drain {
  to { height: 0%; }
}

@keyframes fill {
  to { height: 100%; }
}

以上方法可以根据需求选择适合的实现方式。CSS 方法简单但功能有限,Canvas 方法灵活但代码量较大。

标签: 沙漏js
分享给朋友:

相关文章

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-containe…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js 实现分页

js 实现分页

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

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…