<div class="jd_layout">
<div class="jd_banner">
<ul class="jd_bannerImg clearfix">
<li>
<a href="javascript:;">
<img src="images/l1.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l2.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l3.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l4.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l5.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l6.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l7.jpg" alt="">
a>
li>
<li>
<a href="javascript:;">
<img src="images/l8.jpg" alt="">
a>
li>
ul>
<ul class="jd_bannerIndicator">
<li class="active">li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
ul>
div>
div>
imgBox.appendChild(first.cloneNode(true));
imgBox.insertBefore(last.cloneNode(true),imgBox.firstChild);
bannerWidth
bannerWidth
imgBox
= bannerWidth
* li的个数 + ‘px’ var lis=imgBox.querySelectorAll("li");
var count=lis.length;
var bannerWidth=banner.offsetWidth;
imgBox.style.width=count*bannerWidth+"px";
for(var i=0;i<lis.length;i++){
lis[i].style.width=bannerWidth+"px";
}
imgBox.style.left=-bannerWidth+"px";
window.onresize
监听bannerWidth
不需要再声明var
/*4.当屏幕变化的时候,重新计算宽度*/
window.onresize=function(){
bannerWidth=banner.offsetWidth;
imgBox.style.width=count*bannerWidth+"px";
for(var i=0;i<lis.length;i++){
lis[i].style.width=bannerWidth+"px";
}
imgBox.style.left=-index*bannerWidth+"px";
}
active
样式active
样式 /*5.实现点标记*/
var index = 1
var setIndicator=function(index){
var indicators=banner.querySelector("ul:last-of-type").querySelectorAll("li");
for(var i=0;i<indicators.length;i++){
indicators[i].classList.remove("active");
}
indicators[index-1].classList.add("active");
};
index++
(-index*bannerWidth)+"px"
/*6.实现自动轮播*/
var startTime=function(){
timerId=setInterval(function(){
index++;
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=(-index*bannerWidth)+"px";
setTimeout(function(){
if(index==count-1){
//轮播到最后一张
index=1;
imgBox.style.transition="none";
imgBox.style.left=(-index*bannerWidth)+"px";
}
},500);
},1000);
}
startTime();
imgBox.addEventListener("touchstart",function(e){
clearInterval(timerId);
startX= e.targetTouches[0].clientX;
});
distanceX
distanceX
imgBox.addEventListener("touchmove",function(e){
if(isEnd==true){
console.log("touchmove");
moveX= e.targetTouches[0].clientX;
//计算距离
distanceX=moveX-startX;
//清除过渡
imgBox.style.transition="none";
//设置偏移
imgBox.style.left=(-index*bannerWidth + distanceX)+"px";
}
});
若移动距离小于给定的值则状态不变
否则判断左移还是右移,修改相应的index
并设置相应的偏移
起始位置和移动距离归零
imgBox.addEventListener("touchend",function(e){
isEnd=false;
if(Math.abs(distanceX) > 100){
if(distanceX > 0){//上一张
index--;
}
else{ //下一张
index++;
}
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=-index*bannerWidth+"px";
}
else if(Math.abs(distanceX) > 0){
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=-index*bannerWidth+"px";
}
//初始化
startX=0;
moveX=0;
distanceX=0;
});
imgBox.addEventListener("webkitTransitionEnd",function(){
//最后一张时
if(index==count-1){
index=1;
imgBox.style.transition="none";
imgBox.style.left=-index*bannerWidth+"px";
}
else if(index==0){
//第一张时
index=count-2;
imgBox.style.transition="none";
imgBox.style.left=-index*bannerWidth+"px";
}
setIndicator(index);
isEnd=true;
});
window.onload=function(){
bannerEffect();
}
function bannerEffect(){
/*1.设置修改轮播图的页面结构*/
var banner=document.querySelector(".jd_banner");
var imgBox=banner.querySelector("ul:first-of-type");
var first=imgBox.querySelector("li:first-of-type");
var last=imgBox.querySelector("li:last-of-type");
imgBox.appendChild(first.cloneNode(true));
imgBox.insertBefore(last.cloneNode(true),imgBox.firstChild);
/*2.设置对应的样式*/
var lis=imgBox.querySelectorAll("li");
var count=lis.length;
var bannerWidth=banner.offsetWidth;
imgBox.style.width=count*bannerWidth+"px";
for(var i=0;i<lis.length;i++){
lis[i].style.width=bannerWidth+"px";
}
var index=1;
/*3.设置默认的偏移*/
imgBox.style.left=-bannerWidth+"px";
/*4.当屏幕变化的时候,重新计算宽度*/
window.onresize=function(){
bannerWidth=banner.offsetWidth;
imgBox.style.width=count*bannerWidth+"px";
for(var i=0;i<lis.length;i++){
lis[i].style.width=bannerWidth+"px";
}
imgBox.style.left=-index*bannerWidth+"px";
}
/*5.实现点标记*/
var setIndicator=function(index){
var indicators=banner.querySelector("ul:last-of-type").querySelectorAll("li");
for(var i=0;i<indicators.length;i++){
indicators[i].classList.remove("active");
}
indicators[index-1].classList.add("active");
};
var timerId;
/*6.实现自动轮播*/
var startTime=function(){
timerId=setInterval(function(){
index++;
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=(-index*bannerWidth)+"px";
setTimeout(function(){
if(index==count-1){
index=1;
imgBox.style.transition="none";
imgBox.style.left=(-index*bannerWidth)+"px";
}
},500);
},1000);
}
startTime();
/*7.实现手动轮播*/
var startX,moveX,distanceX;
var isEnd=true;
imgBox.addEventListener("touchstart",function(e){
clearInterval(timerId);
startX= e.targetTouches[0].clientX;
});
imgBox.addEventListener("touchmove",function(e){
if(isEnd==true){
console.log("touchmove");
moveX= e.targetTouches[0].clientX;
distanceX=moveX-startX;
imgBox.style.transition="none";
imgBox.style.left=(-index*bannerWidth + distanceX)+"px";
}
});
imgBox.addEventListener("touchend",function(e){
isEnd=false;
if(Math.abs(distanceX) > 100){
if(distanceX > 0){//上一张
index--;
}
else{ //下一张
index++;
}
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=-index*bannerWidth+"px";
}
else if(Math.abs(distanceX) > 0){
imgBox.style.transition="left 0.5s ease-in-out";
imgBox.style.left=-index*bannerWidth+"px";
}
startX=0;
moveX=0;
distanceX=0;
});
imgBox.addEventListener("webkitTransitionEnd",function(){
if(index==count-1){
index=1;
imgBox.style.transition="none";
imgBox.style.left=-index*bannerWidth+"px";
}
else if(index==0){
index=count-2;
imgBox.style.transition="none";
imgBox.style.left=-index*bannerWidth+"px";
}
setIndicator(index);
isEnd=true;
});
}
$(function(){
// 获取轮播图元素
var banner = $('.jd_banner');
var bannerWidth = banner.width();
//获取图片盒子
var imgBox = banner.find('ul:first-of-type');
//获取点标记
var indicators = banner.find("ul:eq(1)").find("li");
//获取首尾两张图片
var first = imgBox.find('li:first-of-type');
var last = imgBox.find('li:last-of-type');
//将两张图片添加到首层位置 first.clone():将first拷贝一份
imgBox.append(first.clone());
last.clone().insertBefore(first);
//设置图片盒子的宽度
var lis = imgBox.find('li');
var count = lis.length;
imgBox.width(count * bannerWidth);
//设置li标签的宽度
lis.each(function(index,value){
$(lis[index]).width(bannerWidth)
});
//设置默认偏移
imgBox.css('left',-bannerWidth);
//定义图片索引
var index = 1;
//开启定时器
setInterval(function(){
index++;
imgBox.animate({"left":-index * bannerWidth},200,'ease-in-out',function(){
if(index == count - 1){//最后一张
index = 1;
//让它瞬间偏移到索引1的位置 -- 非过渡
imgBox.css('left',-index * bannerWidth);
}else if(index == 0){
index = count - 2;
imgBox.css('left',-index * bannerWidth);
}
//设置点标记
indicators.removeClass('active').eq(index-1).addClass('active');
})
},2000)
//添加滑动事件
+ imgBox.on('swipeLeft',function(){
+ console.log('swipeLeft');
+ })
+ imgBox.on('swipeRight',function(){
+ console.log('swipeRight');
+ })
+ })