react 中列表实现左滑删除

基本布局实现:

render(){
  return (
       {
          dataArray.map((item, index) => (
              <div key={index} id={"swipeable" + index} className={"swipeable"} style={{position: 'relative'}}>
                    <Ons.ListItem>
                        滑动列表{item.content}
                    </Ons.ListItem>
                    <div id={'delButton' + index} 
                        onClick={() => this.deleteItem(item.id, index)}>
                        删除
                    </div>
                </div>
            ));
        }
    )
}

定义屏幕触摸开始事件,滑动中事件,以及滑动结束的事件,允许向左滑动80px,而且同一时间只能有一条数据在滑开状态。

    //触屏开始事件
    touchStartFun(e: any) {
        console.log("touchStartFun");
        if (lastObj !== null && preObj === null) {
            $(lastObj).animate({"left": "0px"}, 300);
            lastObj = null;
        } else {
            pos = {x: 0, y: 0};
            preObj = e.currentTarget;
            Left = parseInt($(preObj).css('left'));
            let touch = e.touches[0];
            pos.x = touch.pageX;
            pos.y = touch.pageY;
            run = 0;
        }
    }

    //触屏滑动事件
    touchmoveFun(e: any) {
        console.log("touchmoveFun");
        if (preObj !== null && lastObj === null) {
            let touch = e.touches[0];
            if (run === 0) {
                if (touch.pageX === pos.x && touch.pageY !== pos.y) {
                    run = 2;
                }
                if (touch.pageX !== pos.x && touch.pageY === pos.y) {
                    run = 1;
                    pos.x = touch.pageX;
                }
                if (touch.pageX !== pos.x && touch.pageY !== pos.y) {
                    if (Math.abs(touch.pageX - pos.x) > Math.abs(touch.pageY - pos.y)) {
                        run = 1;
                        pos.x = touch.pageX;
                    } else {
                        run = 2;
                    }
                }
                if (touch.pageX === pos.x && touch.pageY === pos.y) {
                    run = 0;
                }
            } else if (run === 1) {
                e.preventDefault();
                let len = touch.pageX - pos.x;
                if (Left >= 0) {
                    if (len >= -80 && len < 0) {
                        $(e.currentTarget).css({"left": len + "px"});
                    } else if (len < -80) {
                        $(e.currentTarget).css({"left": -80 + "px"});
                    } else if (len > 0) {
                        $(e.currentTarget).css({"left": 0 + "px"});
                    }
                } else if (Left <= -80) {
                    if (len <= 80 && len > 0) {
                        $(e.currentTarget).css({"left": len - 80 + "px"});
                    } else if (len > 80) {
                        $(e.currentTarget).css({"left": 0 + "px"});
                    } else if (len < 0) {
                        $(e.currentTarget).css({"left": -80 + "px"});
                    }
                }
            }
        }
    }
    
    //触屏结束事件
    touchendFun(e: any) {
        console.log("touchendFun");
        if (preObj !== null && lastObj === null) {
            let touch = e.changedTouches[0].pageX;
            let len = touch - pos.x;
            if (run === 1) {
                e.preventDefault();
                if (len <= -40) {
                    $(preObj).animate({"left": "-80px"}, 150);
                    lastObj = preObj;
                    preObj = null;
                    isCanClick = false;
                } else if (len > -40 && len < 0 && Left >= 0) {
                    $(preObj).animate({"left": "0px"}, 150);
                    preObj = null;
                    lastObj = null;
                    isCanClick = true;
                } else if (len > 0 && len < 40) {
                    $(preObj).animate({"left": "-80px"}, 150);
                    lastObj = preObj;
                    preObj = null;
                    isCanClick = false;
                } else if (len >= 40) {
                    $(preObj).animate({"left": "0px"}, 150);
                    preObj = null;
                    lastObj = null;
                    isCanClick = true;
                }
            }
        }
    }

绑定事件:

    componentDidUpdate() {
        if (this.dataFlag) {
            let that = this;
            ons.ready(function () {
                let swipeItems = (that.refs.messageDom as HTMLElement).querySelectorAll('.swipeable');
                for (let i = 0; i < swipeItems.length; i++) {//循环给每个消息的item绑定触屏事件
                    $(swipeItems[i]).bind("touchstart", that.touchStartFun);
                    $(swipeItems[i]).bind("touchmove", that.touchmoveFun);
                    $(swipeItems[i]).bind("touchend", that.touchendFun);
                }
                domNum = swipeItems.length;
            });
        }
    }

    componentWillUpdate(nextProps: any, nextState: any) {
        //只有在某个条件下才会绑定事件
        if ( canBindListener ) {
            this.dataFlag = true;
        } else {    //之后都为false不会重复绑定dom监听事件
            this.dataFlag = false;
        }
    }

页面销毁,移除事件:

    componentWillUnmount() {
        let swipeItems = (this.refs.messageDom as HTMLElement).querySelectorAll('.swipeable');
        for (let i = 0; i < swipeItems.length; i++) {//循环给消息的每个item解绑触屏事件
            $(swipeItems[i]).unbind("touchstart");
            $(swipeItems[i]).unbind("touchmove");
            $(swipeItems[i]).unbind("touchend");
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,995评论 4 61
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,670评论 1 11
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,618评论 1 32
  • 在一些成功故事里,所以人都会强调那些成功者有坚定的信念,或与众不同的信念,为了信念甚至能忍受苦难,牺牲生命,看起来...
    lancelot丶阅读 164评论 2 0
  • 中医理论认为:“肾气足,百病除”。 人体内的左肾为阳,右肾为阴。左肾司六腑之功能,右肾辖五脏之运行。肾对五脏六腑起...
    兰花_9a59阅读 724评论 1 7

友情链接更多精彩内容