码迷,mamicode.com
首页 > Web开发 > 详细

js原生轮播图

时间:2016-10-15 09:24:48      阅读:943      评论:0      收藏:0      [点我收藏+]

标签:


轮播图是新手学前端的必经之路!
直接上代码!


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
.all{
margin: 100px auto;
width: 520px;
height: 280px;
position: relative;
cursor: pointer;
overflow: hidden;
}
/*图片*/
.all .pic{
width: 3120px;
height: 280px;
position: absolute;
top: 0;
left: 0;
}
/*左右点击*/
.all .slip{
width: 520px;
height: 24px;
position: absolute;
top: 50%;
left: 0;
margin-top: -12px;
font-size: 36px;
}
.all .slip .left{
float: left;
width: 21px;
height: 36px;
padding-left: 3px;
background: rgba(0, 0, 0, 0.5);
}
.all .slip .right{
float: right;
width: 21px;
height: 36px;
padding-left: 3px;
background: rgba(0, 0, 0, 0.5);
}
.all .slip span{
color: white;
/*width: 10px;*/
line-height: 36px;
display: inline-block;
margin: 0 auto;
}
/*小圆点*/
.all .circle ul{
width: 65px;
height: 13px;
overflow: hidden;
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -32px;
list-style: none;
background: rgba(255, 255, 255, 0.3);
border-radius: 7px;
cursor: hand;
}
.all .circle ul li{
width: 9px;
height: 9px;
background-color: gainsboro;
border-radius: 50%;
float: left;
margin: 2px;
}
</style>
</head>
<body>
<div class="all">
<div class="pic" id="pic">
    <!--这里是要放五张图片不过要把第一张连接到最后一张 这样能显示较好的轮播效果-->

      <img src="images/1.jpg" /><img src="images/2.jpg" /><img src="images/3.jpg" /><img src="images/4.jpg" /><img src="images/5.jpg" /><img src="images/1.jpg" />
    </div>
<div class="slip">
<div class="left" id="slip_left"><span><</span></div>
<div class="right" id="slip_right"><span>></span></div>
</div>
<div class="circle">
<ul id="circle_ul">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
<script>
//获取dom元素
var pic = document.getElementById("pic");
var slipLeft = document.getElementById("slip_left");
var slipRight = document.getElementById("slip_right");
var circileUl = document.getElementById("circle_ul");
var lis = circileUl.getElementsByTagName("li");
//命名变量 leader为盒子所在位置 target为盒子目标位置(在点击左右按钮及小圆点确定位置时用到)
var leader = 0, target = 0;
var time1 = null,time2 = null,time3 = null;

//time1 定时器是在没有任何操作下 让图片开始轮着播放出来
time1 = setInterval(autoplay,1);
// 轮播函数
function autoplay() {
leader--;
//当位置超过之后重新回到第一张
leader < -2599? leader = 0 : leader;
pic.style.left = leader + "px";
//当整张图片显示出来的时候停留两秒
if(leader%520 == 0){
target = leader;
clearInterval(time1);
//设置定时器在两秒之后重新开启轮播的定时器time1
time2 = setTimeout(function () {
time1 = setInterval(autoplay,1);
},2000);
}
//将小圆点的颜色全部变为灰色
for(var i = 0; i < lis.length; i++){
lis[i].style.backgroundColor = "gainsboro";
}
//选中当前选中的图片对应的小圆点将他的颜色变为橘黄色
if(-target/520 == 5){
lis[0].style.backgroundColor = "#f40";
}else{
lis[(-target/520)].style.backgroundColor = "#f40";
}
}
//move为在点击之后跳转到目标目标图片的函数
function move(){
leader = leader + (target - leader)/10;
pic.style.left = leader + "px";
if(Math.abs(leader)%520 < 0.1 || (Math.abs(leader)%520 - 520) > -0.1){
leader = target;
clearInterval(time3);
time2 = setTimeout(function () {
time1 = setInterval(autoplay,1);
},2000);
}
for(var i = 0; i < lis.length; i++){
lis[i].style.backgroundColor = "gainsboro";
}
if(-target/520 == 5){
lis[0].style.backgroundColor = "#f40";
}else{
lis[(-target/520)].style.backgroundColor = "#f40";
}
}


//左滑点击事件
slipLeft.onclick = function () {
//点击之后先清除定时器
clearInterval(time1);
clearTimeout(time2);
clearTimeout(time3);
//得到目标值
if(leader%520 == 0){
target = target + 520;
}
target > 0? target = -2080 : target;
time3 = setInterval(move,10);
};

//右滑点击事件
slipRight.onclick = function () {
clearInterval(time1);
clearTimeout(time2);
clearTimeout(time3);
if(leader%520 == 0){
target = target - 520;
target < -1560? target = 0 : target;
}else{
target < -1560? target = 0 : target -=520;
}
time3 = setInterval(move,10);
};

//设置点击圆点触发的事件
for(var i = 0; i < lis.length; i++){
//得到下标
lis[i].index = i;
lis[i].onclick = function () {
clearInterval(time1);
clearTimeout(time2);
clearTimeout(time3);
//得到目标位置值
target = - this.index*520;
time3 = setInterval(move,10);
//将选中的的小圆点变色
this.style.backgroundColor = "#f40";
}
}
</script>
</body>
</html>

js原生轮播图

标签:

原文地址:http://www.cnblogs.com/zhangxyjack/p/5962737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!