How to make Bootstrap carousel slider use mobile left/right swipe

前端 未结 10 1028
走了就别回头了
走了就别回头了 2020-11-29 15:49

DEMO JSSFIDDLE

  
10条回答
  •  攒了一身酷
    2020-11-29 16:27

    Checked solution is not accurate, sometimes mouse-right-click triggers right-swipe. after trying different plugins for swipe i found an almost perfect one.

    touchSwipe

    i said "almost" because this plugin does not support future elements. so we would have to reinitialize the swipe call when the swipe content is changed by ajax or something. this plugin have lots of options to play with touch events like multi-finger-touch,pinch etc.

    http://labs.rampinteractive.co.uk/touchSwipe/demos/index.html

    solution 1 :

    $("#myCarousel").swipe( {
                swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
    
                    if(direction=='left'){
                        $(this).carousel('next');
                    }else if(direction=='right'){
                        $(this).carousel('prev');
                    }
    
                }
            });
    

    solution 2:(for future element case)

    function addSwipeTo(selector){
                $(selector).swipe("destroy");
                $(selector).swipe( {
                    swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
                        if(direction=='left'){
                            $(this).carousel('next');
                        }else if(direction=='right'){
                            $(this).carousel('prev');
                        }
                    }
                });
    }
    addSwipeTo("#myCarousel");
    

提交回复
热议问题