1.css代码
* {
margin: 0;
padding: 0;
list-style: none;
}
.slider {
height: 340px;
width: 790px;
position: relative;
overflow: hidden;
}
.slider ul>li {
float: left;
overflow: hidden;
}
.slider ul>li:first-child {
display: block;
}
.arrow {
display: none;
}
.slider:hover .arrow {
display: block;
}
.arrow-left,
.arrow-right {
width: 30px;
height: 60px;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
text-align: center;
line-height: 60px;
color: #fff;
font-weight: 700;
font-size: 30px;
}
.arrow-left:hover,
.arrow-right:hover {
background-color: rgba(0, 0, 0, .5);
}
.arrow-left {
left: 0;
}
.arrow-right {
right: 0;
}
ol{
position: absolute;
left: 50%;
margin-left: -160px;
bottom: 20px;
line-height: 20px;
text-align: center;
}
ol li{
float: left;
width: 30px;
height: 30px;
border-radius: 5px;
background: #fff;
border: 1px solid #ccc;
margin-left: 10px;
cursor: pointer;
font-size: 14px;
text-align: center;
line-height: 30px;
}
.red{
background: #4fcaff;
color: #fff;
}
#myUl{
width: 7110px;
}
2.html代码
<div class="slider">
<ul id="myUl">
<li><a href="#"><img src="images/01.jpg" alt=""></a></li>
<li><a href="#"><img src="images/02.jpg" alt=""></a></li>
<li><a href="#"><img src="images/03.jpg" alt=""></a></li>
<li><a href="#"><img src="images/04.jpg" alt=""></a></li>
<li><a href="#"><img src="images/05.jpg" alt=""></a></li>
<li><a href="#"><img src="images/06.jpg" alt=""></a></li>
<li><a href="#"><img src="images/07.jpg" alt=""></a></li>
<li><a href="#"><img src="images/08.jpg" alt=""></a></li>
<li><a href="#"><img src="images/01.jpg" alt=""></a></li>
</ul>
<ol>
<li class="red">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ol>
<!--箭头-->
<div class="arrow">
<span class="arrow-left"><</span>
<span class="arrow-right">></span>
</div>
3.jquery代码
var index = 0;
var time = setInterval(move, 1500);
var width = $(".slider").width();
function move() {
index++;
$("#myUl").animate({marginLeft:-index*width },500);
if( index == 8){
//瞬间换成第一张图片
index = 0;
$("#myUl").animate({marginLeft:-index*width },0);
}
//设置小圆点
$("ol li").eq(index).addClass("red").siblings().removeClass("red");
}
//移入移出
$(".slider").mouseenter(function () {
clearInterval(time);
})
$(".slider").mouseleave(function () {
time = setInterval(move, 1500);
})
//点击左右按钮
$(".arrow-left").click(function () {
if(index == 0){
index = 8;
$("#myUl").animate({marginLeft: -index*width},0);
}
index--;
$("#myUl").animate({marginLeft:-index*width },500);
//设置小圆点
$("ol li").eq(index).addClass("red").siblings().removeClass("red");
})
$(".arrow-right").click(move);
//点击小圆点滑动到对应的图片
$("ol li").mouseenter(function () {
index = $(this).index();
$("#myUl").animate({marginLeft:-index*width },500);
//设置小圆点
$("ol li").eq(index).addClass("red").siblings().removeClass("red");
})
效果图
在使用jquery之前一定要引用jquery的相关js代码