Search code examples
javascriptjqueryhtmlcssswipe-gesture

Swiperight and swipeleft gestures for slideshow from w3school


I have a problem with jquery swiperight and swipeleft gestures.

I have two slideshows, first one is Bootstrap Carousel and second one is inspiration from w3school.

(codepen)

I found good solution for Bootstrap somewhere on this page. Here is the code. It works perfect.

$(document).ready(function() {
    $(".carousel").swiperight(function() {
        $(this).carousel('prev');
    });
    $(".carousel").swipeleft(function() {
        $(this).carousel('next');
    });
}); 

but if I want this code include and modificate to code from w3school it won't work. So I tried something like this (It won't work in codepen I don't know why..)

$(function(){
            $( ".news-slide-content-img" ).on( "swipeleft", swipeleftHandler );
            $( ".news-slide-content-img" ).on( "swiperight", swiperightHandler );
            function swipeleftHandler( ){
                plusSlides(1).css('display', 'block');
            }
            function swiperightHandler( ){
                plusSlides(-1).css('display', 'none');
            }
        });

If I swipe it it swipe more images than one.

Any ideas how to solve this gesture problem?

Here is codepen


Solution

  • It is just a matter of jquery version. Using the documentation of the function swipeleft and swiperight, these versions of jquery solve the issue:

    src="//code.jquery.com/jquery-3.2.1.min.js"></script>
      <script src="//code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.js"></script>
    

    However, you also have a typo in the function swipeleftHandler and swipeleftHandler; you can consider these changes:

    function swipeleftHandler( ){
        plusSlides(1);
      }
      function swiperightHandler( ){
        plusSlides(-1);
      }
    

    You can have a look at this working snippet : https://codepen.io/edkeveked/pen/ypyJJX