原理:刪除按鈕藏在列表項的右側視窗外,使用者看不到;當手指往左滑(內容右移)時,把列表項整體左移,藏在右邊的按鈕就露出來了。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=no" />
<title>小紙條</title>
<script src="assets/wap/kuaiqin/js/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="assets/wap/kuaiqin/js/jl.js" type="text/javascript"></script>
<style>
*{ margin:0px; padding:0px; font-size:14px; box-sizing:border-box; }
body{ overflow:hidden; width:100%; height:100%; }
#list{ border-top:1px solid #e6e6e6; }
ul li{
list-style-type:none; width:100%; height:60px;
position:relative; border-bottom:1px solid #e6e6e6;
}
div#header{
width:100%; height:45px; text-align:center; line-height:45px;
border-bottom:1px solid #e6e6e6; margin-bottom:10px;
}
li p{ height:30px; margin-left:75px; }
.right{ float:right; }
.time{
margin-right:24px; display:none; border-radius:50%;
width:15px; height:15px; background-color:red; margin-top:20px;
font-size:13px; text-align:center; line-height:15px; color:white;
}
.img-box{ width:40px; height:40px; margin:10px 0px 10px 24px; position:absolute; }
.blank{ width:100%; height:15px; }
/* 刪除按鈕定位在 li 右側外部,right 為負值即藏在視窗外 */
.delfriend{
height:60px; width:70px; background-color:red;
display:block; position:absolute; top:0px; right:-70px;
line-height:60px; color:#ffffff; text-align:center; z-index:13;
}
</style>
</head>
<body>
<script>
var w = $(window).width() + "px";
var h = $(window).height() + "px";
$("body").css({ "width": w, "height": h });
</script>
<div id="header">
<img src="assets/wap/kuaiqin/img/return.png"
style="position:absolute;top:5px;left:5px;z-index:100;width:10px;height:18px;margin-top:5px;"
onclick="window.location.href = 'wap/kuaiqin/index'; return false">
小紙條
</div>
<div id="list">
<div class="blank"></div>
<ul id="myf">
<li class="myfriend" data-friendid="" id="del">
<img src="" class="img-box">
<p style="line-height:40px;">
<span class="right time" id=""></span>
</p>
<p style="line-height:20px;" id="">uu</p>
<span class="delfriend">刪除</span>
</li>
</ul>
</div>
<script>
$("#myf").unbind()
.on("touchstart", ".myfriend", function (e) {
e.preventDefault();
var _a = e.originalEvent.targetTouches[0];
start = _a.pageX;
friendid = $(this).attr("data-friendid");
})
.on("touchend", ".myfriend", function (e) {
e.preventDefault();
var _a = e.originalEvent.changedTouches[0];
end = _a.pageX;
if (start - end < 0) {
// 手指右滑 → 收起重現
$("#del_" + friendid).animate({ "margin-left": "0px" }, 1000);
} else if (start - end > 0) {
// 手指左滑 → 展開刪除
$(".myfriend").not("#del_" + friendid).animate({ "margin-left": "0px" }, 1000);
$("#del_" + friendid).animate({ "margin-left": "-70px" }, 1000);
} else {
// 沒有位移視為點擊
var cls = $(e.target).attr("data-cls");
var ff = $(e.target).attr("data-friendid");
if (cls == "delfriend") {
delfriend(ff);
} else {
window.location = "wap/kuaiqin/sr_index?smallmy=" + localStorage.tokenforkuaiqin + "&friendid=" + friendid;
}
}
});
</script>
</body>
</html>
幾個可以再加強的地方:位移量最好對應手指移動的實際距離,而不是寫死 70px,手感會自然很多;展開某一項時應該順手收起其他項(程式裡已用 .not() 處理);另外建議加上「滑動超過閾值才判定為手勢」的判斷,避免輕微抖動就被誤認為滑動。