Repository navigation
Expand file tree
/
Copy pathjquery.cssPageTransitions.js
More file actions
269 lines (223 loc) · 9.39 KB
/
Copy pathjquery.cssPageTransitions.js
File metadata and controls
269 lines (223 loc) · 9.39 KB
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
//jQuery plugin to simplify implementation of full page transitions
//2015 — Erik Friberg
(function($) {
'use strict';
$.cssPageTransitions = function(element, options) {
var plugin = this;
plugin.element = $(element);
plugin.settings = {};
plugin.pushed = false;
plugin.wrapper = null;
plugin.newElement = null;
plugin.elementsOut = null;
//setup defaults
var defaults = {
urlAttr: 'href',
onClicked: function() {},
onLoaded: function() {},
elementsOut: 'article',
elementsIn: 'article',
classOut: '.is-moveout',
classIn: '.is-movein',
alignWithPrevious: true,
scrollDisable: true,
updateUrl: true,
animationEnded: function() {},
onErrorLoading: null
};
/****** PUBLIC FUNCTIONS ******/
//check if the url is local
plugin.localUrl = function(url) {
if (url.indexOf(document.domain) > -1 || url.indexOf(':') === -1) {
return true;
}
return false;
};
//Add new URL address to history
plugin.bindNewLocalUrl = function(url) {
//handle window location field
if(url !== window.location) {
//add the new page to the window.history
window.history.pushState('{pushed: true}', null, url);
plugin.pushed = true;
return true;
}
return false;
};
//bind event to make back button update after pushState event
plugin.bindBackButtonUrl = function() {
$(window).on("popstate", function(e) {
if(plugin.pushed || e.originalEvent.state !== null) {
window.location.reload();
}else{
window.history.replaceState('{pushed: true}', null, window.location);
}
});
return false;
};
//replaces click actions on both mobile and desktop with customFunction
plugin.bindTouchClicks = function(elem,callbackFunction) {
var e = 'ontouchstart' in $(window) ? 'touchstart':'click';
//bind callback event so that this is preserved
$(elem).on(e, function(e){callbackFunction.apply(elem,[e]);});
return false;
};
//executes customFunction on animationEnd and transitonEnd
plugin.bindAnimationTranstionEnd = function(elem, callbackFunction) {
var animationEndEvent = 'webkitAnimationEnd oanimationend msAnimationEnd animationend webkitTransitionEnd otransitonend msTransitionEnd transitionend';
$(elem).one(animationEndEvent, function(e) {
//prevents animationEndEvent from firing twice a bit backwards but works
$(this).off(animationEndEvent);
//bind callback event so that this is preserved
callbackFunction.apply(elem,[e]);
});
return false;
};
//toggle scroll
plugin.togglePreventWindowScroll = function(bind) {
var scrollEvent = 'mousewheel DOMMouseScroll touchmove scroll';
if(bind) {
$(window).on(scrollEvent, function(ev) {
ev.preventDefault();
});
return true;
}else{
$(window).off(scrollEvent);
}
return false;
};
/****** PRIVATE FUNCTIONS ******/
//Start fading animation
var startAnimationOut = function() {
//add classes
plugin.elementsOut.addClass(plugin.settings.classOut);
//set the new element to align at the top of the previous content
if(plugin.settings.alignWithPrevious) {
plugin.wrapper.css({'overflow':'hidden', 'height':plugin.elementsOut.height()});
}
};
//Start fade in animation
var startAnimationIn = function() {
//set the new element to align at the top of the previous content
if(plugin.settings.alignWithPrevious) {
var currentScroll = $(document).scrollTop();
var offset = plugin.wrapper.offset();
plugin.newElement.css({ "top": (currentScroll-offset.top)+"px"});
}
};
//Functions after transition ends
var registerTransitionAnimationEnd = function(e) {
//Toggle scroll
if(plugin.settings.scrollDisable) {
plugin.togglePreventWindowScroll(false);
}
//call custom function
plugin.settings.animationEnded.call();
//Scroll to top of new content
if(plugin.settings.alignWithPrevious) {
plugin.newElement.css({ "top": "0px"});
var offset = plugin.wrapper.offset();
$(document).scrollTop(offset.top);
plugin.wrapper.css({'overflow':'visible', 'height':'auto'});
}
//remove classes
plugin.element.removeClass(plugin.settings.classOut);
plugin.newElement.removeClass(plugin.settings.classIn);
return false;
};
//Register classes and handle logic
var registerCssPageTransitions = function(data, response, status, xhr) {
//on error
if ( status == "error" ) {
//Call custom function
if($.isFunction(plugin.settings.onErrorLoading)){
plugin.settings.onErrorLoading.call();
}else{
//if no custom error function was defined, send the user to the page without effects
window.location.href = plugin.element.attr(plugin.settings.urlAttr);
}
return false;
}
//insert loaded element into page
plugin.newElement = data.children().addClass(plugin.settings.classIn).insertAfter(plugin.elementsOut);
//start animating in
startAnimationIn();
//Call custom function
plugin.settings.onLoaded.call();
//bind new url
if(plugin.settings.updateUrl === true){
plugin.bindNewLocalUrl(plugin.element.attr(plugin.settings.urlAttr));
//set the document title to match the link
var title = plugin.element.attr('title');
if( typeof(title) !== 'undefined'){
document.title = title;
}
}
//handle animationEnds
plugin.bindAnimationTranstionEnd(plugin.newElement, registerTransitionAnimationEnd);
return false;
};
//Retrive from url
var registerLoadUrl = function(e) {
var url = plugin.element.unbind(e)
.attr(plugin.settings.urlAttr);
//check if the link is local or not
if(!plugin.localUrl(url)) {
return false;
}
//prevent default link action
e.preventDefault();
//Call custom function
plugin.settings.onClicked.call();
//start animating out
startAnimationOut();
//bind scrollevent
if(plugin.settings.scrollDisable) {
plugin.togglePreventWindowScroll(true);
}
//load the next page
var data = $('<div>').load( url +' '+plugin.settings.elementsIn, function(response, status, xhr) {
registerCssPageTransitions.apply(plugin.element,[data,response,status,xhr]);
});
return false;
};
//inits the plugin object
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
plugin.elementsOut = $(plugin.settings.elementsOut);
//add wrapper
if(plugin.settings.alignWithPrevious) {
plugin.wrapper = $('#js-CssPageTransitionsWrapper');
//create new if wrapper doesn’t exist
if (!plugin.wrapper.length) {
var wrapper = $('<div />').attr('id','js-CssPageTransitionsWrapper')
.css({'position': 'relative', 'width': '100%'});
plugin.elementsOut.wrapAll(wrapper);
plugin.wrapper = $('#js-CssPageTransitionsWrapper');
}
}
//store the initial page
if('state' in window.history && window.history.state !== null) {
window.history.replaceState({path: document.location.href}, document.title, document.location.href);
}
//bind back button for corrected behaviour
if(plugin.settings.updateUrl === true){
plugin.bindBackButtonUrl();
}
//Trigger the plugin on click
plugin.bindTouchClicks(element, registerLoadUrl);
return false;
};
//Fire up the plugin
plugin.init();
};
// add the plugin to the jQuery.fn object
$.fn.cssPageTransitions = function(options) {
return this.each(function() {
if (undefined === $(this).data('cssPageTransitions')) {
var plugin = new $.cssPageTransitions(this, options);
$(this).data('cssPageTransitions', plugin);
}
});
};
})(jQuery);