-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscripts.js
300 lines (254 loc) · 9.88 KB
/
scripts.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
window.jQuery = window.$ = require( "jquery" );
require( "velocity-animate/velocity.js" );
require( "lazysizes" );
require( "lazysizes/plugins/unveilhooks/ls.unveilhooks.js" );
// Jquery & Velocity JS included in GULP
$( document ).ready( function() {
toggleMobileNav();
ShowHideNav();
formCheck();
} );
// Close modal if ESC is pressed
$( document ).keyup( function( e ) {
e.keyCode === 27 ? removeModal() : null;
} );
$( window ).resize( function() {
$( ".header" ).removeClass( "hide-nav" ); // Ensure nav will be shown on resize
$( ".header__toggle" ).removeClass( "--open" );
$( ".header__links" ).removeClass( "js--open" );
$( ".header__links" ).removeAttr( "style" ); // If mobile nav was collapsed, make sure it's show on DESK
$( ".header__overlay" ).remove(); // Remove mobile navigation overlay in case it was opened
} );
/*-------------------------------------------------------------------------*/
/* MOBILE NAVIGATION */
/* -----------------------------------------------------------------------*/
function toggleMobileNav() {
$( ".header__toggle" ).click( function() {
if ( !$( ".header__links" ).is( ".velocity-animating" ) ) {
if ( $( ".header__links" ).hasClass( "js--open" ) ) {
hideMobileNav();
} else {
openMobileNav();
}
}
} );
$( "body" ).on( "click", function( e ) {
if ( e.target.classList.contains( "header__overlay" ) ) {
hideMobileNav();
}
} );
}
function openMobileNav() {
$( ".header__links" ).velocity( "slideDown", {
duration: 300,
easing: "ease-out",
display: "block",
visibility: "visible",
begin: function() {
$( ".header__toggle" ).addClass( "--open" );
$( "body" ).append( "<div class='header__overlay'></div>" );
},
progress: function() {
$( ".header__overlay" ).addClass( "--open" );
},
complete: function() {
$( this ).addClass( "js--open" );
}
} );
}
function hideMobileNav() {
$( ".header__overlay" ).remove();
$( ".header__links" ).velocity( "slideUp", {
duration: 300,
easing: "ease-out",
display: "none",
visibility: "hidden",
begin: function() {
$( ".header__toggle" ).removeClass( "--open" );
},
progress: function() {
$( ".header__overlay" ).removeClass( "--open" );
},
complete: function() {
$( this ).removeClass( "js--open" );
$( ".header__toggle, .header__overlay" ).removeClass( "--open" );
}
} );
}
/*-------------------------------------------------------------------------*/
/* SHOW/SCROLL NAVIGATION */
/* -----------------------------------------------------------------------*/
function ShowHideNav() {
var previousScroll = 0, // previous scroll position
$header = $( ".header" ), // just storing header in a variable
navHeight = $header.outerHeight(), // nav height
detachPoint = 576 + 60, // after scroll past this nav will be hidden
hideShowOffset = 6; // scroll value after which nav will be shown/hidden
$( window ).scroll( function() {
var wW = 1024;
// if window width is more than 1024px start show/hide nav
if ( $( window ).width() >= wW ) {
if ( !$header.hasClass( "fixed" ) ) {
var currentScroll = $( this ).scrollTop(),
scrollDifference = Math.abs( currentScroll - previousScroll );
// if scrolled past nav
if ( currentScroll > navHeight ) {
// if scrolled past detach point -> show nav
if ( currentScroll > detachPoint ) {
if ( !$header.hasClass( "fix-nav" ) ) {
$header.addClass( "fix-nav" );
}
}
if ( scrollDifference >= hideShowOffset ) {
if ( currentScroll > previousScroll ) {
// scroll down -> hide nav
if ( !$header.hasClass( "hide-nav" ) ) {
$header.addClass( "hide-nav" );
}
} else {
// scroll up -> show nav
if ( $header.hasClass( "hide-nav" ) ) {
$( $header ).removeClass( "hide-nav" );
}
}
}
} else {
// at the top
if ( currentScroll <= 0 ) {
$header.removeClass( "hide-nav show-nav" );
$header.addClass( "top" );
}
}
}
// scrolled to the bottom -> show nav
if ( ( window.innerHeight + window.scrollY ) >= document.body.offsetHeight ) {
$header.removeClass( "hide-nav" );
}
previousScroll = currentScroll;
} else {
$header.addClass( "fix-nav" );
}
} );
}
/*-------------------------------------------------------------------------*/
/* HANDLE MODAL */
/* -----------------------------------------------------------------------*/
function openModal() {
$( "body" ).css( "overflow", "hidden" );
$( ".modal, .modal__overlay" ).show().css( "display", "flex" );
$( ".modal__inner" ).velocity( { translateY: 0, opacity: 1 } );
$( ".modal__overlay" ).velocity( { opacity: 1 }, 100 );
}
function removeModal() {
$( "body" ).css( { "overflow": "visible" } );
$( ".modal, .modal__overlay, .modal__inner" ).velocity( { opacity: 0 }, function() {
$( ".modal" ).css( { opacity: 1 } );
$( ".modal__inner" ).css( {
"-webkit-transform": "translateY(200px)",
"-ms-transform": "translateY(200px)",
transform: "translateY(200px)"
} );
$( ".modal, .modal__overlay" ).hide();
$( ".modal__body" ).empty();
} );
}
$( ".js-modal-close" ).click( function() {
removeModal();
} );
$( ".modal__overlay" ).click( function() {
removeModal();
} );
/*-------------------------------------------------------------------------*/
/* FORM VALIDATION */
/* -----------------------------------------------------------------------*/
function formCheck() {
$( ".js-submit" ).click( function( e ) {
e.preventDefault();
var $inputs = $( ".form__input input" );
var textarea = $( ".form__input textarea" );
var isError = false;
$( ".form__input" ).removeClass( "error" );
$( ".error-data" ).remove();
for ( var i = 0; i < $inputs.length; i++ ) {
var input = $inputs[ i ];
if ( $( input ).attr( "required", true ) && !validateRequired( $( input ).val() ) ) {
addErrorData( $( input ), "This field is required" );
isError = true;
}
if ( $( input ).attr( "required", true ) && $( input ).attr( "type" ) === "email" && !validateEmail( $( input ).val() ) ) {
addErrorData( $( input ), "Email address is invalid" );
isError = true;
}
if ( $( textarea ).attr( "required", true ) && !validateRequired( $( textarea ).val() ) ) {
addErrorData( $( textarea ), "This field is required" );
isError = true;
}
}
if ( isError === false ) {
$( "#contactForm" ).submit();
}
} );
}
// Validate if the input is not empty
function validateRequired( value ) {
if ( value === "" ) {
return false;
}
return true;
}
// Validate if the email is using correct format
function validateEmail( value ) {
if ( value !== "" ) {
return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i.test( value );
}
return true;
}
// Add error message to the input
function addErrorData( element, error ) {
element.parent().addClass( "error" );
element.after( "<span class='error-data'>" + error + "</span>" );
}
/*-------------------------------------------------------------------------*/
/* AJAX FORM SUBMIT
/* Formspree now only supports AJAX for Gold Users
/* https://github.com/formspree/formspree/pull/173
/* Uncomment if you want to use AJAX Form submission and you're a gold user
/* -----------------------------------------------------------------------*/
// $( "#contactForm" ).submit( function( e ) {
// e.preventDefault();
// var $btn = $( ".js-submit" ),
// $inputs = $( ".form__input input" ),
// $textarea = $( ".form__input textarea" ),
// $name = $( "input#name" ).val(),
// $url = $( "#contactForm" ).attr( "action" );
// $.ajax( {
// url: $url,
// method: "POST",
// data: $( this ).serialize(),
// dataType: "json",
// beforeSend: function() {
// $btn.prop( "disabled", true );
// $btn.text( "Sending..." );
// },
// // eslint-disable-next-line no-unused-vars
// success: function( data ) {
// $inputs.val( "" );
// $textarea.val( "" );
// $btn.prop( "disabled", false );
// $btn.text( "Send" );
// openModal();
// $( ".modal__body" ).append(
// "<h1>Thanks " +
// $name +
// "!</h1><p>Your message was successfully sent! Will get back to you soon.</p>"
// );
// },
// error: function( err ) {
// $( ".modal, .modal__overlay" ).addClass( "--show" );
// $( ".modal__body" ).append(
// "<h1>Aww snap!</h1><p>Something went wrong, please try again. Error message: </p>" +
// err
// );
// }
// } );
// } );