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

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

DEMO JSSFIDDLE

  
10条回答
  •  醉酒成梦
    2020-11-29 16:27

    I'm a bit late to the party, but here's a bit of jQuery I've been using:

    $(".carousel").on("touchstart", function(event){
            var xClick = event.originalEvent.touches[0].pageX;
        $(this).one("touchmove", function(event){
            var xMove = event.originalEvent.touches[0].pageX;
            if( Math.floor(xClick - xMove) > 5 ){
                $(this).carousel('next');
            }
            else if( Math.floor(xClick - xMove) < -5 ){
                $(this).carousel('prev');
            }
        });
        $(".carousel").on("touchend", function(){
                $(this).off("touchmove");
        });
    });
    

    No need for jQuery mobile or any other plugins. If you need to adjust the sensitivity of the swipe adjust the 5 and -5. Hope this helps someone.

提交回复
热议问题