Html的代码:

<div class="carousel_img">
<div class="car_img" style="background:url(files/DocMatchImg_1.png) no-repeat;background-size:cover;background-position:center;">
</div>
<div class="car_img" style="background:url(files/DocMatchImg_2.png) no-repeat;background-size:cover;background-position:center;">
</div>
<div class="car_img" style="background:url(files/DocMatchImg_1.png) no-repeat;background-size:cover;background-position:center;">
</div>
<div class="car_img" style="background:url(files/DocMatchImg_2.png) no-repeat;background-size:cover;background-position:center;">
</div>
<div class="car_img" style="background:url(files/DocMatchImg_2.png) no-repeat;background-size:cover;background-position:center;">
</div> <div class="carousel_index">
<div class="carousel_icon carousel_icon1"></div>
<div class="carousel_icon carousel_icon2"></div>
<div class="carousel_icon carousel_icon2"></div>
<div class="carousel_icon carousel_icon2"></div>
<div class="carousel_icon carousel_icon2"></div>
</div>
</div>

css代码:

.carousel_img{width:100%;position:relative;top:0;left:0;overflow:hidden;height:200px;}
.car_img{width:100%;height:200px;position:absolute;top:0;left:0;}
.carousel_index{position:absolute;top:180px;right:0;padding-right:24px;height:12px;}
.carousel_icon{width:12px;height:12px;float:left;}
.carousel_icon1{background:url(../image/DovmatchWhite.png) no-repeat;background-size:8px;background-position:center center;}
.carousel_icon2{background:url(../image/DovmatchGrey.png) no-repeat;background-size:8px;background-position:center center;}

jq代码:

$(document).ready(function(e) {
var imgObj = document.getElementsByClassName("car_img");
var imgLen = imgObj.length;
var windowWidth = $(window).width();
$(".car_img").bind("click",function(event){ });
int = setInterval(carouselImg,3000);
for(var i=0;i<imgLen;i++){
$(".car_img").eq(i).css({"top":"0","left":i*windowWidth});
imgObj[i].addEventListener('touchstart',touchstart,false);
imgObj[i].addEventListener('touchmove',touchmove,false);
imgObj[i].addEventListener('touchend',touchend,false);
} }); function touchstart(event){
event.preventDefault();
if( event.targetTouches.length == 1 )
{
clearInterval(int);
var touch = event.targetTouches[0];
pressX = touch.pageX;
}
} /*
*定义每次滑动的距离spanX
*定义当前滑动的索引位置thisIndex,轮播图的个数imgLen
*/
function touchmove(event){
event.preventDefault(); if( event.targetTouches.length == 1 )
{
var touch = event.targetTouches[0];
var spanX = touch.pageX - pressX ,
windowWidth = $(window).width();
var $car_img = $(".car_img"),
$this = $(this);
var thisIndex = $this.index(),
imgLen = $(".car_img").length;
for(var i=0;i < imgLen;i++){
$car_img.eq(i).css("left",windowWidth*(i-thisIndex)+spanX);
} }
} function touchend(event){
var $car_img = $(".car_img"),
$this = $(this),
$carousel_icon = $(".carousel_icon"),
windowWidth = $(window).width();
var thisIndex = $this.index(),
imgLen = $(".car_img").length;
var thisLeft = parseInt($(this).css("left"));
//向左滑动执行的方法
if(thisLeft < -32 && thisIndex < imgLen){
//当轮播图滑动最后一个位置时,当前轮播图位置不变
if(thisIndex == imgLen-1){
for(var i=0;i < imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-thisIndex)},300); }
}
//当轮播不在最后一个位置时,轮播图位置变化方法
else{
for(var i=0;i < imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-(thisIndex+1))},300);
$carousel_icon.eq(i).addClass("carousel_icon2").removeClass("carousel_icon1");
}
$carousel_icon.eq(thisIndex+1).removeClass("carousel_icon2").addClass("carousel_icon1");
} }
//向右滑动执行的方法
else if(thisLeft > 32 && thisIndex >= 0){
//当轮播图在第一个位置时
if( thisIndex == 0){
for(var i=0;i < imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-thisIndex)},300);
}
}
//轮播图不在第一个位置
else{
for(var i=0;i < imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-(thisIndex-1))},300);
$carousel_icon.eq(i).addClass("carousel_icon2").removeClass("carousel_icon1");
}
$carousel_icon.eq(thisIndex-1).removeClass("carousel_icon2").addClass("carousel_icon1");
}
}
//当滑动距离在大于-32px并且小于32px时,当前轮播图位置不变
else{
for(var i=0;i < imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-thisIndex)},100);
}
}
int = setInterval(carouselImg,3000);
} function carouselImg(){
var $car_img = $(".car_img"),
$carousel_icon = $(".carousel_icon"),
windowWidth = $(window).width();
var imgLen = $car_img.length,
imgZeroIndex = 0;
for(var i=0;i<imgLen;i++){
var everyImgLeft = parseInt($car_img.eq(i).css("left"));
if(everyImgLeft == 0){
imgZeroIndex = i;
break;
} }
if(imgZeroIndex == imgLen-1){
for(var i=0;i<imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*i},300);
$carousel_icon.eq(i).removeClass("carousel_icon1").addClass("carousel_icon2");
}
$carousel_icon.eq(0).removeClass("carousel_icon2").addClass("carousel_icon1");
}
else{
for(var i=0;i<imgLen;i++){
$car_img.eq(i).animate({"left":windowWidth*(i-(imgZeroIndex+1))},300);
$carousel_icon.eq(i).removeClass("carousel_icon1").addClass("carousel_icon2");
}
$carousel_icon.eq(imgZeroIndex+1).removeClass("carousel_icon2").addClass("carousel_icon1");
}
}

代码有缺陷,其中touchstart函数中点击开始的X坐标pressX不用全局变量该怎么表示?还有int这样的一个全局变量,没有解决好,有大神可以指正下。

展示效果图

最新文章

  1. java第四次作业
  2. asp.net MVC 过滤器使用案例:统一处理异常顺道精简代码
  3. 洛谷P1209 [USACO1.3]修理牛棚 Barn Repair
  4. Iphone [Tab Bar实现多view切换,Picker,DataPicter实现
  5. C#数据结构
  6. PHP类与面向对象(二)
  7. winform 承载 WCF 注意,可能不是工作在多线程模式下
  8. DBHelper 类(网上收集)
  9. C#实现多态之一抽象
  10. java简单数据类型转化
  11. ●BZOJ 1499 [NOI2005]瑰丽华尔兹
  12. JSP知识点总结
  13. Linux 工作目录切换命令
  14. 180425、cookie工具类
  15. 2017年蓝桥杯省赛A组c++第1题(走迷宫)
  16. 数位DP学习笔记
  17. LeetCode: Longest Common Prefix 解题报告
  18. R语言ggplot2-labs 函数
  19. create-react-app项目中的eslint
  20. ZOJ 17届校赛 How Many Nines

热门文章

  1. 使用 Istio CNI 支持强安全 TKE Stack 集群的服务网格流量捕获
  2. Vue3中setup语法糖学习
  3. TP5框架中实现多条件登录
  4. Linux开机自启应用&amp;开机执行脚本&amp;监听端口应用挂掉了执行启动脚本
  5. m3u8文件合并处理
  6. Spring IOC---Bug处理
  7. docker是干什么的,docker常用命令
  8. 时间模块 time 随机模块random os模块,sys模块
  9. 冒泡排序和鸡尾酒排序(code)
  10. 开源电调blheli / blheli_s分析