<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>h5无缝滚动</title>
<style>
*{
margin:0;
padding:0;
list-style: none;
}
#box{
width:16rem;
height: 7.5rem;
overflow: hidden;
position: relative;
maring:0 auto;
}
#box ul{
width:96rem;
height: 7.5rem;
transform: translateX(-16rem);
}
#box ul li{
width:16rem;
height:7.5rem;
float: left;
font-size:2rem;
color: #fff;
line-height: 7.5rem;
text-align: center;
}
</style>
<script>
(function(win,doc){
function change(){
doc.documentElement.style.fontSize = doc.documentElement.clientWidth*20/320 +'px';
}
change();
win.addEventListener('resize',change,false);
})(window,document);
</script>
<script>
document.addEventListener('DOMContentLoaded',function(){
var oBox = document.getElementById('box');
var oUl = document.querySelector('#box ul');
var aLi = oUl.children;
var iNow = 1;
var x = -iNow*aLi[0].offsetWidth;
var bReady = true;
oUl.addEventListener('touchstart',function(ev){
if(bReady==false){return;}
bReady = false;
oUl.style.transition = 'none';
var disX = ev.targetTouches[0].pageX - x;
var downX = ev.targetTouches[0].pageX;
function fnMove(ev){
x = ev.targetTouches[0].pageX - disX;
oUl.style.transform = 'translateX('+x+'px)';
}
function fnEnd(ev){
var upX = ev.changedTouches[0].pageX;
//判断是否移动距离大于50
if(Math.abs(upX - downX)>50){
//左边移动
if(upX - downX<0){
iNow++;
if(iNow==aLi.length){iNow=aLi.length-1;}
}else{
//右边移动
iNow--;
if(iNow==-1){iNow=0;}
}
}
x = -iNow*aLi[0].offsetWidth;
oUl.style.transform = 'translateX('+x+'px)';
oUl.style.transition = '200ms all ease';
function tEnd(){
if(iNow==5){
iNow=1;
}
if(iNow==0){iNow=4;}
oUl.style.transition = 'none'
x = -iNow*aLi[0].offsetWidth;
oUl.style.transform = 'translateX('+x+'px)';
bReady = true;
}
oUl.addEventListener('transitionend',tEnd,false);
document.removeEventListener('touchend',fnEnd,false);
document.removeEventListener('touchmove',fnMove,false);
}
document.addEventListener('touchmove',fnMove,false);
document.addEventListener('touchend',fnEnd,false);
ev.preventDefault();
},false);
},false);
</script>
</head>
<body>
<div id="box">
<ul>
<li style ="background:green">3</li>
<li style="background: red;">0</li>
<li style="background: yellow;">1</li>
<li style="background: blue;">2</li>
<li style ="background:green">3</li>
<li style="background: red;">0</li>
</ul>
</div>
</body>
</html>
h5无缝滚动
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前端入坑纪 03 让假期来得更猛烈些吧!今天假期第一天,来个新闻滚动! 今天总感觉直接切换过来太突兀,想想还是这样...