开发者

how to integrate swipe gestures into a shadowbox slideshow?

I am loving Shadowbox and its quick and easy setup. I would like to add swipe functionality to navigate from one photo to the next.

I am cur开发者_如何转开发rently using jswipe and though I can get the swipe gestures to fire correctly and control the slideshow outside #sb-container, I cannot get the swipe gestures to function anywhere inside the div.

Has anyone successfully integrated swipe navigation to control a shadowbox.js slideshow?

Here is a link to the site I am building...

http://totophoto.mattwooddc.com/portfolio/commercial/

Any suggestions would be greatly appreciated. Thank you!


I was able to get this to work using touchswipe.

First include the touchswipe js file that you can download here: https://github.com/mattbryson/TouchSwipe-Jquery-Plugin

Then include the following code:

$(document).ready(function(){

  $("#sb-body").swipe( {
      //Generic swipe handler for all directions
      swipeLeft:function(event, direction, distance, duration, fingerCount) {
         Shadowbox.next();
      },
      swipeRight:function(event, direction, distance, duration, fingerCount) {
         Shadowbox.previous();
      },
      //Default is 75px, set to 0 for demo so any distance triggers swipe
     threshold:0
  });
});  

i was able to get this to work with jquery-1.3.2 (i know its older but i'm locked into this version b/c of the template my client is using)


I managed to make it work.

First I “borrowed” a touch-event library from jQuery Mobile. Here's the minified code:

(function(e,t){e.each("touchstart touchmove touchend orientationchange tap taphold swipe swipeleft swiperight scrollstart scrollstop".split(" "),function(t,n){e.fn[n]=function(e){return e?this.bind(n,e):this.trigger(n)};e.attrFn[n]=true});var n="ontouchstart"in window,r="touchmove scroll",i=n?"touchstart":"mousedown",s=n?"touchend":"mouseup",o=n?"touchmove":"mousemove";e.event.special.scrollstart={enabled:true,setup:function(){function o(n,r){i=r;var s=n.type;n.type=i?"scrollstart":"scrollstop";e.event.handle.call(t,n);n.type=s}var t=this,n=e(t),i,s;n.bind(r,function(t){if(!e.event.special.scrollstart.enabled){return}if(!i){o(t,true)}clearTimeout(s);s=setTimeout(function(){o(t,false)},50)})}};e.event.special.tap={setup:function(){var t=this,n=e(t);n.bind(i,function(r){function p(e){if(e.type=="scroll"){i=true;return}var t=e.type=="touchmove"?e.originalEvent.touches[0]:e;if(Math.abs(l[0]-t.pageX)>10||Math.abs(l[1]-t.pageY)>10){i=true}}if(r.which&&r.which!==1||n.data("prevEvent")&&n.data("prevEvent")!==r.type){return false}n.data("prevEvent",r.type);setTimeout(function(){n.removeData("prevEvent")},800);var i=false,u=true,a=r.target,f=r.originalEvent,l=r.type=="touchstart"?[f.touches[0].pageX,f.touches[0].pageY]:[r.pageX,r.pageY],c,h;h=setTimeout(function(){if(u&&!i){c=r.type;r.type="taphold";e.event.handle.call(t,r);r.type=c}},750);e(window).one("scroll",p);n.bind(o,p).one(s,function(r){n.unbind(o,p);e(window).unbind("scroll",p);clearTimeout(h);u=false;if(!i&&a==r.target){c=r.type;r.type="tap";e.event.handle.call(t,r);r.type=c}})})}};e.event.special.swipe={setup:function(){var n=this,r=e(n);r.bind(i,function(n){function f(e){if(!u){return}var t=e.originalEvent.touches?e.originalEvent.touches[0]:e;a={time:(new Date).getTime(),coords:[t.pageX,t.pageY]};if(Math.abs(u.coords[0]-a.coords[0])>10){e.preventDefault()}}var i=n.originalEvent.touches?n.originalEvent.touches[0]:n,u={time:(new Date).getTime(),coords:[i.pageX,i.pageY],origin:e(n.target)},a;r.bind(o,f).one(s,function(e){r.unbind(o,f);if(u&&a){if(a.time-u.time<1e3&&Math.abs(u.coords[0]-a.coords[0])>30&&Math.abs(u.coords[1]-a.coords[1])<75){u.origin.trigger("swipe").trigger(u.coords[0]>a.coords[0]?"swipeleft":"swiperight")}}u=a=t})})}};(function(e){function s(){var e=r();if(e!==i){i=e;t.trigger("orientationchange")}}var t=e(window),n,r,i;e.event.special.orientationchange=n={setup:function(){if(e.support.orientation){return false}i=r();t.bind("resize",s)},teardown:function(){if(e.support.orientation){return false}t.unbind("resize",s)},add:function(e){var t=e.handler;e.handler=function(e){e.orientation=r();return t.apply(this,arguments)}}};n.orientation=r=function(){var e=document.documentElement;return e&&e.clientWidth/e.clientHeight<1.1?"portrait":"landscape"}})(jQuery);e.each({scrollstop:"scrollstart",taphold:"tap",swipeleft:"swipe",swiperight:"swipe"},function(t,n){e.event.special[t]={setup:function(){e(this).bind(n,e.noop)}}})})(jQuery)

I included jQuery to take advantage of their selector engine, and some nice helper functions.

Then, I check if the shadowbox is a gallery that would normally display previous and next buttons. If it is, the shadowbox is given the class .sb-touch, and the two event handlers listen after touch-events on that selector.

var myApp = {
    SBAdjust: function() {
        if (Shadowbox.hasNext())
            $('#sb-body').addClass('sb-touch');

        $(document).on('swipeleft', '.sb-touch', function (e) {
            e.preventDefault();
            Shadowbox.next();
        });
        $(document).on('swiperight', '.sb-touch', function (e) {
            e.preventDefault();
            Shadowbox.previous();
        });
    }
}

Of course, you need to initialise all of this somewhere, so edit your Shadowbox.init() property so it looks more like this:

Shadowbox.init({
    onOpen: myApp.SBAdjust,
    onClose: function() {
        $('.sb-touch').removeClass('sb-touch');
    }
});


Jezen Thomas's solution was a great start! However, it didn't quite work for me because a single swipe would trigger multiple swipe events. The slideshow would advance multiple images ahead.

I started with Jezen's code and I ended up using jQuery Swipe. You can see my implementation in action here.

Hope this helps :)


Thank you Jezen Thomas :) Its works! I know it has no 'slide right to left' effect, but anyway it useful. Btw, I added a little function for change images by click/tap right on photo. Its here:

jQuery(function(){
    jQuery(document).on('click', '#sb-body', function(){
        Shadowbox.next();
    });
});

Shadowbox standard controls (arrows) too small, and it impossible to use it on mobile phone. So I hope it helps people who wannt to get everywhere nice working shadowbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜