利用HTML5 + CSS3 + JavaScript写一个个人网页代码
时间: 2023-08-10 07:05:42 浏览: 119
以下是一个简单的HTML5 + CSS3 + JavaScript个人网页的示例代码,可以作为参考:
HTML代码:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>个人网页</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>个人网页</h1>
<nav>
<ul>
<li><a href="#about">关于我</a></li>
<li><a href="#portfolio">作品集</a></li>
<li><a href="#contact">联系我</a></li>
</ul>
</nav>
</header>
<main>
<section id="about">
<h2>关于我</h2>
<p>我是一名前端开发工程师,热爱技术和设计。</p>
</section>
<section id="portfolio">
<h2>作品集</h2>
<ul>
<li>
<img src="portfolio-1.jpg" alt="作品1">
<h3>作品1</h3>
<p>作品1的介绍</p>
</li>
<li>
<img src="portfolio-2.jpg" alt="作品2">
<h3>作品2</h3>
<p>作品2的介绍</p>
</li>
<li>
<img src="portfolio-3.jpg" alt="作品3">
<h3>作品3</h3>
<p>作品3的介绍</p>
</li>
</ul>
</section>
<section id="contact">
<h2>联系我</h2>
<form>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
<label for="message">留言:</label>
<textarea id="message" name="message"></textarea>
<button type="submit">发送</button>
</form>
</section>
</main>
<footer>
<p>©2021 个人网页</p>
</footer>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
display: flex;
justify-content: space-between;
padding: 20px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #fff;
display: block;
padding: 10px;
text-decoration: none;
}
main {
padding: 20px;
}
section {
margin-bottom: 40px;
}
section h2 {
font-size: 36px;
margin-bottom: 20px;
}
section ul {
list-style: none;
margin: 0;
padding: 0;
}
section ul li {
margin-bottom: 20px;
}
section ul li img {
max-width: 100%;
}
section ul li h3 {
font-size: 24px;
margin-bottom: 10px;
}
section ul li p {
font-size: 16px;
}
form label {
display: block;
font-size: 18px;
margin-bottom: 10px;
}
form input[type="text"],
form input[type="email"],
form textarea {
border: 1px solid #ccc;
font-size: 16px;
padding: 10px;
width: 100%;
}
form button[type="submit"] {
background-color: #333;
border: none;
color: #fff;
cursor: pointer;
font-size: 18px;
margin-top: 10px;
padding: 10px;
}
footer {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
```
JavaScript代码:
```
// 为导航栏添加滚动效果
const navLinks = document.querySelectorAll('nav ul li a');
for (const link of navLinks) {
link.addEventListener('click', e => {
e.preventDefault();
const section = document.querySelector(link.getAttribute('href'));
section.scrollIntoView({behavior: 'smooth'});
});
}
// 验证表单并发送邮件
const form = document.querySelector('form');
form.addEventListener('submit', e => {
e.preventDefault();
const name = document.querySelector('#name').value.trim();
const email = document.querySelector('#email').value.trim();
const message = document.querySelector('#message').value.trim();
if (name === '' || email === '' || message === '') {
alert('请输入姓名、邮箱和留言内容');
return;
}
// 发送邮件的代码
});
```
以上代码仅作为示例,实际网页开发需要根据具体需求和目标进行调整和完善。
阅读全文