html5好玩的特效代码
时间: 2023-09-22 13:07:46 浏览: 164
以下是几个有趣的 HTML5 特效代码,供您参考:
1. 粒子效果
```html
<!DOCTYPE html>
<html>
<head>
<style>
#particles {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
}
</style>
</head>
<body>
<canvas id="particles"></canvas>
<script>
var canvas = document.getElementById('particles');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var particles = [];
function Particle() {
this.x = canvas.width * Math.random();
this.y = canvas.height * Math.random();
this.vx = Math.random() * 10 - 5;
this.vy = Math.random() * 10 - 5;
this.size = Math.random() * 3 + 1;
this.opacity = Math.random();
}
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
if (this.x < 0 || this.x > canvas.width) {
this.vx = -this.vx;
}
if (this.y < 0 || this.y > canvas.height) {
this.vy = -this.vy;
}
this.opacity -= 0.01;
if (this.opacity < 0) {
this.opacity = 0;
}
};
Particle.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255, 255, 255, ' + this.opacity + ')';
ctx.fill();
};
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
if (particles[i].opacity === 0) {
particles.splice(i, 1);
}
}
if (particles.length < 1000) {
particles.push(new Particle());
}
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
```
2. 水波纹效果
```html
<!DOCTYPE html>
<html>
<head>
<style>
#ripple {
position: relative;
width: 300px;
height: 300px;
margin: 50px auto;
background-color: #eee;
overflow: hidden;
}
#ripple:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-image: radial-gradient(circle, #fff 10%, #000 90%);
background-position: center;
background-repeat: no-repeat;
background-size: 0 0;
opacity: 0.7;
transition: background-size 0.3s ease-out;
}
</style>
</head>
<body>
<div id="ripple"></div>
<script>
var ripple = document.getElementById('ripple');
ripple.addEventListener('click', function(e) {
var x = e.clientX - ripple.offsetLeft;
var y = e.clientY - ripple.offsetTop;
var size = 0;
var interval = setInterval(function() {
size += 5;
ripple.style.backgroundSize = size + 'px ' + size + 'px';
ripple.style.backgroundPosition = x + 'px ' + y + 'px';
if (size > 300) {
clearInterval(interval);
ripple.style.backgroundSize = '0 0';
}
}, 30);
});
</script>
</body>
</html>
```
3. 3D 立方体翻转效果
```html
<!DOCTYPE html>
<html>
<head>
<style>
.container {
perspective: 1000px;
perspective-origin: center;
width: 400px;
height: 400px;
margin: 50px auto;
}
.cube {
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: transform 1s;
}
.cube:hover {
transform: rotateY(180deg);
}
.cube .face {
position: absolute;
width: 400px;
height: 400px;
background-color: #eee;
border: 1px solid #ccc;
line-height: 400px;
font-size: 48px;
text-align: center;
}
.cube .front {
transform: translateZ(200px);
}
.cube .back {
transform: rotateY(180deg) translateZ(200px);
}
</style>
</head>
<body>
<div class="container">
<div class="cube">
<div class="face front">Front</div>
<div class="face back">Back</div>
</div>
</div>
</body>
</html>
```
希望这些代码对您有帮助!
阅读全文