h5頁面實現長按刪除的效果
直接貼上複製到編輯器中進行檢視效果
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>長按刪除效果</title>
<script src="http://www.jq22.com/jquery/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
ul{
border:1px solid black;
}
li{
padding:10px 20px;
background:#ccc;
border-bottom:1px solid black;
}
</style>
</head>
<body>
<div class="container">
<h2>長按列表刪除</h2>
<ul class="list-unstyled">
<li class="touchMe">列表1</li>
<li class="touchMe">列表2</li>
<li class="touchMe">列表3</li>
</ul>
</div>
<script>
var timeOutEvent = 0;
$(function(){
$('.touchMe').on({
touchstart:function(e){
console.log($(this));
var index = $(this).index();
// 將當前元素的索引作為引數進行傳遞
timeOutEvent = setTimeout("longPress("+index+")",500);
e.preventDefault();
},
touchmove:function(){
clearTimeout(timeOutEvent);
timeOutEvent = 0;
},
touchend:function(){
clearTimeout(timeOutEvent);
if(timeOutEvent!=0){
alert('你這是點選,不是長按');
}
return false;
}
});
});
function longPress(t){
timeOutEvent = 0;
if(confirm('您確定要刪除?' + t)){
// 用傳遞過來的引數定位當前元素
$('.touchMe').eq(t).remove();
console.log('已刪除');
}
}
</script>
</body>
</html>