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. CSS/CSS3语法新特性笔记

    CSS层叠样式表 三大特性 层叠性:相同的样式会覆盖 继承性:属性可向下继承 优先级:范围越小权重越高 选择器 基础选择器 标签选择器 1 body { 2 color:#fff; 3 } 类选择器 ...

  2. Python——条件语句及其循环

    条件语句及其循环 一. 条件语句 在条件语句中可以使用以下所有的运算符: 算术运算符:+.-.*././/.%.** 关系运算符:>.<.==.<=.>=.!= 测试运算符:i ...

  3. ASP.NET Core 6框架揭秘实例演示[19]:数据加解密与哈希

    数据保护(Data Protection)框架旨在解决数据在传输与持久化存储过程中的一致性(Integrity)和机密性(confidentiality)问题,前者用于检验接收到的数据是否经过篡改,后 ...

  4. 如何写Markdown格式文档

    Markdown Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯.它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者HTML)文档.这种语言吸收了很多在电子邮件中 ...

  5. laravel 怎么获取public路径

    app_path()   app_path函数返回app目录的绝对路径: $path = app_path();   你还可以使用app_path函数为相对于app目录的给定文件生成绝对路径: $pa ...

  6. 如何使用 Hexo 搭建个人博客

    原文链接 什么是 Hexo ? Hexo 是一个简单快速的静态博客框架,可以通过编辑 Markdown 文档生成好看的静态博客. 搭建 Hexo 要求 安装 Hexo 十分简单,只需要 Node.js ...

  7. MYSQL数年库安装

    MySQL系列 MySQL 的三大主要分支mysqlmariadbpercona Server MySQL系列2.2.2.1 MySQL 的三大主要分支mysqlmariadbpercona Serv ...

  8. mysql更改my.ini配置文件以后mysql服务无法启动

    最近在调试mysql时,更改了mysql的端口以后发现,mysql怎么改都启动不了,从其它机器重新复制一个my.ini文件就可以启动,这是由于一般用记事本打开配置文件同时更改的ini的格式,我们需要重 ...

  9. centos7 安装 nginx-1.18.0 并设置开机自启动

    一.到官网下载nginx Mainline  version:  nginx主力版本,为开发版 Stable version: 稳定版,在生产环境中选择此版本进行安装 Legacy versions: ...

  10. ActiveMQ-5.9-笔记-02