【常见】移动端实例——图片无缝滚动
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,user-scalable=no">
<title>document</title>
<script type="text/javascript">
//获取html
var html = document.documentelement;
//设置html的字体大小 = 可视区的宽度 / 15
html.style.fontsize = html.clientwidth / 15 + 'px';
//阻止pc和浏览器默认行为。
document.addeventlistener('touchstart',function(ev){
ev.preventdefault();
});
</script>
<style>
body{
margin: 0;
}
.wrap{
height: 4.68rem;
position: relative;
}
.list{
padding: 0;
margin: 0;
width:400%;
position: absolute;
top:0;
left:0;
list-style: none;
}
.list li{
float: left;
width:15rem;
}
.list img{
width:15rem;
display: block;
}
nav{
width:15rem;
height: 10px;
position: absolute;
bottom:5px;
z-index: 1;
text-align:center;
}
nav a{
width:15px;
height: 15px;
display: inline-block;
background: red;
border-radius:50%;
vertical-align:top;
}
nav a.active{
background:#fff;
}
</style>
</head>
<body>
<section class="wrap">
<ul class="list">
<li>
<img src="img/img.jpg" alt="" />
</li>
<li>
<img src="img/img2.jpg" alt="" />
</li>
<li>
<img src="img/img3.jpg" alt="" />
</li>
<li>
<img src="img/img4.jpg" alt="" />
</li>
</ul>
<nav>
<a href="javascript:;" class="active"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</nav>
</section>
<script type="text/javascript">
var wrap=document.getelementsbyclassname("wrap")[0],
list=document.getelementsbyclassname("list")[0],
disx=0,
listl=0,
n=0,
w=wrap.clientwidth,
s=0,
timer=0;
a=document.getelementsbytagname("a");
list.addeventlistener("touchstart",start);
list.addeventlistener("touchmove",move);
list.addeventlistener("touchend",end);
list.innerhtml+=list.innerhtml;
len=list.children.length;
console.log(len);
list.style.width=w*len+"px";
function start(ev){
clearinterval(timer);
var e=ev.changedtouches[0];
disx=e.pagex;
list.style.transition="none";
var num=math.round(list.offsetleft/w);
list.style.left=num*w+"px";
if(num==0){
num=a.length;
list.style.left=-num*w+"px";
}
if(-num==len-1){
num=a.length-1;
list.style.left=-num*w+"px";
}
listl=this.offsetleft;
}
function move(ev){
var e=ev.changedtouches[0];
list.style.left=(e.pagex-disx)+listl+"px";
}
function end(){
var num=math.round(list.offsetleft/w);
console.log(num)
list.style.transition="0.5s";
list.style.left=num*w+"px";
a[n].classname="";
a[(-num)%a.length].classname="active";
n=(-num)%a.length;
timer=setinterval(function(){
inn();
},1000)
s=-num;
}
timer=setinterval(function(){
inn();
},1000)
function inn(){
s++;
list.style.left=-s*w+"px";
list.style.transition="0.5s";
console.log(s);
if(s>len-1){
s=a.length-1;
list.style.transition="none";
list.style.left=-s*w+"px";
console.log(list.style.left);
settimeout(function(){
list.style.transition="0.5s";
s++;
list.style.left=-s*w+"px";
a[n].classname="";
a[s%a.length].classname="active";
n=s%a.length;
},30)
}
a[n].classname="";
a[s%a.length].classname="active";
n=s%a.length;
}
</script>
</body>
</html>
上一篇: 序列化组建