用html做一个包含网页首页面,商品详情页等基本页面的书籍网站,并有表单验证功能、日期动态更新、图片轮播、鼠标悬停商品放大显示等功能,页面布局使用 DIV + CSS 方式实现。
时间: 2023-11-27 08:54:58 浏览: 36
很好,下面是一个示例代码,仅供参考:
HTML代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍网站</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<header>
<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>
<div id="slider">
<ul>
<li><img src="images/slider1.jpg" alt=""></li>
<li><img src="images/slider2.jpg" alt=""></li>
<li><img src="images/slider3.jpg" alt=""></li>
</ul>
</div>
<div id="content">
<div class="book_item">
<div class="book_img">
<img src="images/book1.jpg" alt="">
</div>
<div class="book_info">
<h3>图书名称</h3>
<p>作者:XXX</p>
<p>出版社:XXX</p>
<p>定价:XXX</p>
<a href="#">查看详情</a>
</div>
</div>
<div class="book_item">
<div class="book_img">
<img src="images/book2.jpg" alt="">
</div>
<div class="book_info">
<h3>图书名称</h3>
<p>作者:XXX</p>
<p>出版社:XXX</p>
<p>定价:XXX</p>
<a href="#">查看详情</a>
</div>
</div>
<!-- 其他图书信息 -->
</div>
<footer>
<p>版权所有 © 2021 书籍网站</p>
</footer>
<div id="mask"></div>
<div id="login_box">
<h3>用户登录</h3>
<form action="#" method="post" id="login_form">
<input type="text" name="username" placeholder="请输入用户名">
<input type="password" name="password" placeholder="请输入密码">
<button type="submit">登录</button>
</form>
</div>
</body>
</html>
```
CSS代码:
```css
* {
margin: 0;
padding: 0;
}
header {
height: 50px;
background-color: #333;
color: #fff;
}
nav ul {
list-style: none;
display: flex;
justify-content: flex-end;
height: 100%;
align-items: center;
}
nav li {
margin: 0 20px;
}
nav a {
color: #fff;
text-decoration: none;
}
#slider {
width: 100%;
height: 500px;
overflow: hidden;
position: relative;
}
#slider ul {
width: 300%;
height: 100%;
position: relative;
list-style: none;
left: 0;
transition: all 0.5s ease;
}
#slider ul li {
float: left;
width: 33.3333%;
}
#slider img {
width: 100%;
height: 100%;
object-fit: cover;
}
#content {
margin: 50px auto;
max-width: 1200px;
display: flex;
flex-wrap: wrap;
}
.book_item {
width: 25%;
margin: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
.book_img {
height: 200px;
overflow: hidden;
position: relative;
}
.book_img img {
width: 100%;
height: 100%;
object-fit: cover;
transition: all 0.5s ease;
}
.book_info {
padding: 10px;
}
.book_info h3 {
font-size: 18px;
margin-bottom: 10px;
}
.book_info p {
margin-bottom: 5px;
}
.book_info a {
display: inline-block;
margin-top: 10px;
padding: 5px 10px;
background-color: #333;
color: #fff;
border-radius: 5px;
text-decoration: none;
}
footer {
height: 50px;
background-color: #333;
color: #fff;
text-align: center;
line-height: 50px;
margin-top: 50px;
}
#mask {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
position: fixed;
top: 0;
left: 0;
display: none;
}
#login_box {
width: 400px;
height: 300px;
background-color: #fff;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
}
#login_box h3 {
text-align: center;
margin-top: 20px;
}
#login_form {
margin-top: 50px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#login_form input {
width: 80%;
padding: 10px;
margin-bottom: 20px;
border: none;
border-bottom: 2px solid #333;
}
#login_form button {
width: 100px;
padding: 10px;
background-color: #333;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.5s ease;
}
#login_form button:hover {
background-color: #fff;
color: #333;
}
```
JS代码:
```js
$(function () {
// 图片轮播
var current = 0;
var slider = $('#slider ul');
var sliderWidth = $('#slider').width();
var slideCount = slider.find('li').length;
var slideWidth = sliderWidth / slideCount;
slider.find('li').each(function () {
$(this).css({
width: slideWidth
});
});
slider.width(sliderWidth * slideCount);
setInterval(function () {
current++;
if (current >= slideCount) {
current = 0;
}
slider.animate({
left: -current * sliderWidth
}, 500);
}, 3000);
// 鼠标悬停放大图片
$('.book_img').hover(function () {
$(this).find('img').css({
transform: 'scale(1.2)'
});
}, function () {
$(this).find('img').css({
transform: 'scale(1)'
});
});
// 弹出登录框
$('.book_info a').click(function () {
$('#mask, #login_box').fadeIn();
});
// 关闭登录框
$('#mask, #login_box button').click(function () {
$('#mask, #login_box').fadeOut();
});
// 表单验证
$('#login_form').submit(function () {
var username = $('input[name="username"]').val();
var password = $('input[name="password"]').val();
if (username === '') {
alert('请输入用户名');
return false;
}
if (password === '') {
alert('请输入密码');
return false;
}
return true;
});
// 日期动态更新
setInterval(function () {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
var time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
$('#time').text(time);
}, 1000);
});
```