写一个有导航栏有轮播的主页框架,jQuery代码
时间: 2024-03-08 21:46:42 浏览: 59
基于Jquery的轮播图
以下是一个简单的有导航栏和轮播图的主页框架,使用了jQuery技术:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>导航栏和轮播图主页框架</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="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>
<section id="slider">
<div class="slide active-slide">
<img src="slide1.jpg">
<div class="slide-caption">
<h2>第一张图片标题</h2>
<p>第一张图片描述内容</p>
</div>
</div>
<div class="slide">
<img src="slide2.jpg">
<div class="slide-caption">
<h2>第二张图片标题</h2>
<p>第二张图片描述内容</p>
</div>
</div>
<div class="slide">
<img src="slide3.jpg">
<div class="slide-caption">
<h2>第三张图片标题</h2>
<p>第三张图片描述内容</p>
</div>
</div>
</section>
</body>
</html>
```
CSS代码:
```
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav li {
display: inline-block;
margin-right: 20px;
}
nav a {
color: #fff;
text-decoration: none;
font-size: 18px;
}
#slider {
position: relative;
overflow: hidden;
}
#slider .slide {
position: absolute;
top: 0;
left: 0;
height: 500px;
width: 100%;
display: none;
}
#slider .active-slide {
display: block;
}
#slider .slide img {
height: 500px;
width: 100%;
object-fit: cover;
}
.slide-caption {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.5);
padding: 20px;
border-radius: 5px;
color: #fff;
}
.slide-caption h2 {
margin-top: 0;
font-size: 36px;
}
.slide-caption p {
margin-bottom: 0;
font-size: 18px;
}
```
jQuery代码:
```
$(document).ready(function() {
var slideCount = $('#slider .slide').length;
var slideWidth = $('#slider .slide').width();
var slideHeight = $('#slider .slide').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider .slide:last-child').prependTo('#slider ul');
function moveLeft() {
$('#slider ul').animate({
left: +slideWidth
}, 200, function() {
$('#slider .slide:last-child').prependTo('#slider ul');
$('#slider ul').css('left', '');
});
};
function moveRight() {
$('#slider ul').animate({
left: -slideWidth
}, 200, function() {
$('#slider .slide:first-child').appendTo('#slider ul');
$('#slider ul').css('left', '');
});
};
$('a.control_prev').click(function() {
moveLeft();
});
$('a.control_next').click(function() {
moveRight();
});
});
```
这段代码中,我们使用jQuery来实现轮播功能,其中`slideCount`表示轮播图的数量,`slideWidth`和`slideHeight`分别表示轮播图的宽度和高度,`sliderUlWidth`表示轮播图容器的总宽度。`moveLeft()`和`moveRight()`是移动轮播图的函数,分别用于左移和右移。在HTML中,我们为左右箭头添加了`control_prev`和`control_next`类,并使用jQuery监听它们的点击事件,来调用对应的移动函数。
阅读全文