HTML5白云飄飄動態效果教程
這里寫目錄標題
- HTML5白云飄飄動態效果教程
- 效果介紹
- 實現步驟
- 步驟一:創建HTML結構
- 步驟二:設計CSS樣式
- 步驟三:添加JavaScript交互
- 代碼解析
- HTML結構解析
- CSS樣式解析
- JavaScript功能解析
- 自定義調整
- 總結
效果介紹
本教程將教你如何使用純HTML5、CSS3和JavaScript創建一個優美的白云飄飄動態效果。最終效果包括:
- 多朵白云從左向右飄動
- 云朵大小、位置、速度和透明度各不相同
- 動態生成隨機云朵
- 鼠標互動效果(移動鼠標時云朵會輕微跟隨)
實現步驟
步驟一:創建HTML結構
首先,我們需要創建基本的HTML結構:
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>白云飄飄動態效果</title><link rel="stylesheet" href="style.css">
</head>
<body><div class="sky"><div class="cloud cloud1"></div><div class="cloud cloud2"></div><div class="cloud cloud3"></div><div class="cloud cloud4"></div><div class="cloud cloud5"></div></div><script src="script.js"></script>
</body>
</html>
這里我們創建了一個名為sky
的容器,內部放置了5個基礎云朵元素。
步驟二:設計CSS樣式
接下來,創建style.css
文件,設計云朵的樣式和動畫效果:
* {margin: 0;padding: 0;box-sizing: border-box;
}body {overflow: hidden;background: linear-gradient(to bottom, #87CEEB, #E0F7FF);height: 100vh;width: 100%;
}.sky {width: 100%;height: 100%;position: relative;
}/* 云朵基本樣式 */
.cloud {position: absolute;background: white;border-radius: 50px;filter: drop-shadow(3px 5px 5px rgba(0, 0, 0, 0.1));
}/* 使用偽元素創建云朵的圓形部分 */
.cloud:before, .cloud:after {content: '';position: absolute;background: white;border-radius: 50%;
}.cloud:before {width: 50px;height: 50px;top: -30px;left: 15px;
}.cloud:after {width: 70px;height: 70px;top: -35px;right: 15px;
}/* 各個云朵的特定樣式 */
.cloud1 {width: 150px;height: 60px;top: 10%;left: -150px;opacity: 0.9;animation: moveCloud 35s linear infinite;
}.cloud2 {width: 120px;height: 50px;top: 25%;left: -120px;opacity: 0.85;animation: moveCloud 45s linear infinite;animation-delay: 5s;
}.cloud3 {width: 180px;height: 70px;top: 40%;left: -180px;opacity: 0.8;animation: moveCloud 40s linear infinite;animation-delay: 10s;
}.cloud4 {width: 100px;height: 40px;top: 60%;left: -100px;opacity: 0.75;animation: moveCloud 50s linear infinite;animation-delay: 15s;
}.cloud5 {width: 160px;height: 65px;top: 75%;left: -160px;opacity: 0.7;animation: moveCloud 38s linear infinite;animation-delay: 20s;
}/* 定義云朵移動動畫 */
@keyframes moveCloud {from {left: -300px;}to {left: 100%;}
}
步驟三:添加JavaScript交互
最后,創建script.js
文件,添加動態效果和交互功能:
document.addEventListener('DOMContentLoaded', function() {const sky = document.querySelector('.sky');// 隨機創建更多云朵function createClouds() {const extraClouds = 10; // 額外創建的云朵數量for (let i = 0; i < extraClouds; i++) {const cloud = document.createElement('div');cloud.classList.add('cloud');// 隨機大小const size = Math.random() * 100 + 80;cloud.style.width = `${size}px`;cloud.style.height = `${size / 3}px`;// 隨機位置const top = Math.random() * 90; // 0-90% 的高度cloud.style.top = `${top}%`;// 隨機透明度const opacity = Math.random() * 0.4 + 0.5; // 0.5-0.9cloud.style.opacity = opacity;// 隨機速度const duration = Math.random() * 30 + 30; // 30-60秒cloud.style.animation = `moveCloud ${duration}s linear infinite`;// 隨機延遲const delay = Math.random() * 30;cloud.style.animationDelay = `${delay}s`;// 隨機初始位置const startPosition = Math.random() * 100;cloud.style.left = `${startPosition}%`;// 添加偽元素樣式cloud.style.position = 'absolute';cloud.style.background = 'white';cloud.style.borderRadius = '50px';cloud.style.filter = 'drop-shadow(3px 5px 5px rgba(0, 0, 0, 0.1))';sky.appendChild(cloud);}}// 當頁面加載完成后創建云朵createClouds();// 對云朵添加鼠標互動效果document.addEventListener('mousemove', function(e) {// 計算鼠標在頁面上的相對位置(0-1)const mouseX = e.clientX / window.innerWidth;const mouseY = e.clientY / window.innerHeight;// 獲取所有云朵const clouds = document.querySelectorAll('.cloud');// 為每個云朵添加輕微移動效果clouds.forEach(cloud => {const moveX = (mouseX - 0.5) * 10; // -5 到 5 像素的水平移動const moveY = (mouseY - 0.5) * 5; // -2.5 到 2.5 像素的垂直移動// 應用變換cloud.style.transform = `translate(${moveX}px, ${moveY}px)`;});});
});
代碼解析
HTML結構解析
<div class="sky">
作為整個場景的容器- 內部包含5個基礎云朵,每個云朵都有獨特的類名(cloud1-cloud5)
CSS樣式解析
-
云朵造型:
- 使用圓角矩形作為云朵的主體
- 通過
:before
和:after
偽元素添加兩個圓形,形成完整的云朵形狀 - 使用
filter: drop-shadow
添加輕微陰影,增強立體感
-
動畫效果:
- 使用
@keyframes moveCloud
定義云朵從左到右的移動軌跡 - 每個云朵設置不同的動畫持續時間和延遲,使移動看起來更自然
- 不同云朵設置不同的透明度,模擬遠近感
- 使用
JavaScript功能解析
-
動態生成云朵:
createClouds()
函數隨機生成額外的云朵- 每個云朵的大小、位置、透明度、速度和延遲都是隨機的
- 這使得整個場景更加豐富和自然
-
鼠標交互:
- 監聽
mousemove
事件,獲取鼠標位置 - 根據鼠標位置計算云朵的輕微位移
- 使用
transform: translate()
應用位移效果
- 監聽
自定義調整
你可以根據需要調整以下參數來改變效果:
-
背景顏色:
body {background: linear-gradient(to bottom, #新顏色1, #新顏色2); }
-
云朵數量:
const extraClouds = 20; // 增加或減少云朵數量
-
云朵速度:
.cloud1 {animation: moveCloud 20s linear infinite; // 減小數值加快速度 }
-
鼠標互動靈敏度:
const moveX = (mouseX - 0.5) * 20; // 增大數值增強互動效果 const moveY = (mouseY - 0.5) * 10;
總結
通過這個教程,你學會了如何使用HTML5、CSS3和JavaScript創建一個白云飄飄的動態效果。這個效果可以應用于各種網頁場景,如:
- 網站背景
- 登錄頁面
- 天氣相關應用
- 兒童教育網站
- 休閑游戲背景
希望這個教程對你有所幫助!你可以根據自己的需求進一步擴展和優化這個效果。