用js编写一个宠物网站
时间: 2024-05-16 10:16:26 浏览: 8
好的,下面是一个简单的宠物网站的示例代码:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<title>宠物网站</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>宠物网站</h1>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">宠物商店</a></li>
<li><a href="#">宠物医院</a></li>
<li><a href="#">关于我们</a></li>
</ul>
</nav>
</header>
<main>
<section id="banner">
<h2>欢迎来到宠物网站</h2>
<p>这里有最可爱的宠物等你来领养!</p>
</section>
<section id="pets">
<h2>我们的宠物</h2>
<ul>
<li>
<img src="images/cat.jpg" alt="猫">
<h3>猫</h3>
<p>价格:100元</p>
<button>领养</button>
</li>
<li>
<img src="images/dog.jpg" alt="狗">
<h3>狗</h3>
<p>价格:200元</p>
<button>领养</button>
</li>
<li>
<img src="images/rabbit.jpg" alt="兔子">
<h3>兔子</h3>
<p>价格:50元</p>
<button>领养</button>
</li>
</ul>
</section>
</main>
<footer>
<p>版权所有 © 宠物网站</p>
</footer>
</body>
</html>
```
CSS部分:
```css
/* reset styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* global styles */
body {
font-family: Arial, sans-serif;
}
header {
background-color: #6699cc;
color: #fff;
padding: 20px;
}
nav ul {
list-style: none;
display: flex;
}
nav li {
margin-right: 20px;
}
nav a {
color: #fff;
text-decoration: none;
}
main {
padding: 20px;
}
#banner {
background-image: url('images/banner.jpg');
background-size: cover;
background-position: center;
text-align: center;
color: #fff;
padding: 100px;
}
#pets {
margin-top: 50px;
}
#pets ul {
list-style: none;
display: flex;
flex-wrap: wrap;
}
#pets li {
width: calc(33.33% - 20px);
margin-right: 20px;
margin-bottom: 20px;
background-color: #eee;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
}
#pets li img {
max-width: 100%;
}
#pets li h3 {
font-size: 20px;
margin-top: 10px;
margin-bottom: 5px;
}
#pets li p {
font-size: 16px;
margin-bottom: 10px;
}
#pets li button {
background-color: #6699cc;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
#pets li button:hover {
background-color: #336699;
}
```
JavaScript部分(用于按钮点击事件):
```javascript
const buttons = document.querySelectorAll('button');
buttons.forEach(button => {
button.addEventListener('click', () => {
alert('您已领养成功!');
});
});
```
以上代码可以实现一个简单的宠物网站,你可以根据自己的需求和喜好进行修改和扩展。