当前位置:首页 > JavaScript

js实现背景动态

2026-02-03 07:23:28JavaScript

使用CSS动画实现动态背景

通过CSS的@keyframesanimation属性可以创建平滑的动态背景效果。以下是一个渐变颜色变化的示例:

body {
  animation: gradientShift 10s ease infinite;
  background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fbc2eb, #a18cd1);
  background-size: 400% 400%;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

Canvas绘制粒子背景

使用HTML5 Canvas创建粒子系统动态背景:

js实现背景动态

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.style.position = 'fixed';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.zIndex = '-1';

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

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

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

  particles.forEach(particle => {
    particle.x += particle.speedX;
    particle.y += particle.speedY;

    if (particle.x < 0 || particle.x > canvas.width) particle.speedX *= -1;
    if (particle.y < 0 || particle.y > canvas.height) particle.speedY *= -1;

    ctx.fillStyle = `rgba(255, 255, 255, ${Math.random()})`;
    ctx.beginPath();
    ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
    ctx.fill();
  });

  requestAnimationFrame(animate);
}

animate();

window.addEventListener('resize', () => {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
});

WebGL实现3D动态背景

使用Three.js库创建3D动态背景:

js实现背景动态

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ 
  color: 0x00ff00,
  wireframe: true 
});
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

camera.position.z = 5;

function animate() {
  requestAnimationFrame(animate);
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render(scene, camera);
}

animate();

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
});

SVG动画背景

使用SVG创建可缩放的动态背景:

<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="grid" width="50" height="50" patternUnits="userSpaceOnUse">
      <path d="M 50 0 L 0 0 0 50" fill="none" stroke="#eee" stroke-width="1"/>
    </pattern>
  </defs>

  <rect width="100%" height="100%" fill="url(#grid)">
    <animateTransform attributeName="transform" type="translate" from="0 0" to="50 50" dur="10s" repeatCount="indefinite"/>
  </rect>

  <circle cx="50%" cy="50%" r="10%" fill="none" stroke="#ff5252" stroke-width="2">
    <animate attributeName="r" values="10%;20%;10%" dur="5s" repeatCount="indefinite"/>
  </circle>
</svg>

视频背景实现

使用HTML5 video元素作为动态背景:

<video autoplay muted loop id="bgVideo">
  <source src="background.mp4" type="video/mp4">
</video>

<style>
  #bgVideo {
    position: fixed;
    right: 0;
    bottom: 0;
    min-width: 100%;
    min-height: 100%;
    z-index: -1;
    filter: brightness(0.6);
  }
</style>

标签: 背景动态
分享给朋友:

相关文章

elementui动态

elementui动态

ElementUI 动态组件与数据绑定 ElementUI 支持动态渲染组件和数据绑定,常用于表格、表单等场景的动态生成。通过 v-for 和动态属性绑定实现组件灵活配置。 动态生成表格列 &…

vue动态实现select

vue动态实现select

Vue 动态实现 Select 组件 在 Vue 中动态实现 Select 组件可以通过多种方式完成,以下介绍几种常见的方法: 使用 v-for 动态渲染选项 通过 v-for 指令可以动态渲染 s…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…

vue实现动态按钮

vue实现动态按钮

实现动态按钮的基本思路 在Vue中实现动态按钮通常涉及根据数据状态动态改变按钮的样式、文本或行为。可以通过绑定动态类名、样式或事件来实现。 动态绑定按钮样式 使用v-bind:class或简写:cl…

vue实现动态换肤

vue实现动态换肤

Vue 动态换肤实现方法 动态换肤是前端开发中常见的需求,Vue 可以通过多种方式实现这一功能。以下是几种常见的实现方法: CSS 变量结合 Vuex 或 Pinia 定义全局 CSS 变量,通过…

vue实现动态筛选

vue实现动态筛选

Vue实现动态筛选的方法 动态筛选是Vue应用中常见的功能需求,可以通过多种方式实现。以下是几种常用的实现方法: 使用计算属性实现筛选 计算属性是Vue中实现动态筛选的理想选择,它会根据依赖的数据…