js實現(xiàn)移動端向左滑動刪除效果

使用插件alloy_finger.js

(vue方法使用)

1.引用js

<script src="js/alloy_finger/alloy_finger.vue.js"></script>
<script src="js/alloy_finger/alloy_finger.js"></script>

2.html方法的使用

<div id="container" v-cloak>
  <div v-finger:swipe="swipe" ></div>
</div>

3.js方法的使用

Vue.use(AlloyFingerVue);
var vm=new Vue({
  el:'#container',
  methods:{
    swipe:function(e){
            console.log(e);
            var _content = md_lib.parents(e.target,'cart_item_content');
            if (e.direction == 'Left') {
                console.log('向左滑動了');
            }
            if (e.direction == 'Right') {
                console.log('向右滑動了');
            }
        }
  }
})

原生

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" 
content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="viewport"
      content="width=device-width, user-scalable=no, initial-
scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<style>
*{
    margin: 0;
    padding: 0;
    list-style: none;
}
body,html{
    height: 100%;
    width:16rem;
    overflow:hidden;
}
ul{
    width:16rem;
    height:28.4rem;
}
li{
    width:20rem;
    height:2rem;
    line-height: 2rem;
    background:pink;
}
.div1{
    width:16rem;
    height:2rem;
    float:left;
}
.del{
    width:4rem;
    height:2rem;
    background:red;
    float:left;
    text-align: center;
    line-height: 2rem;
    color:#fff;
}
</style>
<script>
//設置html的字體大小,方便使用rem布局
(function(win,doc){
    function change(){
        doc.documentElement.style.fontSize = doc.documentElement.clientWidth*20/320+'px';
    }
    change();
    win.onresize = change;
})(window,document);
</script>
<script>
document.addEventListener('DOMContentLoaded',function(){
    var oUl=document.querySelector('ul');
    var aLi=document.querySelectorAll('li');
    var aDiv=document.querySelectorAll('.div1');
    var aDel=document.querySelectorAll('.del');
    var n=0;
    for(var i=0;i<aLi.length;i++){
        console.log(i);
        aLi[i].index=i;
        //滑動開始
        aLi[i].addEventListener('touchstart',function(ev){
            ev.preventDefault();
            var oldX=ev.targetTouches[0].pageX;
            console.log(oldX);
            function fnMove(ev){
                ev.preventDefault();
                var newX=ev.targetTouches[0].pageX-oldX;
                aLi[this.index].style.transform='translateX('+newX+'px)';
            }
            function fnEnd(ev){
                aLi[this.index].removeEventListener('touchmove', fnMove,false);
                aLi[this.index].removeEventListener('touchend', fnEnd,false);
                ev.preventDefault();
                var changeX=ev.changedTouches[0].pageX;
                if(oldX-changeX > 40){
                    aLi[this.index].style.transition='1s all ease';
                    aLi[this.index].style.transform='translateX(-4rem)';
                }else{
                    aLi[this.index].style.transform='translateX(0)';
                }
            }
            aLi[this.index].addEventListener('touchmove', fnMove,false);
            aLi[this.index].addEventListener('touchend', fnEnd,false);
        },false)
        //刪除按鈕
        aDel[i].index=i;
        aDel[i].addEventListener('touchstart', function(){
            this.innerHTML='';
            this.style.height='0';
            this.style.transition='0.5s all ease';
            aLi[this.index].style.height='0';
            aLi[this.index].style.transition='0.5s all ease';
            aDiv[this.index].style.height='0';
            aDiv[this.index].style.transition='0.5s all ease';
            aDiv[this.index].innerHTML='';
            this.addEventListener('transitionend',function(){
                oUl.removeChild(aLi[this.index]);
            }, false)
        },false)
    }
},false)
</script>
</head>
<body>
<ul>
    <li><div class="div1">1</div><div class="del">刪除</div></li>
    <li><div class="div1">2</div><div class="del">刪除</div></li>
    <li><div class="div1">3</div><div class="del">刪除</div></li>
    <li><div class="div1">4</div><div class="del">刪除</div></li>
    <li><div class="div1">5</div><div class="del">刪除</div></li>
    <li><div class="div1">我只是一個單純的div</div><div class="del">刪除</div></li>
    <li><div class="div1">7</div><div class="del">刪除</div></li>
</ul>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容