<div class="content">
<button class="left">left</button>
<button class="right">right</button>
<div class="index"></div>
<div class="lists">
<!--&lt;!&ndash;width: item的数量÷3乘以100%&ndash;&gt;-->
<div class="box">
<!--width: 1÷item的数量乘以100%-->
<div class="item">
<img src="a.png" alt="a">
<p>aaa</p>
</div>
<div class="item active">
<img src="b.png" alt="b">
<p>bbb</p>
</div>
<div class="item">
<img src="c.png" alt="c">
<p>ccc</p>
</div>
<div class="item">
<img src="d.png" alt="d">
<p>ddd</p>
</div>
<div class="item">
<img src="e.png" alt="e">
<p>eee</p>
</div>
<div class="item">
<img src="f.png" alt="f">
<p>ffff</p>
</div>
</div>
</div>
</div>
      <script>
$(function(){
// 循环数据 假设有个数组,有10条数据
// var arr = [1,2,3,4,5,6,7,8,9,10];
// var arr_len = arr.length;
// var box = '<div class="box" style="width: '+arr_len/3*100+'%;"></div>';
// $('.lists').append(box);
// for (var i = 0; i < arr_len;i++){
// var newDiv = document.createElement('div');
// newDiv.innerHTML = '<img src=""/>'+
// '<p>' +
// (i+1)+
// '</p>';
// newDiv.className = 'item '+(i==1?'active':'');
// newDiv.style = 'width: '+1/arr_len*100+'%;';
// newDiv.onclick = (function(ind) {
// return function () {
// index = ind-1;
// console.log(ind)
// $(".box").animate({left: -index*100/3+"%"})
// $(".item").removeClass('active')
// $($(".item")[index+1]).addClass('active');
// $('.index').text(index+2)
// }
// })(i) ;
// $('.box').append(newDiv);
// } var arr_len = $('.item').length; $('.box').css({width: arr_len/3*100+'%'})
$('.item').css({width: 1/arr_len*100+'%'}) $('.item').on('click',function (e) {
var _this = $(e.target);
if (!_this.hasClass('item')){
_this = _this.parents('.item');
}
index = _this.index('.item')-1;
$(".box").animate({left: -index*100/3+"%"})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
}) var index = 0;
var len = arr_len;
var temp = true;
var pageX,pageY;
$('.content').on('touchstart',function (e) {
var touches = e.originalEvent.targetTouches[0];
pageX = touches.pageX;
pageY = touches.pageY;
}).on('touchmove',function (e) {
var touches = e.originalEvent.targetTouches[0];
if (pageX>touches.pageX+20){
left()
}else if (pageX<touches.pageX-20){
right()
}
})
$(".left").on('click',left)
$(".right").on('click',right)
function left() {
console.log(index,temp,'left')
if (index < len-2&&temp){
index++;
move(index)
}
}
function right() {
if (index > 0&&temp){
index--;
move(index)
}
}
function move(index) {
if (temp){
temp = false;
var left = $(".box").offset().left;
$(".box").animate({left: -index*100/3+"%"},function () {
temp = true;
})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
} } })
      .lists {
position: relative;
height: 100px;
overflow: hidden;
}
.box {
position: absolute;
}
.item {
float: left;
background: #008000;
height: 100px;
}
.item img {
width: 20px;
display: block;
margin: 0 auto;
}
.item p {
text-align: center;
}
.item.active {
background: #0000FF;
font-size: 30px;
}
.item.active img {
width: 40px;
}
.item.active p {
font-size: 30px;
}

最新文章

  1. JSF primefaces dataTable paginator 表格分页 问题
  2. golang笔记——函数与方法
  3. Web之路笔记之一
  4. 关于CSS初步入门简述1
  5. jquery on()方法绑定多个选择器,多个事件
  6. cocos代码研究(1)sprite学习笔记
  7. cocos2dx 网络编程(CCHttpRequest和CURL两个方式)
  8. C#操作XML的完整例子——XmlDocument篇
  9. bing 输入法,切换简体、繁体快捷键与myeclipse 格式化代码冲突。。
  10. css变形几大属性
  11. (转)通过在 Page 指令或 配置节中设置 validateRequest=false 可以禁用请求验证
  12. element 时间选择器——年
  13. html5/h5课件如何制作?
  14. pycharm导入自己写的.py文件时,模块下方出现红色波浪线解决
  15. MSSQL数据导出到MYSQL
  16. HTML &lt;form&gt; action 属性
  17. 20155326刘美岑 《网络对抗》Exp1 PC平台逆向破解
  18. springboot jpa 审计
  19. C#设计模式--设配器模式
  20. 360电影主页和详情页爬去入Mysql库链表读取--lowbiprogrammer

热门文章

  1. EIGRP-6-EIGRP数据包
  2. java 集合解析
  3. 树的计数 Prufer序列+Cayley公式
  4. Python Unittest - Test Fixtures 测试夹具
  5. ubuntu apache2配置多站点
  6. SQL海量数据读写性能优化
  7. C# 多线程之线程池
  8. 网页title旁边的小图片
  9. 从零开始的全栈工程师——js篇2.2
  10. 【Java】Maven 常用命令