
姓名
专业
基本信息
关于我
在当今数字化的时代,拥有一个属于自己的个人空间网页是一件非常有意义的事情。本文将详细介绍如何运用 HTML、CSS 和 JavaScript 技术,构建一个类似宇宙的个人空间站网页。
这个个人空间站网页由首页、个人中心、作品展示和个人博客四个核心页面组成。整体设计以宇宙星空为主题,融入星球、星星和流星等元素,营造出神秘而奇幻的氛围。用户可以通过点击首页的星球图标,轻松导航至不同页面,开启探索之旅。
项目主要页面介绍:
每个页面都有相似的 HTML 结构,主要包括头部导航栏、主体内容区域和底部交互提示(部分页面有)。以下是导航栏的代码示例:
这段代码实现了一个响应式导航栏,在移动端会显示菜单切换按钮,方便用户操作。
CSS 部分是实现页面视觉效果的关键,通过定义背景、动画和布局样式,营造出宇宙星空的氛围。以下是一些关键的 CSS 代码:
body {
background: linear-gradient(135deg, #0a0f23 0%, #1a1f3a 100%);
position: relative;
overflow-x: hidden;
color: #e2e8f0;
min-height: 100vh;
font-family: 'Inter', sans-serif;
}
.star {
position: absolute;
background-color: white;
border-radius: 50%;
animation: twinkle 3s infinite ease-in-out;
}
@keyframes twinkle {
0%, 100% { opacity: 0.5; }
50% { opacity: 1; }
}
.planet {
position: absolute;
border-radius: 50%;
animation: float 20s infinite linear;
box-shadow: 0 0 30px rgba(113, 162, 202, 0.5);
}
@keyframes float {
0% {
transform: translateY(0) rotate(0deg);
}
100% {
transform: translateY(-30px) rotate(360deg);
}
}
上述代码定义了背景颜色、星星闪烁动画和星球漂浮动画,为页面增添了动态和神秘的氛围。
JavaScript 负责实现网页的交互效果,如导航栏的滚动效果、移动端菜单的切换等。以下是导航栏滚动效果的代码示例:
const navbar = document.getElementById('navbar');
const backToTopBtn = document.getElementById('back-to-top');
window.addEventListener('scroll', () => {
if (window.scrollY > 100) {
navbar.classList.add('shadow-lg');
navbar.style.backgroundColor = 'rgba(31, 41, 55, 0.95)';
navbar.style.borderBottomColor = 'rgba(255, 255, 255, 0.1)';
backToTopBtn.classList.remove('opacity-0', 'invisible');
backToTopBtn.classList.add('opacity-100', 'visible');
} else {
navbar.classList.remove('shadow-lg');
navbar.style.backgroundColor = 'rgba(31, 41, 55, 0.9)';
navbar.style.borderBottomColor = 'rgba(255, 255, 255, 0.1)';
backToTopBtn.classList.add('opacity-0', 'invisible');
backToTopBtn.classList.remove('opacity-100', 'visible');
}
});
这段代码实现了导航栏在滚动时的样式变化,以及返回顶部按钮的显示与隐藏,提升了用户体验。
首页的核心亮点是太阳系模型,通过 CSS 和 JavaScript 创建星球元素,并为其添加动画效果和点击事件。通过点击星球,可以跳转到对应的页面。同时,页面会动态生成星星背景,营造出星空的氛围。代码示例如下:
CSS部分
Javascript部分
// 生成星星背景
function generateStars() {
const starsContainer = document.getElementById('stars');
const count = 300;
for (let i = 0; i < count; i++) {
const star = document.createElement('div');
star.className = 'star';
const x = Math.random() * 100;
const y = Math.random() * 100;
const size = Math.random() * 3 + 1;
const delay = Math.random() * 5;
star.style.left = `${x}%`;
star.style.top = `${y}%`;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.animationDelay = `${delay}s`;
starsContainer.appendChild(star);
}
}
// 创建背景星球元素
function createBgPlanets() {
const container = document.getElementById('planet-bg');
// 创建大型背景星球
const planet1 = document.createElement('div');
planet1.className = 'bg-planet';
planet1.style.background = 'radial-gradient(circle at 30% 30%, #71a2ca, #4a6d8c)';
planet1.style.width = '180px';
planet1.style.height = '180px';
planet1.style.top = '15%';
planet1.style.right = '10%';
planet1.style.animationDuration = '80s';
planet1.style.filter = 'blur(2px)';
planet1.style.zIndex = '-1';
container.appendChild(planet1);
// 添加行星环
const ring1 = document.createElement('div');
ring1.className = 'bg-planet-ring';
ring1.style.width = '240px';
ring1.style.height = '50px';
ring1.style.top = 'calc(15% + 90px)';
ring1.style.right = 'calc(10% - 30px)';
ring1.style.zIndex = '-1';
container.appendChild(ring1);
// 创建中型背景星球
const planet2 = document.createElement('div');
planet2.className = 'bg-planet';
planet2.style.background = 'radial-gradient(circle at 30% 30%, #a27dcb, #6d4f8f)';
planet2.style.width = '120px';
planet2.style.height = '120px';
planet2.style.top = '30%';
planet2.style.left = '5%';
planet2.style.animationDuration = '60s';
planet2.style.animationDirection = 'reverse';
planet2.style.filter = 'blur(1.5px)';
planet2.style.zIndex = '-1';
container.appendChild(planet2);
// 创建小型背景星球
const planet3 = document.createElement('div');
planet3.className = 'bg-planet';
planet3.style.background = 'radial-gradient(circle at 30% 30%, #f7b267, #d98c4a)';
planet3.style.width = '80px';
planet3.style.height = '80px';
planet3.style.bottom = '20%';
planet3.style.left = '15%';
planet3.style.animationDuration = '40s';
planet3.style.filter = 'blur(1px)';
planet3.style.zIndex = '-1';
container.appendChild(planet3);
// 创建流星
createShootingStars();
}
// 创建流星效果
function createShootingStars() {
const container = document.getElementById('planet-bg');
// 创建多个流星
for (let i = 0; i < 3; i++) {
const star = document.createElement('div');
star.className = 'shooting-star';
star.style.width = Math.random() * 100 + 50 + 'px';
star.style.top = Math.random() * 30 + '%';
star.style.left = Math.random() * 30 + '%';
star.style.animationDelay = Math.random() * 10 + 's';
star.style.animationDuration = (Math.random() * 3 + 2) + 's';
container.appendChild(star);
}
}
// 鼠标移动3D效果
function handleMouseMove(e) {
const solarSystem = document.getElementById('solar-system');
const xAxis = (window.innerWidth / 2 - e.pageX) / 50;
const yAxis = (window.innerHeight / 2 - e.pageY) / 50;
solarSystem.style.transform = `rotateY(${xAxis}deg) rotateX(${yAxis}deg)`;
}
// 导航栏滚动效果
const navbar = document.getElementById('navbar');
window.addEventListener('scroll', () => {
if (window.scrollY > 100) {
navbar.classList.add('shadow-lg');
navbar.style.backgroundColor = 'rgba(31, 41, 55, 0.95)';
navbar.style.borderBottomColor = 'rgba(255, 255, 255, 0.1)';
} else {
navbar.classList.remove('shadow-lg');
navbar.style.backgroundColor = 'rgba(31, 41, 55, 0.9)';
navbar.style.borderBottomColor = 'rgba(255, 255, 255, 0.1)';
}
// 滚动时隐藏提示
const prompt = document.getElementById('interaction-prompt');
if (window.scrollY > 50) {
prompt.style.opacity = '0';
prompt.style.pointerEvents = 'none';
} else {
prompt.style.opacity = '1';
prompt.style.pointerEvents = 'all';
}
});
// 移动端菜单切换
const menuToggle = document.getElementById('menu-toggle');
const mobileMenu = document.getElementById('mobile-menu');
menuToggle.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
menuToggle.innerHTML = mobileMenu.classList.contains('hidden')
? ''
: '';
});
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
generateStars();
createBgPlanets();
document.addEventListener('mousemove', handleMouseMove);
// 元素动画触发
const welcomeMessage = document.getElementById('welcome-message');
const interactionPrompt = document.getElementById('interaction-prompt');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = 1;
entry.target.style.transform = 'translateY(0)';
observer.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
if (welcomeMessage) observer.observe(welcomeMessage);
if (interactionPrompt) observer.observe(interactionPrompt);
// 添加星球点击事件
document.querySelectorAll('.planet-container').forEach(planet => {
planet.addEventListener('click', function() {
const url = this.getAttribute('data-url');
if (url) {
window.location.href = url;
}
});
});
});
个人中心页面采用侧边栏和主内容区的布局方式,通过 JavaScript 实现了侧边栏的切换功能,当点击侧边栏的菜单项时,主内容区会显示相应的内容。代码示例如下:
个人中心
作品展示页面使用卡片的形式展示项目作品,通过 CSS 的hover
效果实现了图片的缩放动画。每个卡片包含项目预览图、项目名称、技术栈和详情链接,方便用户查看项目详情。代码示例如下:
作品展示
个人博客页面使用iframe
嵌入了自己在 CSDN 的博客页面,并实现了缩放控制功能。通过 JavaScript 监听窗口大小的变化,自动调整博客页面的缩放比例,以适应不同的屏幕尺寸。代码示例如下:
个人博客
Javascript部分
const blogIframe = document.getElementById('blog-iframe');
let zoomLevel = 0.8;
function applyZoom() {
blogIframe.style.transform = `scale(${zoomLevel})`;
blogIframe.style.width = `${100 / zoomLevel}%`;
blogIframe.style.height = `${100 / zoomLevel}%`;
}
// 初始缩放
applyZoom();
// 缩放按钮事件
document.getElementById('zoom-in')?.addEventListener('click', () => {
zoomLevel = Math.min(zoomLevel + 0.1, 1.5);
applyZoom();
});
document.getElementById('zoom-out')?.addEventListener('click', () => {
zoomLevel = Math.max(zoomLevel - 0.1, 0.5);
applyZoom();
});
document.getElementById('zoom-reset')?.addEventListener('click', () => {
zoomLevel = 0.8;
applyZoom();
});
// 响应式调整
function handleResize() {
if (window.innerWidth < 768) {
zoomLevel = 0.6;
} else {
zoomLevel = 0.8;
}
applyZoom();
}
// 初始调整
handleResize();
// 窗口大小改变时重新调整
window.addEventListener('resize', handleResize);
通过上述代码,我们成功运用 HTML、CSS 和 JavaScript 技术,结合 Tailwind CSS 框架,打造了一个类似宇宙的个人空间网页。这个网页不仅具有独特的视觉效果,还具备丰富的交互功能,为用户提供了良好的浏览体验。希望本文能为你提供一些灵感和参考,让你也能打造出属于自己的个人空间站。