0

I am using the mostslider.js jquery Plugin. I need to add swipe navigation for the slider in mobile view, sadly mostslider doesn't support swipe navigation. So how can I manually add swipe navigation to this slider..?

P.S: Changing the slider plugin is not an option.

T J
  • 42,762
  • 13
  • 83
  • 138
Krish
  • 1,884
  • 2
  • 16
  • 40
  • sorry #2pha I'm the beginner in Jquery :( – Krish Oct 19 '14 at 05:39
  • @Krish BTW, *"Please don't advice to change the slider"* - Could you please explain why..? There might be other slider plugins which does the exact same thing with swipe support. – T J Oct 26 '14 at 08:44
  • @TJ , bcoz i'm already customize the slider for more match to the design, spend more time tooo. TJ, advice will be appreaciate you for any touch event support this slider. – Krish Oct 26 '14 at 08:50
  • @Krish Ok.. let me see if I can help. BTW, It'd be great if you can find a CDN for this plugin, at the moment I couldn't find any... – T J Oct 26 '14 at 08:54

4 Answers4

2

You can use a touch library for detecting touch events and call the next() and prev() methods of the Mostslider plugin according to the swipe direction.


For example, You can use Hammer.js (3.96KB gzipped) to detect the swipe:

$(document).ready(function(){
    var slider = $("#slider").mostSlider({
        animation: 'slide',
        aniMethod: 'auto',
        autoPlay:false
    });
    var hammertime = new Hammer(slider.get(0)); // Pure hammer.js requires native DOM element
    hammertime.on('swipe', function(ev) {
        if(ev.direction == 4){ // Swipe right
           slider.next();
        }
        else if(ev.direction == 2){
           slider.prev(); // Swipe left
        }
    });
});

$(document).ready(function() {
  var slider = $("#slider").mostSlider({
    animation: 'slide',
    aniMethod: 'auto',
    autoPlay: false
  });
  var hammertime = new Hammer(slider.get(0));
  hammertime.on('swipe', function(ev) {
    if (ev.direction == 2) {
      slider.prev(); // Swipe left
    } else if (ev.direction == 4) { // Swipe right
      slider.next();
    }
  });
});
* {
  margin: 0;
  padding: 0;
}
html,body,.default,#slider {
  height: 100%;
}
#slider img {
  height: 100% !important;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githack.com/julianhandl/mostslider/master/mostslider.min.js"></script>
<script src="http://hammerjs.github.io/dist/hammer.min.js"></script>
<script src="https://raw.githack.com/hammerjs/jquery.hammer.js/master/jquery.hammer.js"></script>
<link href="https://raw.githack.com/julianhandl/mostslider/master/themes/default/default.css" rel="stylesheet" />
<div class="slider-wrapper default">
  <div id="slider">
    <img src="http://www.blackberry-wallpapers.com/uploads/allimg/110511/2-1105112346350-L.jpg" />
    <img src="http://www.blackberry-wallpapers.com/uploads/allimg/110511/2-1105111042530-L.jpg" />
    <img src="http://www.kucuu.com/desktop-wallpaper-home/Landscape-Nature-Lake-Trees-Forest-Wallpaper-640x480.jpg" />
  </div>
</div>

Hammer.js also have a jQuery Plugin, which emits jquery events. If you include it, you can simply do:

$(document).ready(function () {
  var slider = $("#slider").mostSlider({
    animation: 'slide',
    aniMethod: 'auto',
    autoPlay: false
  }).hammer().bind('swipe', function (ev) { // Yay, now we can chain :)
     if (ev.gesture.direction == 2) {
        slider.prev(); // Swipe left
    }
     else if (ev.gesture.direction == 4) { // Swipe right
        slider.next();
    }
  });
});

Note that the in jQuery event object, you've to access the direction like event.gesture.direction.

$(document).ready(function() {
  var slider = $("#slider").mostSlider({
    animation: 'slide',
    aniMethod: 'auto',
    autoPlay: false
  }).hammer().bind('swipe', function(ev) {
    if (ev.gesture.direction == 4) { // Swipe right
      slider.next();
    } else if (ev.gesture.direction == 2) {
      slider.prev(); // Swipe left
    }
  });
});
* {
  margin: 0;
  padding: 0;
}
html,body,.default,#slider {
  height: 100%;
}
#slider img {
  height: 100% !important;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githack.com/julianhandl/mostslider/master/mostslider.min.js"></script>
<script src="http://hammerjs.github.io/dist/hammer.min.js"></script>
<script src="https://raw.githack.com/hammerjs/jquery.hammer.js/master/jquery.hammer.js"></script>
<link href="https://raw.githack.com/julianhandl/mostslider/master/themes/default/default.css" rel="stylesheet" />
<div class="slider-wrapper default">
  <div id="slider">
    <img src="http://www.blackberry-wallpapers.com/uploads/allimg/110511/2-1105112346350-L.jpg" />
    <img src="http://www.blackberry-wallpapers.com/uploads/allimg/110511/2-1105111042530-L.jpg" />
    <img src="http://www.kucuu.com/desktop-wallpaper-home/Landscape-Nature-Lake-Trees-Forest-Wallpaper-640x480.jpg" />
  </div>
</div>

Side note: The values 2 and 4 of ev.direction is "DIRECTION_LEFT" and "DIRECTION_RIGHT" respectively. Those constants are mentioned here

T J
  • 42,762
  • 13
  • 83
  • 138
  • 1
    @Krish I tested this by emulating touch events in chrome. If you can provide a cdn to the plugin I can provide a working demo... – T J Oct 26 '14 at 10:35
  • which tool used for checking in FF? i used webdeveloper > responsive design view (ctrl+shift+M) – Krish Oct 26 '14 at 11:17
  • 1
    @Krish I'm doing the same. In `FF` you click `run` after loading the fiddle then it starts working. It's probably a simulator issue. Maybe if you test in real devices it'll work fine – T J Oct 26 '14 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63662/discussion-between-krish-and-t-j). – Krish Oct 26 '14 at 11:27
1

I use this plugin to add swipe support:

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

You could take advantage of the left & right arrows and do something like this:

  $("#slider").swipe( {
    swipe:function(direction) {
      var dir = direction == "left" ? "right" : "left"; //If you swipe left, you want to trigger the right arrow
      $("#" + dir).trigger('click');  
    }
  });
FuriousD
  • 844
  • 5
  • 16
0

Oh hey. Here's julian (creator of mostslider). Maybe I should subscript to the tag for my slider... Never thought it's used somewhere as there were no feedback messages or anything else.

I'm really sorry for not answering to your question.

By the way: I'm working on v2.0.0 (look for the branch on the github repo). This already supports swipe navigation by default. Maybe you would like to use it in future projects.

All the best, Julian

jurihandl
  • 665
  • 1
  • 9
  • 24
0

I use touchSwipe plugin. I did it the next way:

 $("#slider").swipe( {

        swipe:function(event, direction, distance, duration, fingerCount, fingerData) {   
           if (direction == "right"){
               var dir = "left";
            } else {
                var dir = "right";
            };  
            $("#" + dir).trigger('click');      
        }

});