✨博主:命运之光
🌸专栏:Python星辰秘典
🐳专栏:web开发(简单好用又好看)
❤️专栏:Java经典程序设计
☀️博主的其他文章:点击进入博主的主页
前言:欢迎踏入我的Web项目专栏,一段神奇而令人陶醉的数字世界!
🌌在这里,我将带您穿越时空,揭开属于Web的奥秘。通过HTML、CSS和JavaScript的魔力,我创造了一系列令人惊叹的Web项目,它们仿佛是从梦境中涌现而出。
🌌在这个专栏中,您将遇到华丽的界面,如流星划过夜空般迷人;您将感受到动态的交互,如魔法般让您沉浸其中;您将探索响应式设计的玄妙,让您的屏幕变幻出不同的绚丽景象。
🌌无论您是一个探险家还是一位嗜血的代码巫师,这个专栏将成为您的魔法书。我将分享每个项目的秘密,解开编码的谜题,让您也能够拥有制作奇迹的力量。
🌌准备好了吗?拿起您的键盘,跟随我的指引,一起进入这个神秘而充满惊喜的数字王国。在这里,您将找到灵感的源泉,为自己创造出一段奇幻的Web之旅!
目录
🍓2.将上面的源代码复制粘贴到记事本里面将文件另存为HTML文件点击保存即可
引言
在现代互联网时代,用户对于网页的要求越来越高,除了内容的丰富和易读性外,视觉效果也成为吸引用户的重要因素之一。本文将向您展示如何利用HTML5和JavaScript创建一个视觉效果震撼的交互式网页,让您的网站在视觉上脱颖而出,吸引更多访问者。
动态图展示
静态图展示
图1
图2
图3
图4
技术背景
在这个技术快速发展的时代,利用HTML5和JavaScript创建交互式的视觉效果已经成为前端开发的重要趋势。HTML5的<canvas>元素提供了一个强大的绘图环境,结合JavaScript的动态特性,我们可以轻松实现各种视觉效果,例如粒子效果、动画和过渡效果等。
实现视觉效果震撼的网页
下面是一个简单的HTML文档,通过HTML5的<canvas>元素和JavaScript代码,创建了一个令人惊叹的视觉效果。该网页会在用户的鼠标移动时,产生绚丽多彩的粒子效果,为用户带来沉浸式的交互体验。
<!DOCTYPE html>
<html>
<head>
<title>绚丽粒子炸裂效果</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// JavaScript代码部分
// ...(以下省略JavaScript代码)
</script>
</body>
</html>
JavaScript代码解析
JavaScript代码部分负责创建粒子效果和实现动画效果。我们使用一个Particle类来表示每个粒子,每个粒子具有随机的大小、颜色和速度,使得粒子在画布上随机运动。
// JavaScript代码部分
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// 设置画布大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 10 + 2;
this.speedX = (Math.random() - 0.5) * 5;
this.speedY = (Math.random() - 0.5) * 5;
this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.size > 0.2) this.size -= 0.1;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
const particles = [];
function createParticles(x, y) {
for (let i = 0; i < 10; i++) {
const particle = new Particle(x, y);
particles.push(particle);
}
}
function animate() {
requestAnimationFrame(animate);
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
if (particles[i].size <= 0.3) {
particles.splice(i, 1);
i--;
}
}
}
canvas.addEventListener("mousemove", (e) => {
const { offsetX, offsetY } = e;
createParticles(offsetX, offsetY);
});
animate();
项目完整代码
<!DOCTYPE html>
<html>
<head>
<title>绚丽粒子炸裂效果</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// 设置画布大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 10 + 2;
this.speedX = (Math.random() - 0.5) * 5;
this.speedY = (Math.random() - 0.5) * 5;
this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.size > 0.2) this.size -= 0.1;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
const particles = [];
function createParticles(x, y) {
for (let i = 0; i < 10; i++) {
const particle = new Particle(x, y);
particles.push(particle);
}
}
function animate() {
requestAnimationFrame(animate);
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
if (particles[i].size <= 0.3) {
particles.splice(i, 1);
i--;
}
}
}
canvas.addEventListener("mousemove", (e) => {
const { offsetX, offsetY } = e;
createParticles(offsetX, offsetY);
});
animate();
</script>
</body>
</html>
代码的使用方法(超简单什么都不用下载)
🍓1.打开记事本
🍓2.将上面的源代码复制粘贴到记事本里面将文件另存为HTML文件点击保存即可
🍓3.打开html文件(大功告成(●'◡'●))
结语
本章的内容就到这里了,觉得对你有帮助的话就支持一下博主把~
🌌点击下方个人名片,交流会更方便哦~
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓