代码如下
<style>
*{
margin: 0;
padding: 0
}
div ul{
overflow: hidden;
}
div ul li{
width: 130%;
height: 50px;
background: #000;
line-height: 50px;
color: #fff;
font-size: 16px;
list-style: none;
margin-top: 10px
}
div ul li span{
float: right;
line-height: 50px;
color: #fff;
font-size: 16px
}
.leftyd{
margin-left: -15%;
transition:.5s;
}
.rightyd{
margin-left: 0;
transition:.5s;
}
</style>
<div class='divs'>
<ul>
<li>1
<span>
删除
</span>
</li>
<li>2
<span>
删除
</span>
</li>
<li>3
<span>
删除
</span>
</li>
<li>4
<span>
删除
</span>
</li>
<li>5
<span>
删除
</span>
</li>
<li>6
<span>
删除
</span>
</li>
</ul>
</div>
var divs = $('.divs ul li');
lefttest(divs)
var lefttest = function (e) {
var lines = e;
var len = lines.length;
var lastX, lastXForMobile;
var pressedObj;
var lastLeftObj;
var start;
for (var i = 0; i < len; ++i) {
lines[i].addEventListener('touchstart', function(e){
lastXForMobile = e.changedTouches[0].pageX;
pressedObj = this;
var touches = event.touches[0];
start = {
x: touches.pageX,
y: touches.pageY
};
});
lines[i].addEventListener('touchmove',function(e){
var touches = event.touches[0];
delta = {
x: touches.pageX - start.x,
y: touches.pageY - start.y
};
if (Math.abs(delta.x) > Math.abs(delta.y)) {
event.preventDefault();
}
});
lines[i].addEventListener('touchend', function(e){
if (lastLeftObj && pressedObj != lastLeftObj) {
$(lastLeftObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = null;
}
var diffX = e.changedTouches[0].pageX - lastXForMobile;
if (diffX < -150) {
$(pressedObj).removeClass('rightyd').addClass('leftyd');
lastLeftObj && lastLeftObj != pressedObj &&
$(lastLeftObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = pressedObj;
} else if (diffX > 150) {
if (pressedObj == lastLeftObj) {
$(pressedObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = null;
}
}
});
}
for (var i = 0; i < len; ++i) {
$(lines[i]).bind('mousedown', function(e){
lastX = e.clientX;
pressedObj = this;
});
$(lines[i]).bind('mouseup', function(e){
if (lastLeftObj && pressedObj != lastLeftObj) {
$(lastLeftObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = null;
}
var diffX = e.clientX - lastX;
if (diffX < -150) {
$(pressedObj).addClass('leftyd');
lastLeftObj && lastLeftObj != pressedObj &&
$(lastLeftObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = pressedObj;
} else if (diffX > 150) {
if (pressedObj == lastLeftObj) {
$(pressedObj).removeClass('leftyd').addClass('rightyd');
lastLeftObj = null;
}
}
});
}
}