syq816

1、创建一个html页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script src="jquery-3.2.1.js"></script>
<script>
$(function(){
var len=$("#div1 .img li").length;
var i=0;
//设置自动播放
//通过ul左移来实现
var t=setInterval(function(){
if(++i==len){
i=0;
}
$(".img").animate({left:-i*790},1000);
//设置原点变化
$(".controls li").eq(i).addClass("on").siblings().removeClass("on");
},1000);

//设置鼠标移入停止动画
$("#div1").hover(function(){
clearInterval(t);
},function(){
t=setInterval(function(){
if(++i==len){
i=0;
}
$(".img").animate({left:-i*790},1000);
$("#div1 .controls li").eq(i).addClass("on").siblings().removeClass("on");
},3000);
});

//设置鼠标点击左右键的事件
$(".left").click(function(){
var left=$(".img").css("left");
i=i-1;
//Number newleft=Nuber(left-790);
//$(".img").animate({left:newleft},500);
$(".img").animate({left:-i*790},1000);
$("#div1 .controls li").eq(i).addClass("on").siblings().removeClass("on");
});

$(".right").click(function(){
var left=$(".img").css("left");
i=i+1;
//Number newleft=Nuber(left-790);
//$(".img").animate({left:newleft},500);
$(".img").animate({left:-i*790},1000);
$("#div1 .controls li").eq(i).addClass("on").siblings().removeClass("on");
});

//设置原点的点击事件
$(".controls li").click(function(){
i=$(this).index();
$(".img").animate({left:-i*790},1000);
$("#div1 .controls li").eq(i).addClass("on").siblings().removeClass("on");
});
})


</script>
<style>
*{
margin:0;
padding:0;
}
#div1{
width: 790px;
height: 340px;
position: absolute;
overflow: hidden;
left: 230px;
top: 100px;
}
#div1 .img{
list-style: none;
height: 340px;
width:3500px;
position: absolute;
}
#div1 .img li{
list-style: none;
float: left;
}
#div1 .controls{
height: 30px;
width: 200px;
position: absolute;
left: 300px;
top: 320px;
}
/*将li变更成一个一个的圆点*/
.controls li{
height: 10px;
width: 10px;
list-style: none;
float: left;
border: 1px solid white;
background-color: white;
border-radius: 100%;
margin-left: 30px;
opacity: 60%;
}
/*图片显示第几张,li的background变为红色*/
.controls .on{
background-color: red;
}
/*左边键头*/
#div1 .left{
width: 20px;
height: 20px;
border-top: 5px solid white;
border-left: 5px solid white;
position: absolute;
left: 20px;
top:150px;
-webkit-transform:rotate(-45deg);
opacity: 30%;
}
/*右边边键头*/
#div1 .right{
width: 20px;
height: 20px;
border-top: 5px solid white;
border-right: 5px solid white;
position: absolute;
left: 740px;
top:150px;
-webkit-transform:rotate(45deg);
opacity: 30%;
}


</style>
<body>
<div id="div1">
<ul class="img">
<li><img src="图片路径" alt="" class="switch"></li>
<li><img src="图片路径" alt="" class="switch"></li>
<li><img src="图片路径" alt="" class="switch"></li>
<li><img src="图片路径" alt="" class="switch"></li>
</ul>
<!-- 左右键 -->
<span class="left"></span>
<span class="right"></span>
<!-- 圆点 -->
<ul class="controls">
<li class="on"></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>

 

效果图:

 

 

分类:

技术点:

相关文章: