Skip to content

Commit a30f290

Browse files
author
Constantin Saguin
committed
Merge pull request brutaldesign#111 from c0rewell/close_swipe
added ability to close swipebox by vertical swipe
2 parents 796dd7d + ae8480f commit a30f290

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
@@ -9,6 +9,7 @@
99
useCSS : true,
1010
useSVG : true,
1111
initialIndexOnArray : 0,
12+
closeBySwipe: true,
1213
hideBarsDelay : 3000,
1314
videoMaxWidth : 1140,
1415
vimeoColor : 'CCCCCC',
@@ -282,10 +283,15 @@
282283

283284
var $this = this,
284285
distance = null,
286+
vDistance = null,
287+
vSwipe = false,
285288
swipMinDistance = 10,
289+
vSwipMinDistance = 50,
286290
startCoords = {},
287291
endCoords = {};
288-
var bars = $( '#swipebox-caption, #swipebox-action' );
292+
293+
var bars = $('#swipebox-caption, #swipebox-action');
294+
var slider = $('#swipebox-slider');
289295

290296
bars.addClass( 'visible-bars' );
291297
$this.setTimeout();
@@ -296,19 +302,48 @@
296302

297303
endCoords = event.originalEvent.targetTouches[0];
298304
startCoords.pageX = event.originalEvent.targetTouches[0].pageX;
305+
startCoords.pageY = event.originalEvent.targetTouches[0].pageY;
299306

300307
$( '.touching' ).bind( 'touchmove',function( event ) {
301308
event.preventDefault();
302309
event.stopPropagation();
303310
endCoords = event.originalEvent.targetTouches[0];
304311

312+
if (plugin.settings.closeBySwipe) {
313+
vDistance = endCoords.pageY - startCoords.pageY;
314+
if (Math.abs(vDistance) >= vSwipMinDistance || vSwipe) {
315+
var opacity = 0.75 - Math.abs(vDistance) / slider.height();
316+
317+
slider.css({ 'top': vDistance + 'px' });
318+
slider.css({ 'opacity': opacity });
319+
320+
vSwipe = true;
321+
}
322+
}
323+
305324
} );
306325

307326
return false;
308327

309328
} ).bind( 'touchend',function( event ) {
310329
event.preventDefault();
311330
event.stopPropagation();
331+
332+
if (plugin.settings.closeBySwipe) {
333+
if (slider.css("opacity") <= 0.5) {
334+
var vOffset = vDistance > 0 ? slider.height() : -slider.height();
335+
slider.animate({ top: vOffset + 'px', 'opacity': 0 }, 300, function () {
336+
$this.closeSlide();
337+
});
338+
} else {
339+
slider.animate({ top: 0, 'opacity': 1 }, 300);
340+
}
341+
342+
if (vSwipe) {
343+
vSwipe = false;
344+
return;
345+
}
346+
}
312347

313348
distance = endCoords.pageX - startCoords.pageX;
314349

0 commit comments

Comments
 (0)