html5 css 粒子特效

HTML5和CSS3的出现让 web 开发变得更加有趣和生动。其中,CSS3中的粒子特效更是给我们带来了无限展示的可能。下面将详细介绍 HTML5 CSS 粒子特效以及 HTML5 标签的属性值。

一、什么是粒子特效

粒子特效是一种能够显示出像雨滴、火花、烟雾、气泡等微观物质结构运动轨迹的效果。粒子特效通过多个小点组成的粒子来模拟实物的运动和效果,给人一种立体、逼真的感觉。通过粒子特效,我们可以给网页上的元素增加动态效果,让页面看起来更加生动,也可以用来创建一些有趣的背景。

二、HTML5中使用粒子特效

HTML5和CSS3都有内置的粒子特效属性,可以通过这些属性创建不同的粒子效果。

1. CSS的粒子特效

CSS3中的粒子效果主要是通过伪元素:before或:after设置的,然后调整它们的属性来达到各种效果。

(1)创建火花

我们可以通过设定“box-shadow”来给元素创建火花。下面是一个例子:

```

.sparkle {

width: 50px;

height: 50px;

border-radius: 50%;

box-shadow: 0 0 20px red;

}

```

(2)创建下雨效果

我们可以通过设置"background"属性,然后运用 "linear-gradient" 来创建下雨特效。下面是一个例子:

```

.rain {

width: 200px;

height: 200px;

position: relative;

background: linear-gradient(to bottom, rgba(255,255,255,0.1) 0%,rgba(255,255,255,0) 100%), url(https://i.imgur.com/aRNVfR5.png) repeat 0 0;

background-size: contain;

animation: downpour 1.5s linear infinite;

overflow: hidden;

}

.rain:before, .rain:after {

content: "";

position: absolute;

background: white;

width: 100px;

height: 200px;

box-shadow: 0 0 10px white, 0 0 20px white, 0 0 30px white;

transform: skew(-20deg) rotate(-6deg);

top: -100px;

left: 50%;

margin-left: -50px;

}

```

2. HTML中的Canvas元素

Canvas是 HTML5 中新增的标记之一,用于创建动态、交互式的图形效果。我们可以使用它来实现复杂的粒子效果,如烟雾、气泡等。在使用Canvas元素的时候,我们需要编写一些 JavaScript 代码来实现我们需要的效果。下面是一个实现气泡特效的例子:

```

var canvas = document.getElementById("canvas");

var ctx = canvas.getContext("2d");

var w = canvas.width = window.innerWidth;

var h = canvas.height = window.innerHeight;

function random(min, max) {

return min + Math.random() * (max - min);

}

function Ball(x, y, radius, color) {

this.x = x || Math.floor(Math.random() * w);

this.y = y || Math.floor(Math.random() * h);

this.radius = radius || random(20, 50);

this.color = color || "rgba(255, 255, 255, .5)";

this.speed = random(1, 5);

this.angle = random(0, Math.PI * 2);

this.vx = Math.cos(this.angle) * this.speed;

this.vy = Math.sin(this.angle) * this.speed;

this.ballArr = [];

}

Ball.prototype.draw = function() {

ctx.beginPath();

ctx.fillStyle = this.color;

ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);

ctx.fill();

};

Ball.prototype.move = function() {

if (this.x <= 0 + this.radius) {

this.angle = Math.PI - this.angle;

} else if (this.x >= w - this.radius) {

this.angle = Math.PI - this.angle;

}

if (this.y <= 0 + this.radius) {

this.angle = -this.angle;

} else if (this.y >= h - this.radius) {

this.angle = -this.angle;

}

this.x += this.vx;

this.y += this.vy;

this.vx += this.vx * random(0.01, 0.03);

this.vy += this.vy * random(0.01, 0.03);

this.draw();

};

for (var i = 0; i < 50; i++) {

var ball = new Ball();

ball.draw();

ball.move();

ball.ballArr.push(ball);

}

function loop() {

for (var i = 0; i < ball.ballArr.length; i++) {

var bll = ball.ballArr[i];

bll.move();

bll.draw();

}

requestAnimationFrame(loop);

}

loop();

```

三、HTML5标签的属性值

除了通过CSS3和Canvas来实现粒子特效外,HTML5 中的标签也提供了一些属性值可以用来实现粒子效果。

1. 在Canvas中使用ParticlesJS库

ParticlesJS 是一个用于渲染 Canvas 粒子效果的 JavaScript 库。它提供了一个简单的 API 接口,让我们可以轻松在网页中添加动态效果。下面是通过 ParticlesJS 在 Canvas 上添加粒子特效的示例:

```

```

```

/* app.js */

particlesJS("particles-js", {

particles: {

number: {

value: 80,

density: {

enable: true,

value_area: 800

}

},

color: {

value: "#ffffff"

},

shape: {

type: "circle",

stroke: {

width: 0,

color: "#000000"

},

polygon: {

nb_sides: 5

},

image: {

src: "img/github.svg",

width: 100,

height: 100

}

},

opacity: {

value: 0.5,

random: false,

anim: {

enable: false,

speed: 1,

opacity_min: 0.1,

sync: false

}

},

size: {

value: 4,

random: true,

anim: {

enable: false,

speed: 40,

size_min: 0.1,

sync: false

}

},

line_linked: {

enable: true,

distance: 150,

color: "#ffffff",

opacity: 0.4,

width: 1

},

move: {

enable: true,

speed: 6,

direction: "none",

random: false,

straight: false,

out_mode: "out",

bounce: false,

attract: {

enable: false,

rotateX: 600,

rotateY: 1200

}

}

},

interactivity: {

detect_on: "canvas",

events: {

onhover: {

enable: true,

mode: "repulse"

},

onclick: {

enable: true,

mode: "push"

},

resize: true

},

modes: {

grab: {

distance: 400,

line_linked: {

opacity: 1

}

},

bubble: {

distance: 400,

size: 40,

duration: 2,

opacity: 8,

speed: 3

},

repulse: {

distance: 200,

duration: 0.4

},

push: {

particles_nb: 4

},

remove: {

particles_nb: 2

}

}

},

retina_detect: true

});

```

2. 使用SVG元素

我们可以使用 SVG 的精确控制来设置各种粒子特效。 SVG 可以轻松地创建任何形状或自定义模板。实现 SVG 粒子特效,需要用到SVG元素和 JavaScript。

```

Single Particle SVG Animation

```

通过调节 SVG 的属性,我们可以创建各种不同的粒子特效。

总结

HTML5和CSS3让我们可以轻松地实现各种粒子特效,吸引用户的眼球。我们可以使用 CSS3 提供的伪元素、Box Shadow,Canvas画布和第三方库(如ParticlesJS),以及传统的SVG元素和 JavaScript 来实现。下次你在开发网页时,你可以使用这些技术来实现更加生动和有趣的效果。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/

点赞(60) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部