当前位置:首页 > JavaScript

js实现星星闪烁的效果

2026-04-05 01:05:41JavaScript

实现星星闪烁效果的方法

使用CSS动画结合JavaScript

通过CSS定义闪烁动画,JavaScript动态添加星星元素并应用动画。

.star {
  position: absolute;
  background-color: white;
  border-radius: 50%;
  animation: twinkle 1s infinite alternate;
}

@keyframes twinkle {
  0% { opacity: 0.2; transform: scale(0.8); }
  100% { opacity: 1; transform: scale(1.2); }
}
function createStars() {
  const container = document.getElementById('star-container');
  const starCount = 100;

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

    // 随机位置和大小
    const size = Math.random() * 3 + 1;
    star.style.width = `${size}px`;
    star.style.height = `${size}px`;
    star.style.left = `${Math.random() * 100}%`;
    star.style.top = `${Math.random() * 100}%`;

    // 随机动画延迟
    star.style.animationDelay = `${Math.random() * 2}s`;

    container.appendChild(star);
  }
}

window.onload = createStars;

使用Canvas绘制闪烁星星

通过Canvas实现更灵活的星星效果,包括不同形状和大小的星星。

const canvas = document.getElementById('star-canvas');
const ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const stars = Array(200).fill().map(() => ({
  x: Math.random() * canvas.width,
  y: Math.random() * canvas.height,
  size: Math.random() * 3 + 1,
  brightness: Math.random(),
  speed: Math.random() * 0.05
}));

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

  stars.forEach(star => {
    star.brightness += star.speed;
    if (star.brightness > 1 || star.brightness < 0) {
      star.speed = -star.speed;
    }

    ctx.globalAlpha = star.brightness;
    ctx.fillStyle = 'white';
    ctx.beginPath();
    ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
    ctx.fill();
  });

  requestAnimationFrame(animate);
}

animate();

使用GSAP动画库

利用GSAP库实现更复杂的闪烁效果和动画控制。

import { gsap } from 'gsap';

function createGsapStars() {
  const container = document.getElementById('star-container');
  const stars = Array(50).fill().map(() => {
    const star = document.createElement('div');
    star.className = 'star';
    Object.assign(star.style, {
      position: 'absolute',
      width: `${Math.random() * 4 + 1}px`,
      height: `${Math.random() * 4 + 1}px`,
      left: `${Math.random() * 100}%`,
      top: `${Math.random() * 100}%`,
      backgroundColor: 'white',
      borderRadius: '50%'
    });
    container.appendChild(star);
    return star;
  });

  stars.forEach(star => {
    gsap.to(star, {
      opacity: 0,
      scale: 0.5,
      duration: Math.random() * 1 + 0.5,
      repeat: -1,
      yoyo: true,
      ease: 'power1.inOut'
    });
  });
}

实现不同形状的星星

创建星形而非圆形的闪烁效果。

js实现星星闪烁的效果

function createStarShape(x, y, spikes, outerRadius, innerRadius) {
  let rot = Math.PI/2 * 3;
  let step = Math.PI / spikes;

  ctx.beginPath();
  ctx.moveTo(x, y - outerRadius);

  for(let i = 0; i < spikes; i++) {
    const x1 = x + Math.cos(rot) * outerRadius;
    const y1 = y + Math.sin(rot) * outerRadius;
    ctx.lineTo(x1, y1);
    rot += step;

    const x2 = x + Math.cos(rot) * innerRadius;
    const y2 = y + Math.sin(rot) * innerRadius;
    ctx.lineTo(x2, y2);
    rot += step;
  }

  ctx.lineTo(x, y - outerRadius);
  ctx.closePath();
}

// 在Canvas动画循环中使用
stars.forEach(star => {
  ctx.globalAlpha = star.brightness;
  ctx.fillStyle = 'white';
  createStarShape(star.x, star.y, 5, star.size, star.size * 0.4);
  ctx.fill();
});

每种方法适用于不同场景:CSS动画适合简单实现,Canvas提供更高自由度,GSAP适合复杂动画控制,自定义形状可创造独特视觉效果。

标签: 星星效果
分享给朋友:

相关文章

h5实现左右滚动效果

h5实现左右滚动效果

使用CSS实现横向滚动 在HTML5中实现左右滚动效果,可以通过CSS的overflow-x属性配合white-space或flex布局实现。以下是一个基础示例: <div class="sc…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

vue实现翻书效果

vue实现翻书效果

Vue实现翻书效果的方法 翻书效果可以通过CSS动画和Vue的动态绑定实现,以下是几种常见的实现方式: 使用CSS transform和transition 通过CSS的transform属…

vue实现滑动效果

vue实现滑动效果

Vue 实现滑动效果的方法 在 Vue 中实现滑动效果可以通过多种方式,包括使用 CSS 过渡、第三方动画库或直接操作 DOM。以下是几种常见的方法: 使用 CSS 过渡和 Vue 的 <tr…

vue实现tab效果

vue实现tab效果

Vue 实现 Tab 效果的方法 使用动态组件和 v-if 通过 v-if 或 v-show 控制不同标签内容的显示与隐藏,结合点击事件切换当前激活的标签页。 <template>…