当前位置:首页 > JavaScript

js实现520

2026-04-05 11:17:22JavaScript

js实现520

js实现520

以下是几种使用JavaScript实现"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实现复制到剪贴板

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

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 /…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现自举

js实现自举

实现 JavaScript 自举的方法 JavaScript 自举(Bootstrap)通常指通过 JavaScript 动态加载自身或依赖的脚本,实现运行时环境的初始化或扩展。以下是几种常见实现方式…

js实现弹框

js实现弹框

使用 alert 实现简单弹框 JavaScript 的 alert 方法可以直接显示一个简单的弹框,内容为纯文本: alert("这是一个简单的弹框"); 使用 confirm 实现确认弹框…

js实现导航菜单

js实现导航菜单

使用HTML和CSS创建基础结构 导航菜单的基础结构通常由HTML的无序列表(<ul>)和列表项(<li>)组成,每个列表项包含一个链接(<a>)。CSS用于样式化…