当前位置:首页 > JavaScript

js实现积木

2026-03-14 05:32:58JavaScript

实现积木效果的 JavaScript 方法

使用 JavaScript 实现积木效果通常涉及 DOM 操作和 CSS 样式的动态控制。以下是几种常见的实现方式:

拖拽积木

通过 HTML5 的拖拽 API 实现积木的拖放功能:

document.addEventListener('DOMContentLoaded', function() {
  const blocks = document.querySelectorAll('.block');

  blocks.forEach(block => {
    block.setAttribute('draggable', true);

    block.addEventListener('dragstart', function(e) {
      e.dataTransfer.setData('text/plain', this.id);
    });
  });

  const container = document.getElementById('container');
  container.addEventListener('dragover', function(e) {
    e.preventDefault();
  });

  container.addEventListener('drop', function(e) {
    e.preventDefault();
    const id = e.dataTransfer.getData('text/plain');
    const draggedBlock = document.getElementById(id);
    this.appendChild(draggedBlock);
  });
});

随机生成积木

动态创建积木元素并设置随机样式:

function createRandomBlocks(count) {
  const container = document.getElementById('container');

  for(let i = 0; i < count; i++) {
    const block = document.createElement('div');
    block.className = 'block';

    // 随机位置和颜色
    block.style.left = `${Math.random() * 80}%`;
    block.style.top = `${Math.random() * 80}%`;
    block.style.backgroundColor = `hsl(${Math.random() * 360}, 70%, 60%)`;

    container.appendChild(block);
  }
}

积木碰撞检测

实现积木之间的物理碰撞效果:

class Block {
  constructor(x, y, width, height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.vx = Math.random() * 2 - 1;
    this.vy = Math.random() * 2 - 1;
  }

  update(blocks) {
    this.x += this.vx;
    this.y += this.vy;

    // 边界检测
    if(this.x <= 0 || this.x + this.width >= canvas.width) {
      this.vx *= -1;
    }
    if(this.y <= 0 || this.y + this.height >= canvas.height) {
      this.vy *= -1;
    }

    // 碰撞检测
    blocks.forEach(block => {
      if(block !== this && this.checkCollision(block)) {
        [this.vx, block.vx] = [block.vx, this.vx];
        [this.vy, block.vy] = [block.vy, this.vy];
      }
    });
  }

  checkCollision(block) {
    return this.x < block.x + block.width &&
           this.x + this.width > block.x &&
           this.y < block.y + block.height &&
           this.y + this.height > block.y;
  }
}

配套 CSS 样式

积木的基本样式定义:

.block {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: #3498db;
  cursor: move;
  user-select: none;
  transition: transform 0.2s;
}

.block:hover {
  transform: scale(1.1);
  z-index: 10;
}

#container {
  position: relative;
  width: 100%;
  height: 500px;
  border: 1px solid #ccc;
  overflow: hidden;
}

进阶功能实现

积木堆叠系统

实现积木的堆叠和组合功能:

function setupStacking() {
  let currentZIndex = 1;

  document.querySelectorAll('.block').forEach(block => {
    block.addEventListener('mousedown', function() {
      this.style.zIndex = currentZIndex++;
    });
  });
}

积木旋转功能

添加积木旋转交互:

js实现积木

function setupRotation() {
  document.querySelectorAll('.block').forEach(block => {
    let rotation = 0;

    block.addEventListener('dblclick', function() {
      rotation += 15;
      this.style.transform = `rotate(${rotation}deg)`;
    });
  });
}

以上代码提供了积木效果的基本实现框架,可以根据具体需求进行调整和扩展。

标签: 积木js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

js实现轮播图

js实现轮播图

轮播图实现方法 基础HTML结构 <div class="slider"> <div class="slides"> <img src="image1.jpg…

js实现倒计时

js实现倒计时

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

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document.…

js轮播图实现原理

js轮播图实现原理

轮播图的基本原理 轮播图通过动态切换显示的内容(图片、文本等)实现视觉上的滑动效果。核心原理是利用CSS和JavaScript控制元素的显示与隐藏,或通过变换位置实现滑动动画。 实现方法一:…