当前位置:首页 > JavaScript

js实现520

2026-04-05 11:17:22JavaScript

以下是几种使用JavaScript实现"520"相关效果的示例代码,涵盖不同场景需求:

js实现520

动态数字变化效果

const element = document.getElementById('loveNumber');
let count = 0;
const target = 520;
const duration = 2000; // 动画时长2秒
const step = target/(duration/16);

function updateNumber() {
  count = Math.min(count + step, target);
  element.textContent = Math.floor(count);
  if(count < target) requestAnimationFrame(updateNumber);
}
updateNumber();

爱心粒子动画

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');

const particles = [];
for(let i=0; i<520; i++) {
  particles.push({
    x: Math.random() * canvas.width,
    y: Math.random() * canvas.height,
    size: Math.random() * 5 + 2,
    speedX: Math.random() * 2 - 1,
    speedY: Math.random() * 2 - 1
  });
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = 'red';

  particles.forEach(p => {
    p.x += p.speedX;
    p.y += p.speedY;
    if(p.x < 0 || p.x > canvas.width) p.speedX *= -1;
    if(p.y < 0 || p.y > canvas.height) p.speedY *= -1;

    ctx.beginPath();
    ctx.arc(p.x, p.y, p.size, 0, Math.PI*2);
    ctx.fill();
  });

  requestAnimationFrame(animate);
}
animate();

网页背景特效

document.body.style.background = 'linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%)';
document.body.style.display = 'flex';
document.body.style.justifyContent = 'center';
document.body.style.alignItems = 'center';
document.body.style.height = '100vh';

const heart = document.createElement('div');
heart.innerHTML = '520';
heart.style.fontSize = '10rem';
heart.style.color = 'white';
heart.style.textShadow = '0 0 20px rgba(255,0,0,0.8)';
heart.style.animation = 'pulse 1s infinite alternate';

document.body.appendChild(heart);

const style = document.createElement('style');
style.textContent = `
@keyframes pulse {
  from { transform: scale(1); }
  to { transform: scale(1.1); }
}`;
document.head.appendChild(style);

控制台打印特殊效果

console.log('%c520', 'font-size: 50px; color: red; text-shadow: 2px 2px 4px #000;');
console.log('%c❤️', 'font-size: 30px; color: pink;', 'I Love You');

随机位置显示520

setInterval(() => {
  const span = document.createElement('span');
  span.textContent = '520';
  span.style.position = 'fixed';
  span.style.left = Math.random() * window.innerWidth + 'px';
  span.style.top = Math.random() * window.innerHeight + 'px';
  span.style.color = `hsl(${Math.random()*360}, 100%, 50%)`;
  span.style.fontSize = Math.random() * 30 + 10 + 'px';
  document.body.appendChild(span);

  setTimeout(() => {
    span.remove();
  }, 1000);
}, 100);

这些代码可以直接在浏览器中运行,根据需求选择适合的效果或组合使用。每种实现方式都可通过调整参数来自定义显示效果。

标签: js
分享给朋友:

相关文章

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js实现下拉刷新

js实现下拉刷新

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。 let startY = 0; let curr…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…

js实现的游戏

js实现的游戏

JavaScript 游戏开发基础 JavaScript 是开发网页游戏的流行选择,因其无需插件即可在浏览器中运行。以下是一些关键技术和资源: HTML5 Canvas Canvas 提供了绘制图形…

js实现 拖动

js实现 拖动

实现拖动的步骤 HTML 结构 创建一个可拖动的元素和一个放置区域: <div id="draggable" draggable="true">拖动我</div> <d…