-
Notifications
You must be signed in to change notification settings - Fork 47
/
jquery.slideandswipe.js
170 lines (145 loc) · 6.06 KB
/
jquery.slideandswipe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Slide and swipe menu (https://github.com/JoanClaret/slide-and-swipe-menu)
*
* @copyright Copyright 2013-2015 Joan claret
* @license MIT
* @author Joan Claret Teruel <dpam23 at gmail dot com>
*
* Licensed under The MIT License (MIT).
* Copyright (c) Joan Claret Teruel <dpam23 at gmail dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the 'Software'), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
;(function($, document, window, undefined) {
'use strict';
var slideAndSwipe =
$.fn.slideAndSwipe = function(options) {
var nav = $(this); // get the element to swipe
var navWidth = -nav.outerWidth();
var transInitial = navWidth;
// get settings
var settings = $.extend({
triggerOnTouchEnd : true,
swipeStatus : swipeStatus,
allowPageScroll : 'vertical',
threshold : 100,
excludedElements : 'label, button, input, select, textarea, .noSwipe',
speed : 250
}, options );
nav.swipe(settings);
/**
* Catch each phase of the swipe.
* move : we drag the navigation
* cancel : open navigation
* end : close navigation
*/
function swipeStatus(event, phase, direction, distance) {
if(phase == 'start') {
if(nav.hasClass('ssm-nav-visible')) {
transInitial = 0;
} else {
transInitial = navWidth;
}
}
var mDistance;
if (phase == 'move' && (direction == 'left')) {
if(transInitial < 0) {
mDistance = transInitial - distance;
} else {
mDistance = -distance;
}
scrollNav(mDistance, 0);
} else if (phase == 'move' && direction == 'right') {
if(transInitial < 0) {
mDistance = transInitial + distance;
} else {
mDistance = distance;
}
scrollNav(mDistance, 0);
} else if (phase == 'cancel' && (direction == 'left') && transInitial === 0) {
scrollNav(0, settings.speed);
} else if (phase == 'end' && (direction == 'left')) {
hideNavigation();
} else if ((phase == 'end' || phase == 'cancel') && (direction == 'right')) {
console.log('end');
}
}
/**
* Browser detect
*/
function isSafari() {
return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
}
function isChrome() {
return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
}
/**
* Manually update the position of the nav on drag
*/
function scrollNav(distance, duration) {
nav.css('transition-duration', (duration / 1000).toFixed(1) + 's');
if(distance >= 0) {
distance = 0;
}
if(distance <= navWidth) {
distance = navWidth;
}
if(isSafari() || isChrome()) {
nav.css('-webkit-transform', 'translate(' + distance + 'px,0)');
}
else{
nav.css('transform', 'translate(' + distance + 'px,0)');
}
if(distance == '0') {
$('.ssm-toggle-nav').addClass('ssm-nav-visible');
$('html').addClass('is-navOpen');
$('.ssm-overlay').fadeIn();
}
}
/**
* Open / close by click on burger icon
*/
var hideNavigation = (function() {
nav.removeClass('ssm-nav-visible');
scrollNav(navWidth, settings.speed);
$('html').removeClass('is-navOpen');
$('.ssm-overlay').fadeOut();
});
var showNavigation = (function() {
nav.addClass('ssm-nav-visible');
scrollNav(0, settings.speed);
});
$('.ssm-toggle-nav').click(function(e) {
if(nav.hasClass('ssm-nav-visible')) {
hideNavigation();
}
else{
showNavigation();
}
e.preventDefault();
});
}
;
})(window.jQuery || window.$, document, window);
/*
* Export as a CommonJS module
*/
if (typeof module !== 'undefined' && module.exports) {
module.exports = slideAndSwipe;
}