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这样的一个全局变量,没有解决好,有大神可以指正下。

展示效果图

用jq实现移动端滑动轮播以及定时轮播效果的更多相关文章

  1. 前端(十七)—— jQuery基础:jQuery的基本使用、JQ功能概括、JS对象与JQ对象转换、Ajax简单应用、轮播图

    jQuery的基本使用.JQ功能概括.JS对象与JQ对象转换.Ajax简单应用.轮播图 一.认识jQuery 1.什么是jQuery jQuery是对原生JavaScript二次封装的工具函数集合 j ...

  2. 超级详细 一听就会:利用JavaScript jQuery实现图片无限循环轮播(不借助于轮播插件)

    前言 作为一个前端工程师,无论公司是什么行业,无论你做什么端,基本都会遇到一个避不开的动画效果:循环轮播.做轮播并不难,市场上的轮播插件有很多,其中比较著名的是swiper,使用也非常简单.但轮播插件 ...

  3. 利用JavaScript jQuery实现图片无限循环轮播(不借助于轮播插件)-----转载

    前言 作为一个前端工程师,无论公司是什么行业,无论你做什么端,基本都会遇到一个避不开的动画效果:循环轮播.做轮播并不难,市场上的轮播插件有很多,其中比较著名的是swiper,使用也非常简单.但轮播插件 ...

  4. 仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图

    仿百度壁纸客户端(二)--主页自定义ViewPager广告定时轮播图 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Fragment 仿百度壁纸客户端( ...

  5. swiper轮播 swiper整屏轮播

    近期坐了几个移动端 整屏轮播的  效果 之前都是自己一个个写,之前听说过swiper插件,没有使用过,今天一尝试,果然,爽 使用方法示例 <div class="swiper-cont ...

  6. Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View

    最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...

  7. 针对淡入淡出的定时轮播效果js

    如果不使用jquery的fadeIn和fadeOut的接口和不适用animate情况下,如果要做用js实现淡入淡出轮播效果,我所想到的办法就是使用css3新特性transition(注意好兼容性). ...

  8. 实现全屏轮播,并且轮播div中的文字盒子一直自动垂直居中

    效果如下: 直接上代码了: 说明:轮播图基于swiper.js,自行下载.css在最后 <!DOCTYPE html> <html lang="en"> & ...

  9. 利用jQuery实现图片无限循环轮播(不借助于轮播插件)

    原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...

随机推荐

  1. burpsuite常见问题

    中文乱码问题:https://www.cnblogs.com/bk76120/p/12400092.html 无法抓取本地或局域网其他主机的包 去掉"对于本地地址不使用代理服务器" ...

  2. 通过Xshell或Xftp链接Windows10子系统Linux

    1.打开linux系统 2.切换到root角色 sudo -i //切换到root 并输入密码 3.卸载安装ssh server sudo apt-get remove openssh-server ...

  3. oracel数据库ORA-28001: the password has expired

    调试c#项目时登录用户不成功ORA-28001: the password has expired错误 密码过期失效 网上查了一下,是Oracle11g密码过期的原因 Oracle提示错误消息ORA- ...

  4. gcc与g++区别;编译选项

    gcc与g++区别:可以相互编译,但是gcc不会自动进行c++的链接 gcc g++编译选项: -E 预处理 -S 编译指定源文件但是不尽兴汇编 -c 编译汇编,但是不进行链接 -o 编译成可执行文件 ...

  5. JZ-018-二叉树的镜像

    二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 题目链接: 二叉树的镜像 代码 /** * 标题:二叉树的镜像 * 题目描述 * 操作给定的二叉树,将其变换为源二叉树的镜像. * ...

  6. VS Code远程链接报错Could not establish connection to “hz.matpool.com”

    VS Code远程链接报错Could not establish connection to "hz-t2.matpool.com" 输出的部分内容 安装Git git for w ...

  7. php 原生分页

    <?php // 连接数据库 $link = mysqli_connect("127.0.0.1", "root", "root", ...

  8. Thinkphp3.2数据库字段自动转小写,字段大小写自动转换,以及thinkphp3一些bug

    公司在使用thinkphp框架,版本也有些老,被一些bug坑了,记录一下 自动转小写解决办法,最简单的就是在配置文件加上 'DB_PARAMS' => [\PDO::ATTR_CASE => ...

  9. (转载)字符编码那点事:快速理解ASCII、Unicode、GBK和UTF-8

  10. 20192204 2019-2020-2 《Python程序设计》实验四报告

    20192204 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1922 姓名: 李龙威 学号:20192204 实验教师 ...