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. 在用Scrapy进行爬虫时碰到的错误

    1.module() takes at most 2 arguments (3 given) 解决方法:导入Spider类时,是from scrapy import Spider而不是from scr ...

  2. Linux卸载源码编译安装的软件

    使用auto-apt 和 checkinstall,具体命令如下 #安装auto-apt和checkinstall apt install auto-apt checkinstall #在源码目录中 ...

  3. 基于python 信用卡评分系统 的数据分析

    基于python 信用卡评分系统 的数据分析 import pandas as pd import matplotlib.pyplot as plt #导入图像库 from sklearn.ensem ...

  4. CF回顾《二分类》

    cf Educational Codeforces Round 115 (Rated for Div. 2) C题 类型:二分查找. 中文题目: C.删除两项内容 Monocarp有一个由n个整数组成 ...

  5. mybatis的几种like查询

    oracle数据库: Java代码 SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 Java代码 SELECT * FROM ...

  6. Spring Data ElasticSearch的使用

    1.什么是Spring Data Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务. S ...

  7. kubernetes里kube-proxy的ConfigMap误删除处理

    由于想要开启ipvs,没想到把cm当成pod删除了....然后就开始了修复之路 ConfigMap介绍 ConfigMap是一种API对象,用来将非加密数据保存到键值对中.可以用作环境变量.命令行参数 ...

  8. Linux 查看文件大小并按照大小排序

    使用df 命令查看当前系统磁盘的使用情况: [root@node ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/map ...

  9. 部署 Nginx +uwsgi+centos7+django+supervisor 项目

    部署CRM项目 前言 使用软件 nginx 使用nginx是为了它的反向代理功能,项目会通过Django+uWSGI+Nginx进行服务器线上部署. uWSGI python web服务器开发使用WS ...

  10. 4月11日 python学习总结 对象与类

    1.类的定义 #类的定义 class 类名: 属性='xxx' def __init__(self): self.name='enon' self.age=18 def other_func: pas ...