Detect left/right-swipe on touch-devices, but allow up/down-scrolling

前端 未结 5 1467
逝去的感伤
逝去的感伤 2020-12-04 11:24

I need to detect and react to left/right-swipes, but want to give the user the ability to scroll on the same element, so as long as he moves his finger only left/right with

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 12:13

    I wrote my own touch handler events.maybe this helps you

    it checks for:

    fast click : 'fc'

    swipe left : 'swl'

    swipe right : 'swr'

    swipe up : 'swu'

    swipe down : 'swd'

    each check initializes it's correspondent event.but you can scroll and do whatever else you do normally. you just have some new events.

    you need swl swr, I aslo suggest to use fc (fastclick) for click events... it's much faster than normal click.

    window.onload = function() {
        (function(d) {
            var
                ce = function(e, n) {
                    var a = document.createEvent("CustomEvent");
                    a.initCustomEvent(n, true, true, e.target);
                    e.target.dispatchEvent(a);
                    a = null;
                    return false
                },
                nm = true,
                sp = {
                    x: 0,
                    y: 0
                },
                ep = {
                    x: 0,
                    y: 0
                },
                touch = {
                    touchstart: function(e) {
                        sp = {
                            x: e.touches[0].pageX,
                            y: e.touches[0].pageY
                        }
                    },
                    touchmove: function(e) {
                        nm = false;
                        ep = {
                            x: e.touches[0].pageX,
                            y: e.touches[0].pageY
                        }
                    },
                    touchend: function(e) {
                        if (nm) {
                            ce(e, 'fc')
                        } else {
                            var x = ep.x - sp.x,
                                xr = Math.abs(x),
                                y = ep.y - sp.y,
                                yr = Math.abs(y);
                            if (Math.max(xr, yr) > 20) {
                                ce(e, (xr > yr ? (x < 0 ? 'swl' : 'swr') : (y < 0 ? 'swu' : 'swd')))
                            }
                        };
                        nm = true
                    },
                    touchcancel: function(e) {
                        nm = false
                    }
                };
            for (var a in touch) {
                d.addEventListener(a, touch[a], false);
            }
        })(document);
        //EXAMPLE OF USE
        var h = function(e) {
            console.log(e.type, e)
        };
        document.body.addEventListener('fc', h, false); // 0-50ms vs 500ms with normal click
        document.body.addEventListener('swl', h, false);
        document.body.addEventListener('swr', h, false);
        document.body.addEventListener('swu', h, false);
        document.body.addEventListener('swd', h, false);
    }
    

    in this case h is my handler for every type of event and i add the handlers to the body.

    for what i understand your question you just have to write

    YOURELEMENT.addEventListener('swr',YOURSWIPERIGHTFUNCTION,false);
    YOURELEMENT.addEventListener('swl',YOURSWIPELEFTFUNCTION,false);
    

    to handle multiple elements and the same function... just add one handler.

    so if you have

    • 1
    • 2
    • 3

    you do:

    var deleteli=function(e){
        var li=e.target;
        console.log('deleting '+li.textContent);
    }
    document.getElementById('ul').addEventListener('swl',deleteli,false);
    

    same for fc & swr

    there is a bug in ios: don't use alert() .. it will execute 2 times.

提交回复
热议问题