jQuery實現移動端左滑刪除功能
阿新 • • 發佈:2019-01-03
在手機移動端,經常需要實現類似微信聊天工具中的左滑動刪除和右滑動恢復功能。
這裡,我們可以直接使用jQuery來實現該效果。
基本原理:
- 利用jQuery中的touchstart、touchmove、touchend、touch事件和animate方法。
- 條目包裹區塊(item_wrap)和裡面的條目內容區塊(item_top)採用相對定位,刪除按鈕(item_del)採用絕對定位。
- 條目內容區塊預設是在刪除按鈕的上層,即它是蓋住了刪除按鈕。向左滑動條目內容區塊時,刪除按鈕就自動顯示出來了。
- 滑動時,利用css的left屬性實現實時滑動效果;touchend或touch時,利用animate方法實現動畫效果。
- 向左滑動大於等於刪除按鈕的一半寬度時,touchend時才完全顯示刪除按鈕;否則,恢復原狀。
- 當刪除按鈕已處於完全顯示狀態時,可通過右滑或touch條目內容區塊來恢復原狀;touch刪除按鈕時,刪除父元素節點。
完整的示例程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>移動端左滑刪除、右滑恢復效果</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js" ></script>
<style>
html {
font-family:微軟雅黑;
font-size:16px;
}
body, div, p, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, input, textarea, select{
margin:0;
padding:0;
}
.header, .footer {
background-color: #ccc;
border: 1px solid #aaa ;
height:6rem;
font-size:2.5rem;
text-align:center;
line-height:6rem;
}
.footer {
position: fixed;
bottom:0;
width:100%;
}
.item_wrap {
position:relative;
margin-top:1px;
width:100%;
text-align:center;
height:6rem;
line-height:6rem;
font-size:2rem;
}
.item_wrap .item_top {
position:relative;
top:0;
border-bottom: 1px solid #888;
background-color: #fff;
overflow:hidden;
z-index: 10;
}
.item_wrap .item_del { /* 刪除按鈕位於條目內容的下一層 */
position: absolute;
background-color: #c00;
top:0;
right: 0;
width: 15%;
color: #fff;
letter-spacing: 3px;
z-index: 9;
}
</style>
</head>
<body>
<div>
<div class="header">頭部導航</div>
<div class="main">
<div class="item_list">
<div class='item_wrap'>
<div class='item_top'>測試條目1</div>
<div class="item_del">刪除</div>
</div>
<div class='item_wrap'>
<div class='item_top'>測試條目2</div>
<div class="item_del">刪除</div>
</div>
<div class='item_wrap'>
<div class='item_top'>測試條目3</div>
<div class="item_del">刪除</div>
</div>
</div>
</div>
<div class="footer">底部區域</div>
</div>
</body>
</html>
<script>
(function(){
var start_x; // touchstart時的水平起始位置
var end_x; // touchmove過程中的水平結束位置
var touchmove = false; // 標識是否成功觸發了touchmove,預設沒有觸發。
// on方法事件繫結,可將事件繫結到新新增的子元素上
$(document).on("touchstart",'.item_top',function(e){ // 觸控開始時
if (e.originalEvent.targetTouches) {
start_x = e.originalEvent.targetTouches[0].pageX;
} else {
start_x = e.pageX;
}
// console.log(start_x);
});
$(document).on("touchmove",'.item_top',function(e){ // 觸控過程中...
var width = $('.item_del:first').width(); // 刪除按鈕的寬
var left = $(this).css('left');
if(start_x - end_x >= 5){
touchmove = true;
}
if (e.originalEvent.targetTouches) {
end_x = e.originalEvent.targetTouches[0].pageX;
} else {
end_x = e.pageX;
}
if(start_x - end_x >= 0) { // touchmove 向左移動
if(parseInt(left) > -width){
$(this).css('left', end_x-start_x+'px'); // 移動效果
}
}else{ // 向右移動
if(parseInt(left) <0){
$(this).animate({left:0},500); // 恢復原狀(動畫效果)
}
}
});
$(document).on("touchend",'.item_top',function(e){ // 觸控擡起時
var width = $('.item_del:first').width(); // 刪除按鈕的寬
var offset = start_x - end_x; // 偏移量
if(touchmove==false) {
return;
}
if( offset > 0){ // 左滑動
if(offset>=width/2){ // 偏移量大於等於刪除按鈕的一半
$(this).animate({left:-width+'px'},500); // 動畫顯示刪除按鈕
}else{
$(this).animate({left:0},500); // 恢復原狀
}
}
touchmove = false;
});
$(document).on('touch', '.item_top',function(e){ // 點選主條目時,也可恢復原狀
var width = $('.product_item_del:first').width();
var left = $(this).css('left');
if(parseInt(left)<0){
$(this).animate({left:0},500); // 恢復原狀
}
});
$(document).on('touch', '.item_del',function(e){ // 點選刪除,刪除父元素節點
$(this).parent().remove();
});
})();
</script>