最近在做移動(dòng)端項(xiàng)目時(shí)谓媒,需要實(shí)現(xiàn)一個(gè)列表頁面的每一項(xiàng)item向左滑動(dòng)時(shí)出現(xiàn)相應(yīng)的刪除按鈕句惯,本來想著直接使用zepto的touch.js插件,因?yàn)橹皩?shí)現(xiàn)相同的功能時(shí)用過這個(gè)插件拷淘,當(dāng)時(shí)還挺好用的指孤,直接使用它的swipeLeft和swipeRight方法即可,可是今天又開始做這個(gè)功能時(shí)结洼,卻發(fā)現(xiàn)這兩個(gè)方法在使用時(shí)毫無效果松忍,一點(diǎn)反應(yīng)都沒有筷厘。上網(wǎng)查資料,又下載了最新版本的zepto和touch.js酥艳,都沒有用,也不知為什么更扁?所以就棄用了這個(gè)插件赫冬。
下面是我后來實(shí)現(xiàn)后的代碼,其實(shí)就是用了原生js的touch事件膛薛,再結(jié)合觸摸點(diǎn)的坐標(biāo)來判斷左滑和右滑,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>js側(cè)滑顯示刪除按鈕</title>
<style>
*{margin:0;padding:0;}
body{font-size:.14rem;}
li{list-style:none;}
i{font-style:normal;}
a{color:#393939;text-decoration:none;}
.list{overflow:hidden;margin-top:.2rem;padding-left:.3rem;border-top:1px solid #ddd;}
.list li{overflow:hidden;width:120%;border-bottom:1px solid #ddd;}
.list li a{display:block;height:.88rem;line-height:.88rem;-webkit-transition:all 0.3s linear;transition:all 0.3s linear;}
.list li i{float:right;width:15%;text-align:center;background:#E2421B;color:#fff;}
.swipeleft{transform:translateX(-15%);-webkit-transform:translateX(-15%);}
</style>
<script>
//計(jì)算根節(jié)點(diǎn)HTML的字體大小
function resizeRoot(){
var deviceWidth = document.documentElement.clientWidth,
num = 750,
num1 = num / 100;
if(deviceWidth > num){
deviceWidth = num;
}
document.documentElement.style.fontSize = deviceWidth / num1 + "px";
}
//根節(jié)點(diǎn)HTML的字體大小初始化
resizeRoot();
window.onresize = function(){
resizeRoot();
};
</script>
</head>
<body>
<section>
<div class="list">
<ul>
<li><a href="#">側(cè)滑顯示刪除按鈕1<i>刪除</i></a></li>
<li><a href="#">側(cè)滑顯示刪除按鈕2<i>刪除</i></a></li>
<li><a href="#">側(cè)滑顯示刪除按鈕3<i>刪除</i></a></li>
</ul>
</div>
<script>
//側(cè)滑顯示刪除按鈕
var expansion = null; //是否存在展開的list
var container = document.querySelectorAll('.list li a');
for(var i = 0; i < container.length; i++){
var x, y, X, Y, swipeX, swipeY;
container[i].addEventListener('touchstart', function(event) {
x = event.changedTouches[0].pageX;
y = event.changedTouches[0].pageY;
swipeX = true;
swipeY = true ;
if(expansion){ //判斷是否展開,如果展開則收起
expansion.className = "";
}
});
container[i].addEventListener('touchmove', function(event){
X = event.changedTouches[0].pageX;
Y = event.changedTouches[0].pageY;
// 左右滑動(dòng)
if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
// 阻止事件冒泡
event.stopPropagation();
if(X - x > 10){ //右滑
event.preventDefault();
this.className = ""; //右滑收起
}
if(x - X > 10){ //左滑
event.preventDefault();
this.className = "swipeleft"; //左滑展開
expansion = this;
}
swipeY = false;
}
// 上下滑動(dòng)
if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
swipeX = false;
}
});
}
</script>
</body>
</html>
zepto
function addsc(tname,naclass,naju,nafu,nazi){
var expansion=null;
tname.each(function(event){
var x, y, X, Y, swipeX, swipeY;
//獲取每一個(gè)滑塊的滑動(dòng)距離
$(this).on("touchstart",function(event){
x = event.changedTouches[0].pageX;
y = event.changedTouches[0].pageY;
swipeX = true;
swipeY = true;
// console.log("x"+x+"y"+y)
if(expansion){
if($(this).is(naclass)){ $(this).removeClass(naclass); }
else{ $(this).parents(nafu).siblings().children(nazi).removeClass(naclass); }
}
})
$(this).on("touchmove",function(event){
X = event.changedTouches[0].pageX;
Y = event.changedTouches[0].pageY;
// 左右滑動(dòng)
if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
event.stopPropagation();
if(x-X> naju){ //左滑
event.preventDefault();
$(this).addClass(naclass)
expansion = true;
}
if(X-x>naju){//右滑
event.preventDefault();
$(this).removeClass(naclass)
expansion = " ";
}
swipeY = false;
}
if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
swipeX = false;
}
})
})
}
//調(diào)用滑動(dòng)模塊
if($(".fh701").length>0){
$(".fh701").on("touchstart myCrrrg ",function(event){
addsc($(".fh628-dv .soo29-dow .soo29-daw a"),"swipeleft",25,".soo29-daw","a");
event.stopPropagation();
}).triggerHandler("myCrrrg")
}
$("body").on("touchstart", function(e) {
e.preventDefault();
startX = e.originalEvent.changedTouches[0].pageX,
startY = e.originalEvent.changedTouches[0].pageY;
});
$("body").on("touchmove", function(e) {
e.preventDefault();
moveEndX = e.originalEvent.changedTouches[0].pageX,
moveEndY = e.originalEvent.changedTouches[0].pageY,
X = moveEndX - startX,
Y = moveEndY - startY;
if ( X > 0 ) {
alert("left 2 right");
}
else if ( X < 0 ) {
alert("right 2 left");
}
else if ( Y > 0) {
alert("top 2 bottom");
}
else if ( Y < 0 ) {
alert("bottom 2 top");
}
else{
alert("just touch");
}
});
http://www.qdfuns.com/notes/18161/e2f2dc4b5f307375757fc562e4b33788.html