<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>魔幻滚动视差特效</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
color: white;
overflow-x: hidden;
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
min-height: 100vh;
}
.parallax-container {
position: relative;
height: 100vh;
perspective: 8px;
overflow-x: hidden;
overflow-y: scroll;
}
.background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-origin: 0;
transform: translateZ(0px);
background: url('https://images.unsplash.com/photo-1534796636912-3b95b3ab5986?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') center/cover;
z-index: -1;
}
.midground {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-origin: 0;
transform: translateZ(3px) scale(0.625);
z-index: -1;
}
.foreground {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-origin: 0;
transform: translateZ(5px) scale(0.375);
z-index: -1;
}
.content {
position: absolute;
top: 0;
left: 0;
width: 100%;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 20px;
text-align: center;
transform: translateZ(0);
}
h1 {
font-size: 4rem;
margin-bottom: 2rem;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
background: linear-gradient(90deg, #ff8a00, #e52e71);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: glow 2s ease-in-out infinite alternate;
}
p {
font-size: 1.5rem;
max-width: 800px;
margin-bottom: 2rem;
line-height: 1.6;
text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}
.scroll-down {
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 1.2rem;
animation: bounce 2s infinite;
}
.section {
min-height: 100vh;
padding: 50px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
margin-top: 50vh;
border-radius: 20px;
backdrop-filter: blur(5px);
box-shadow: 0 0 20px rgba(255, 255, 255, 0.1);
}
.stars {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -2;
}
.star {
position: absolute;
background-color: white;
border-radius: 50%;
animation: twinkle var(--duration) infinite ease-in-out;
}
.magic-link {
color: #e52e71;
text-decoration: none;
font-weight: bold;
transition: all 0.3s ease;
display: inline-block;
margin-top: 20px;
padding: 10px 20px;
border: 2px solid #e52e71;
border-radius: 30px;
background: rgba(0, 0, 0, 0.3);
}
.magic-link:hover {
color: #ff8a00;
border-color: #ff8a00;
transform: scale(1.05);
box-shadow: 0 0 15px rgba(255, 138, 0, 0.5);
}
@keyframes glow {
from {
text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}
to {
text-shadow: 0 0 20px rgba(255, 255, 255, 0.8), 0 0 30px rgba(255, 138, 0, 0.6);
}
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0) translateX(-50%);
}
40% {
transform: translateY(-20px) translateX(-50%);
}
60% {
transform: translateY(-10px) translateX(-50%);
}
}
@keyframes twinkle {
0%, 100% {
opacity: 0.2;
}
50% {
opacity: 1;
}
}
/* 响应式设计 */
@media (max-width: 768px) {
h1 {
font-size: 2.5rem;
}
p {
font-size: 1.2rem;
}
}
</style>
</head>
<body>
<div class="parallax-container">
<div class="stars" id="stars"></div>
<div class="background"></div>
<div class="midground"></div>
<div class="foreground"></div>
<div class="content">
<h1>魔幻滚动视差世界</h1>
<p>滚动页面体验神奇的视差效果,感受深度与维度的奇妙变化。</p>
<div class="scroll-down">向下滚动 ▼</div>
</div>
<div class="section">
<h2>探索魔幻世界</h2>
<p>这是一个由HTML和CSS创建的魔幻视差滚动效果。当你滚动页面时,不同层次的元素会以不同的速度移动,创造出令人惊叹的3D效果。</p>
<p>视差滚动是现代网页设计中常用的技术,可以为用户提供沉浸式的浏览体验。</p>
</div>
<div class="section">
<h2>技术实现</h2>
<p>这个效果主要通过CSS的transform属性和perspective属性实现。背景元素被赋予不同的translateZ值,使它们在滚动时产生不同的位移速度。</p>
<p>我们还添加了闪烁的星星和渐变文字效果,增强魔幻氛围。</p>
</div>
<div class="section">
<h2>关于我们</h2>
<p>我们致力于创造令人惊叹的网页体验,将艺术与技术完美结合。</p>
<p>如果你喜欢这个效果,可以分享给你的朋友或用于你的个人项目。</p>
</div>
</div>
<script>
// 创建星星背景
function createStars() {
const starsContainer = document.getElementById('stars');
const starCount = 200;
for (let i = 0; i < starCount; i++) {
const star = document.createElement('div');
star.classList.add('star');
// 随机大小
const size = Math.random() * 3;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
// 随机位置
star.style.left = `${Math.random() * 100}%`;
star.style.top = `${Math.random() * 100}%`;
// 随机动画持续时间
const duration = 2 + Math.random() * 3;
star.style.setProperty('--duration', `${duration}s`);
starsContainer.appendChild(star);
}
}
// 视差效果
function updateParallax() {
const scrollY = window.scrollY || window.pageYOffset;
const background = document.querySelector('.background');
const midground = document.querySelector('.midground');
const foreground = document.querySelector('.foreground');
background.style.transform = `translateZ(0px) translateY(${scrollY * 0.5}px)`;
midground.style.transform = `translateZ(3px) scale(0.625) translateY(${scrollY * 0.3}px)`;
foreground.style.transform = `translateZ(5px) scale(0.375) translateY(${scrollY * 0.1}px)`;
}
// 初始化
window.addEventListener('load', () => {
createStars();
updateParallax();
});
window.addEventListener('scroll', updateParallax);
// 响应式调整
window.addEventListener('resize', () => {
document.getElementById('stars').innerHTML = '';
createStars();
});
</script>
</body>
</html>