# 實用的HTML特效代碼有哪些
## 前言
在當今的網頁設計中,HTML特效已經成為提升用戶體驗和視覺吸引力的重要手段。本文將深入探討20類實用HTML特效的實現原理和應用場景,涵蓋從基礎動畫到前沿Web技術的完整解決方案。每種特效都配有可立即使用的代碼示例和詳細的技術解析,幫助開發者快速掌握網頁動態效果的實現技巧。
## 一、懸浮動畫效果
### 1.1 CSS懸浮放大效果
```html
<!DOCTYPE html>
<html>
<head>
<style>
.zoom-box {
width: 200px;
height: 200px;
transition: transform 0.3s;
background-color: #3498db;
}
.zoom-box:hover {
transform: scale(1.1);
}
</style>
</head>
<body>
<div class="zoom-box"></div>
</body>
</html>
技術原理:通過CSS的transform
屬性和transition
實現平滑的縮放動畫。scale(1.1)
表示放大到原始尺寸的110%,transition控制動畫持續時間。
應用場景:產品展示、圖片畫廊、按鈕交互等需要突出顯示元素的場合。
<style>
.shadow-card {
width: 300px;
padding: 20px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
transition: box-shadow 0.3s ease;
}
.shadow-card:hover {
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
</style>
優化建議:可以結合transform: translateY(-5px)
實現懸浮上升效果,增強立體感。
<style>
.parallax-container {
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
perspective: 1px;
}
.parallax-layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.parallax-layer-base {
transform: translateZ(0);
}
.parallax-layer-back {
transform: translateZ(-1px) scale(2);
}
</style>
<div class="parallax-container">
<div class="parallax-layer parallax-layer-base">前景內容</div>
<div class="parallax-layer parallax-layer-back">背景內容</div>
</div>
window.addEventListener('scroll', function() {
const scrollY = window.scrollY;
document.querySelector('.parallax-element').style.transform =
`translateY(${scrollY * 0.5}px)`;
});
性能優化:使用requestAnimationFrame
優化滾動事件處理,避免布局抖動。
<canvas id="particleCanvas"></canvas>
<script>
const canvas = document.getElementById('particleCanvas');
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 = -particle.speedX;
}
if(particle.y < 0 || particle.y > canvas.height) {
particle.speedY = -particle.speedY;
}
ctx.fillStyle = 'rgba(255,255,255,0.8)';
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fill();
});
requestAnimationFrame(animate);
}
animate();
</script>
進階實現:可以添加粒子連線、鼠標交互、顏色漸變等效果增強視覺表現力。
<style>
.flip-card {
width: 300px;
height: 200px;
perspective: 1000px;
}
.flip-card-inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
transform: rotateY(180deg);
}
.flip-card-front, .flip-card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.flip-card-back {
transform: rotateY(180deg);
}
</style>
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<img src="front.jpg" style="width:100%;height:100%;">
</div>
<div class="flip-card-back">
<h2>詳細信息</h2>
<p>這里是卡片的背面內容</p>
</div>
</div>
</div>
兼容性說明:在舊版瀏覽器中需要添加各瀏覽器前綴如-webkit-transform
等。
<svg width="500" height="100">
<path id="svgPath" d="M10,50 C100,100 200,0 300,50 S400,100 490,50"
stroke="#3498db" stroke-width="2" fill="none"/>
</svg>
<script>
const path = document.getElementById('svgPath');
const length = path.getTotalLength();
path.style.strokeDasharray = length;
path.style.strokeDashoffset = length;
window.addEventListener('scroll', function() {
const scrollPercent = (document.body.scrollTop +
document.documentElement.scrollTop) /
(document.documentElement.scrollHeight -
document.documentElement.clientHeight);
const draw = length * scrollPercent;
path.style.strokeDashoffset = length - draw;
});
</script>
<svg width="500" height="200">
<path d="M100,100 C150,50 200,150 250,100" fill="none" stroke="#333">
<animate attributeName="d"
values="M100,100 C150,50 200,150 250,100;
M100,100 C150,150 200,50 250,100;
M100,100 C150,50 200,150 250,100"
dur="5s" repeatCount="indefinite"/>
</path>
</svg>
<div id="typewriter"></div>
<script>
const text = "這是一個模擬打字機效果的文字動畫...";
let i = 0;
function typeWriter() {
if (i < text.length) {
document.getElementById("typewriter").innerHTML += text.charAt(i);
i++;
setTimeout(typeWriter, Math.random() * 100 + 50);
}
}
typeWriter();
</script>
<style>
.fade-in-text {
opacity: 0;
animation: fadeIn 2s ease-in forwards;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.delay-1 { animation-delay: 0.5s; }
.delay-2 { animation-delay: 1s; }
</style>
<h1 class="fade-in-text">標題</h1>
<p class="fade-in-text delay-1">第一段內容</p>
<p class="fade-in-text delay-2">第二段內容</p>
<style>
.nav-toggle {
display: none;
}
.hamburger {
display: none;
cursor: pointer;
}
@media (max-width: 768px) {
.hamburger {
display: block;
}
.nav-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
width: 100%;
background: #333;
}
.nav-toggle:checked ~ .nav-menu {
display: block;
}
}
</style>
<label for="nav-toggle" class="hamburger">?</label>
<input type="checkbox" id="nav-toggle" class="nav-toggle">
<nav class="nav-menu">
<a href="#">首頁</a>
<a href="#">產品</a>
<a href="#">關于</a>
<a href="#">聯系</a>
</nav>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 160px;
box-shadow: 0px 8px 16px rgba(0,0,0,0.2);
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
<div class="dropdown">
<button>下拉菜單</button>
<div class="dropdown-content">
<a href="#">選項1</a>
<a href="#">選項2</a>
<a href="#">選項3</a>
</div>
</div>
<style>
.loader {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
transition: opacity 0.5s, visibility 0.5s;
}
.loader-hidden {
opacity: 0;
visibility: hidden;
}
.loader::after {
content: "";
width: 75px;
height: 75px;
border: 15px solid #dddddd;
border-top-color: #009578;
border-radius: 50%;
animation: loading 0.75s ease infinite;
}
@keyframes loading {
from { transform: rotate(0turn); }
to { transform: rotate(1turn); }
}
</style>
<div class="loader"></div>
<script>
window.addEventListener('load', function() {
const loader = document.querySelector('.loader');
loader.classList.add('loader-hidden');
loader.addEventListener('transitionend', function() {
document.body.removeChild(loader);
});
});
</script>
<style>
.page {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
transform: translateX(-20px);
transition: opacity 0.3s, transform 0.3s;
}
.page.active {
opacity: 1;
transform: translateX(0);
}
.page.exit {
opacity: 0;
transform: translateX(20px);
}
</style>
<script>
function navigateTo(pageId) {
const currentPage = document.querySelector('.page.active');
const nextPage = document.getElementById(pageId);
if(currentPage) {
currentPage.classList.remove('active');
currentPage.classList.add('exit');
currentPage.addEventListener('transitionend', function() {
currentPage.classList.remove('exit');
}, { once: true });
}
nextPage.classList.add('active');
}
</script>
<style>
.input-group {
position: relative;
margin: 20px 0;
}
.input-group input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.input-group label {
position: absolute;
top: 10px;
left: 10px;
color: #999;
transition: all 0.3s;
pointer-events: none;
}
.input-group input:focus + label,
.input-group input:not(:placeholder-shown) + label {
top: -10px;
left: 5px;
font-size: 12px;
background: white;
padding: 0 5px;
color: #3498db;
}
</style>
<div class="input-group">
<input type="text" id="username" placeholder=" ">
<label for="username">用戶名</label>
</div>
<style>
.password-strength {
height: 5px;
margin-top: 5px;
background: #eee;
}
.strength-0 { width: 20%; background: #ff4d4d; }
.strength-1 { width: 40%; background: #ff9933; }
.strength-2 { width: 60%; background: #ffcc00; }
.strength-3 { width: 80%; background: #99cc33; }
.strength-4 { width: 100%; background: #33cc33; }
</style>
<input type="password" id="password" oninput="checkStrength(this.value)">
<div class="password-strength" id="strengthBar"></div>
<script>
function checkStrength(password) {
let strength = 0;
if(password.length > 0) strength++;
if(password.length >= 8) strength++;
if(/[A-Z]/.test(password)) strength++;
if(/\d/.test(password)) strength++;
if(/[^A-Za-z0-9]/.test(password)) strength++;
document.getElementById('strengthBar').className =
`password-strength strength-${strength}`;
}
</script>
<style>
.parallax {
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
perspective: 1px;
}
.parallax-layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.layer-1 {
transform: translateZ(-3px) scale(4);
}
.layer-2 {
transform: translateZ(-2px) scale(3);
}
.layer-3 {
transform: translateZ(-1px) scale(2);
}
.layer-content {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="parallax">
<div class="parallax-layer layer-1">
<div class="layer-content">背景層</div>
</div>
<div class="parallax-layer layer-2">
<div class="layer-content">中間層</div>
</div>
<div class="parallax-layer layer-3">
<div class="layer-content">前景層</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rellax/1.12.1/rellax.min.js"></script>
<div class="rellax" data-rellax-speed="-2">慢速滾動的背景元素</div>
<div class="rellax" data-rellax-speed="3">快速滾動的前景元素</div>
<script>
var rellax = new Rellax('.rellax');
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
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();
</script>
”`javascript // 在Three.js場景中創建粒子系統 const particleCount = 10000; const particles = new THREE.BufferGeometry(); const positions = new Float32Array(particleCount * 3);
for(let i = 0; i < particleCount; i++) { positions[i * 3] = (Math.random() - 0.5) * 2000; positions[i * 3 + 1] = (Math.random() - 0.5) * 2000; positions[i * 3 + 2] = (Math.random() - 0.5) * 2000; }
particles.setAttribute(‘position’, new THREE.BufferAttribute(positions, 3)); const particleMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 2, transparent: true, opacity: 0.8 });
const particleSystem = new
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。