JS類比左滑刪除

來源:互聯網
上載者:User

標籤:需要   時間   部分   border   parent   部落格   apple   html   att   

接觸前端有半年的時間了,不會的都會去搜尋別人的代碼,這是本人第一篇部落格,也是因為在搜尋了很多關於左滑刪除的例子,但是結果並不是很好,雖然樣式上面實現了,但是大多數都會有無法點擊刪除或是無法控制事件的困擾,所以簡單整理一種寫法,菜鳥一隻,只求不噴~

先簡單說一下思路,在一個container容器裡面,定義兩個平級的標籤   【顯示內容+刪除按鈕】,,按鈕絕對位置在右側,z-index 在後方,需要固定寬度,當滑動顯示內容的部分,按鈕顯示出來。通過 active 的添加和刪除實現,下面是代碼部分:

首先,HTML部分,很簡單,一個簡單的內容,一個delete按鍵

<body>
<ul data-role="listview" class="swipe-delete">
<li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>
   <li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>
</ul>
</body>

 

加上簡單的CSS樣式(微醜)

<style>
*{
margin:0;
padding:0;
list-style: none;
}
.swipe-delete{
margin:0 auto;
}
li{
position: relative;
border-bottom:1px solid red;
}
.behind {
position:relative;
width: 100%;
height: 60px;
line-height: 60px;
z-index: 10;
background: #ffffff;

transition: all 0.3s ease;
-webkit-transition: all 0.30s ease;
}
.active{
transform: translate(-60px,0) translateZ(0);
-webkit-transform: translate(-60px,0) translateZ(0);
}
span{
position: absolute;
right:0;
bottom:0;
top:0;
width:60px;
color:#ffffff;
text-align: center;
background: red;
font-size: 20px;
line-height: 60px;
z-index: 1;
}
</style>

效果如下:

 

js來嘍~  【注】需要引入zepto.js

<script src="zepto.js"></script>
<script type="text/javascript">
$(function() {

var oBody = $(document.body), X, Y,startX,startY,moveEndX,moveEndY;
//第一步:擷取觸碰的位置座標
oBody.on("touchstart", "li", function (e) {
startX = e.originalEvent.changedTouches[0].pageX;
startY = e.originalEvent.changedTouches[0].pageY;
});
//abs()是取到元素的絕對值,下面是通過判斷touch座標來決定滑動方向
oBody.on("touchmove", "li", function (e) {
e.preventDefault();
moveEndX = e.originalEvent.changedTouches[0].pageX;
moveEndY = e.originalEvent.changedTouches[0].pageY;
X = moveEndX - startX;
Y = moveEndY - startY;
var $touchDoom = $(this).children(".behind");
//X軸的絕對值大於Y軸的絕對值並且x<0,是向左滑動
if (Math.abs(X) > Math.abs(Y) && X < 0) {
//給每一個預設滑動的元素增加預設屬性data-touch ,data-touch == ‘true‘,表示可以滑動
                if($touchDoom.attr("data-touch") == ‘true‘){
$(this).siblings(‘li‘).find(".behind").attr("data-touch","true").removeClass("active");
$touchDoom.addClass("active");
$touchDoom.attr("data-touch","false");
}
}
//反之......
else if (Math.abs(X) > Math.abs(Y) && X > 0) {
if($touchDoom.attr("data-touch") == ‘false‘){
$touchDoom.removeClass("active");
$touchDoom.attr("data-touch","true");
}
}
});

//點擊刪除

oBody.on("click", "li span", function () {
var parent = $(this).parent("li");
$(this).parent("li").hide(200,function(){
parent.remove();
});
});
});
</script>


附上完整代碼,僅供參考

<!DOCTYPE html>
<html>

<head>
<title>JQM Swipe Delete</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<style>
*{
margin:0;
padding:0;
list-style: none;
}
.swipe-delete{
margin:0 auto;
}
li{
position: relative;
border-bottom:1px solid red;
}
.behind {
position:relative;
width: 100%;
height: 60px;
line-height: 60px;
z-index: 10;
background: #ffffff;

transition: all 0.3s ease;
-webkit-transition: all 0.30s ease;
}
.active{
transform: translate(-60px,0) translateZ(0);
-webkit-transform: translate(-60px,0) translateZ(0);
}
span{
position: absolute;
right:0;
bottom:0;
top:0;
width:60px;
color:#ffffff;
text-align: center;
background: red;
font-size: 20px;
line-height: 60px;
z-index: 1;
}
</style>
</head>

<body>
<ul data-role="listview" class="swipe-delete">
<li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>
<li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>
<li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li><li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>
<li>
<div class="behind" data-touch="true">左滑刪除</div>
<span>Delete</span>
</li>

</ul>
</body>

<script src="zepto.js"></script>
<script type="text/javascript">
$(function() {

var oBody = $(document.body), X, Y,startX,startY,moveEndX,moveEndY;

oBody.on("touchstart", "li", function (e) {
startX = e.originalEvent.changedTouches[0].pageX;
startY = e.originalEvent.changedTouches[0].pageY;
});

oBody.on("touchmove", "li", function (e) {
e.preventDefault();
moveEndX = e.originalEvent.changedTouches[0].pageX;
moveEndY = e.originalEvent.changedTouches[0].pageY;
X = moveEndX - startX;
Y = moveEndY - startY;
var $touchDoom = $(this).children(".behind");
if (Math.abs(X) > Math.abs(Y) && X < 0) {
if($touchDoom.attr("data-touch") == ‘true‘){
$(this).siblings(‘li‘).find(".behind").attr("data-touch","true").removeClass("active");
$touchDoom.addClass("active");
$touchDoom.attr("data-touch","false");
}
}
else if (Math.abs(X) > Math.abs(Y) && X > 0) {
if($touchDoom.attr("data-touch") == ‘false‘){
$touchDoom.removeClass("active");
$touchDoom.attr("data-touch","true");
}
}
});

oBody.on("click", "li span", function () {
var parent = $(this).parent("li");
$(this).parent("li").hide(200,function(){
parent.remove();
});
});
});
</script>

</html>

 

JS類比左滑刪除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.