html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/swiper-land.css"/>
</head>
<body>
<div id="swiper">
<ul class="swiper-box">
<li>![](img-land/01.jpg)</li>
<li>![](img-land/02.jpg)</li>
<li>![](img-land/03.jpg)</li>
<li>![](img-land/04.jpg)</li>
<li>![](img-land/05.jpg)</li>
<li>![](img-land/06.jpg)</li>
</ul>
</div>
<script src="js/swiper-land.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
less
@charset "utf-8";
*{margin: 0;padding: 0;}
li{list-style: none;}
#swiper{width: 100%;height: 200px;overflow: hidden;
.swiper-box{width: 600%;height: 100%;display: flex;flex-direction: row;flex-wrap: nowrap;
li{width: 100%;height: 100%;flex-basis: 100%;
img{width: 100%;height: 100%;display: block;}
}
}
}
javascript
var oswiper = document.getElementById('swiper');
var oswiperbox = oswiper.getElementsByClassName('swiper-box')[0];
var oli = oswiperbox.getElementsByTagName('li');
var starX = '';
var startranslateX;
var num = 0;
init();
oswiperbox.addEventListener('touchstart',function(e){
var starpoint = e.changedTouches[0];
starX = starpoint.clientX;
oswiperbox.style.transition = '';
startranslateX = cssTransform(oswiperbox,'translateX');
})
oswiperbox.addEventListener('touchmove',function(e){
var movePoint = e.changedTouches[0]
var moveX = movePoint.clientX;
var ll = parseInt(moveX)-parseInt(starX);
var disx = ll+startranslateX;
cssTransform(oswiperbox,'translateX',disx);
})
oswiperbox.addEventListener('touchend',function(e){
var endtranslateX = cssTransform(oswiperbox,'translateX');
num = Math.round(-endtranslateX/oswiper.offsetWidth);
console.log(num)
if(num >= oli.length){
num = oli.length-1;
autoPlay();
}else if(num < 0){
num = 0;
autoPlay();
}else{
autoPlay();
}
})
function autoPlay(){
oswiperbox.style.transition = '0.5s';
cssTransform(oswiperbox,'translateX',-num*oswiper.offsetWidth);
}
function init(){
oswiperbox.style.width = oli.length+'00%';
cssTransform(oswiperbox,'translateX',0);
cssTransform(oswiperbox,'translateZ',0);
}
function cssTransform(obj,attr,val){
obj.transform = obj.transform || {};
if(arguments.length === 3){
obj.transform[attr] = val;
var strval = '';
for(var key in obj.transform){
strval += key + '('+obj.transform[key]+'px)'
}
obj.style.transform = strval;
}else if(arguments.length === 2){
val = obj.transform[attr];
if(typeof val === 'undefined'){
val = 0;
}
return val;
}
}
网友评论