Horizontal swipe slider with jQuery and touch devices support?

前端 未结 15 2278
野趣味
野趣味 2020-12-22 19:04

I need to make a product slider like this ( see red area ) swipe slider with momentum.

It should work on Desktop, iPad and Mobile browser. Do you know any jquery/jq

15条回答
  •  無奈伤痛
    2020-12-22 19:56

    If I was you, I would implement my own solution based on the event specs. Basically, what swipe is - it's handling of touch down, touch move, touch up events. here is excerpt of my own lib for handling iPhone touch events:

    touch_object.prototype.handle_touchstart = function(e){
        if (e.targetTouches.length != 1){
            return false;
        }
        this.obj.style.zIndex = 100;
        e.preventDefault();
        this.startX = e.targetTouches[0].pageX - this.geometry.x;
        this.startY = e.targetTouches[0].pageY - this.geometry.y;
        /* adjust for left /top */
        this.bind_handler('touchmove');
        this.bind_handler('touchend');
    }
    touch_object.prototype.handle_touchmove = function(e) {
        e.preventDefault();
        if (e.targetTouches.length != 1){
            return false;
        }
        var x=e.targetTouches[0].pageX - this.startX;
        var y=e.targetTouches[0].pageY - this.startY;
        this.move(x,y);
    
    }
    touch_object.prototype.handle_touchend = function(e){
        this.obj.style.zIndex = 10;
        this.unbind_handler('touchmove');
        this.unbind_handler('touchend');
    }
    

    I used that code to "move things around". But, instead of moving, you can create your own algorithm for e.g. triggering redirect to some other location, or you can use that move to "move/swipe" the element, on which the swipe is on to other location.

    so, it really helps to understand basics of how things work and then create more complicated solutions. this might help as well.

    I used this, to create my solution:

    http://developer.apple.com/library/IOs/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html

提交回复
热议问题