Skip to content

Commit ae8480f

Browse files
author
c0rewell
committed
added ability to close swipebox by vertical swipe
1 parent 1a78cd9 commit ae8480f

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/js/jquery.swipebox.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
var defaults = {
99
useCSS : true,
1010
initialIndexOnArray : 0,
11+
closeBySwipe: true,
1112
hideBarsDelay : 3000,
1213
videoMaxWidth : 1140,
1314
vimeoColor : 'CCCCCC',
@@ -273,10 +274,15 @@
273274

274275
var $this = this,
275276
distance = null,
277+
vDistance = null,
278+
vSwipe = false,
276279
swipMinDistance = 10,
280+
vSwipMinDistance = 50,
277281
startCoords = {},
278282
endCoords = {};
279-
var bars = $( '#swipebox-caption, #swipebox-action' );
283+
284+
var bars = $('#swipebox-caption, #swipebox-action');
285+
var slider = $('#swipebox-slider');
280286

281287
bars.addClass( 'visible-bars' );
282288
$this.setTimeout();
@@ -287,19 +293,48 @@
287293

288294
endCoords = event.originalEvent.targetTouches[0];
289295
startCoords.pageX = event.originalEvent.targetTouches[0].pageX;
296+
startCoords.pageY = event.originalEvent.targetTouches[0].pageY;
290297

291298
$( '.touching' ).bind( 'touchmove',function( event ) {
292299
event.preventDefault();
293300
event.stopPropagation();
294301
endCoords = event.originalEvent.targetTouches[0];
295302

303+
if (plugin.settings.closeBySwipe) {
304+
vDistance = endCoords.pageY - startCoords.pageY;
305+
if (Math.abs(vDistance) >= vSwipMinDistance || vSwipe) {
306+
var opacity = 0.75 - Math.abs(vDistance) / slider.height();
307+
308+
slider.css({ 'top': vDistance + 'px' });
309+
slider.css({ 'opacity': opacity });
310+
311+
vSwipe = true;
312+
}
313+
}
314+
296315
} );
297316

298317
return false;
299318

300319
} ).bind( 'touchend',function( event ) {
301320
event.preventDefault();
302321
event.stopPropagation();
322+
323+
if (plugin.settings.closeBySwipe) {
324+
if (slider.css("opacity") <= 0.5) {
325+
var vOffset = vDistance > 0 ? slider.height() : -slider.height();
326+
slider.animate({ top: vOffset + 'px', 'opacity': 0 }, 300, function () {
327+
$this.closeSlide();
328+
});
329+
} else {
330+
slider.animate({ top: 0, 'opacity': 1 }, 300);
331+
}
332+
333+
if (vSwipe) {
334+
vSwipe = false;
335+
return;
336+
}
337+
}
303338

304339
distance = endCoords.pageX - startCoords.pageX;
305340

0 commit comments

Comments
 (0)