animsition.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*!
  2. * animsition v4.0.2
  3. * A simple and easy jQuery plugin for CSS animated page transitions.
  4. * http://blivesta.github.io/animsition
  5. * License : MIT
  6. * Author : blivesta (http://blivesta.com/)
  7. */
  8. ;(function (factory) {
  9. 'use strict';
  10. if (typeof define === 'function' && define.amd) {
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. module.exports = factory(require('jquery'));
  14. } else {
  15. factory(jQuery);
  16. }
  17. }(function ($) {
  18. 'use strict';
  19. var namespace = 'animsition';
  20. var loaded = false;
  21. $(window).on('load', function() {
  22. loaded = true;
  23. });
  24. var __ = {
  25. init: function(options){
  26. options = $.extend({
  27. inClass : 'fade-in',
  28. outClass : 'fade-out',
  29. inDuration : 1500,
  30. outDuration : 800,
  31. linkElement : '.animsition-link',
  32. // e.g. linkElement : 'a:not([target="_blank"]):not([href^="#"])'
  33. loading : true,
  34. loadingParentElement : 'body', //animsition wrapper element
  35. loadingClass : 'animsition-loading',
  36. loadingInner : '', // e.g '<img src="loading.svg" />'
  37. timeout : false,
  38. timeoutCountdown : 5000,
  39. onLoadEvent : true,
  40. browser : [ 'animation-duration', '-webkit-animation-duration'],
  41. // "browser" option allows you to disable the "animsition" in case the css property in the array is not supported by your browser.
  42. // The default setting is to disable the "animsition" in a browser that does not support "animation-duration".
  43. overlay : false,
  44. overlayClass : 'animsition-overlay-slide',
  45. overlayParentElement : 'body',
  46. transition : function(url){ window.location.href = url; }
  47. }, options);
  48. __.settings = {
  49. timer: false,
  50. data: {
  51. inClass: 'animsition-in-class',
  52. inDuration: 'animsition-in-duration',
  53. outClass: 'animsition-out-class',
  54. outDuration: 'animsition-out-duration',
  55. overlay: 'animsition-overlay'
  56. },
  57. events: {
  58. inStart: 'animsition.inStart',
  59. inEnd: 'animsition.inEnd',
  60. outStart: 'animsition.outStart',
  61. outEnd: 'animsition.outEnd'
  62. }
  63. };
  64. // Remove the "Animsition" in a browser
  65. // that does not support the "animaition-duration".
  66. var support = __.supportCheck.call(this, options);
  67. if(!support && options.browser.length > 0){
  68. if(!support || !this.length){
  69. // If do not have a console object to object window
  70. if (!('console' in window)) {
  71. window.console = {};
  72. window.console.log = function(str){ return str; };
  73. }
  74. if(!this.length) console.log('Animsition: Element does not exist on page.');
  75. if(!support) console.log('Animsition: Does not support this browser.');
  76. return __.destroy.call(this);
  77. }
  78. }
  79. var overlayMode = __.optionCheck.call(this, options);
  80. if (overlayMode && $('.' + options.overlayClass).length <= 0) {
  81. __.addOverlay.call(this, options);
  82. }
  83. if (options.loading && $('.' + options.loadingClass).length <= 0) {
  84. __.addLoading.call(this, options);
  85. }
  86. return this.each(function(){
  87. var _this = this;
  88. var $this = $(this);
  89. var $window = $(window);
  90. var $document = $(document);
  91. var data = $this.data(namespace);
  92. if (!data) {
  93. options = $.extend({}, options);
  94. $this.data(namespace, { options: options });
  95. if(options.timeout) __.addTimer.call(_this);
  96. if(options.onLoadEvent) {
  97. if(loaded) {
  98. if(__.settings.timer) clearTimeout(__.settings.timer);
  99. __.in.call(_this);
  100. } else {
  101. $window.on('load', function(e) {
  102. if(__.settings.timer) clearTimeout(__.settings.timer);
  103. __.in.call(_this);
  104. });
  105. }
  106. }
  107. $window.on('pageshow.' + namespace, function(event) {
  108. if(event.originalEvent.persisted) __.in.call(_this);
  109. });
  110. // Firefox back button issue #4
  111. $window.on('unload.' + namespace, function() { });
  112. $document.on('click.' + namespace, options.linkElement, function(event) {
  113. event.preventDefault();
  114. var $self = $(this);
  115. var url = $self.attr('href');
  116. // middle mouse button issue #24
  117. // if(middle mouse button || command key || shift key || win control key)
  118. if (event.which === 2 || event.metaKey || event.shiftKey || navigator.platform.toUpperCase().indexOf('WIN') !== -1 && event.ctrlKey) {
  119. window.open(url, '_blank');
  120. } else {
  121. __.out.call(_this, $self, url);
  122. }
  123. });
  124. }
  125. }); // end each
  126. },
  127. addOverlay: function(options){
  128. $(options.overlayParentElement)
  129. .prepend('<div class="' + options.overlayClass + '"></div>');
  130. },
  131. addLoading: function(options){
  132. $(options.loadingParentElement)
  133. .append('<div class="' + options.loadingClass + '">' + options.loadingInner + '</div>');
  134. },
  135. removeLoading: function(){
  136. var $this = $(this);
  137. var options = $this.data(namespace).options;
  138. var $loading = $(options.loadingParentElement).children('.' + options.loadingClass);
  139. $loading.fadeOut().remove();
  140. },
  141. addTimer: function(){
  142. var _this = this;
  143. var $this = $(this);
  144. var options = $this.data(namespace).options;
  145. __.settings.timer = setTimeout(function(){
  146. __.in.call(_this);
  147. $(window).off('load.' + namespace);
  148. }, options.timeoutCountdown);
  149. },
  150. supportCheck: function(options){
  151. var $this = $(this);
  152. var props = options.browser;
  153. var propsNum = props.length;
  154. var support = false;
  155. if (propsNum === 0) {
  156. support = true;
  157. }
  158. for (var i = 0; i < propsNum; i++) {
  159. if (typeof $this.css(props[i]) === 'string') {
  160. support = true;
  161. break;
  162. }
  163. }
  164. return support;
  165. },
  166. optionCheck: function(options){
  167. var $this = $(this);
  168. var overlayMode;
  169. if(options.overlay || $this.data(__.settings.data.overlay)){
  170. overlayMode = true;
  171. } else {
  172. overlayMode = false;
  173. }
  174. return overlayMode;
  175. },
  176. animationCheck : function(data, stateClass, stateIn){
  177. var $this = $(this);
  178. var options = $this.data(namespace).options;
  179. var dataType = typeof data;
  180. var dataDuration = !stateClass && dataType === 'number';
  181. var dataClass = stateClass && dataType === 'string' && data.length > 0;
  182. if(dataDuration || dataClass){
  183. data = data;
  184. } else if(stateClass && stateIn) {
  185. data = options.inClass;
  186. } else if(!stateClass && stateIn) {
  187. data = options.inDuration;
  188. } else if(stateClass && !stateIn) {
  189. data = options.outClass;
  190. } else if(!stateClass && !stateIn) {
  191. data = options.outDuration;
  192. }
  193. return data;
  194. },
  195. in: function(){
  196. var _this = this;
  197. var $this = $(this);
  198. var options = $this.data(namespace).options;
  199. var thisInDuration = $this.data(__.settings.data.inDuration);
  200. var thisInClass = $this.data(__.settings.data.inClass);
  201. var inDuration = __.animationCheck.call(_this, thisInDuration, false, true);
  202. var inClass = __.animationCheck.call(_this, thisInClass, true, true);
  203. var overlayMode = __.optionCheck.call(_this, options);
  204. var outClass = $this.data(namespace).outClass;
  205. if(options.loading) __.removeLoading.call(_this);
  206. if(outClass) $this.removeClass(outClass);
  207. if(overlayMode) {
  208. __.inOverlay.call(_this, inClass, inDuration);
  209. } else {
  210. __.inDefault.call(_this, inClass, inDuration);
  211. }
  212. },
  213. inDefault: function(inClass, inDuration){
  214. var $this = $(this);
  215. $this
  216. .css({ 'animation-duration' : inDuration + 'ms' })
  217. .addClass(inClass)
  218. .trigger(__.settings.events.inStart)
  219. .animateCallback(function(){
  220. $this
  221. .removeClass(inClass)
  222. .css({ 'opacity' : 1 })
  223. .trigger(__.settings.events.inEnd);
  224. });
  225. },
  226. inOverlay: function(inClass, inDuration){
  227. var $this = $(this);
  228. var options = $this.data(namespace).options;
  229. $this
  230. .css({ 'opacity' : 1 })
  231. .trigger(__.settings.events.inStart);
  232. $(options.overlayParentElement)
  233. .children('.' + options.overlayClass)
  234. .css({ 'animation-duration' : inDuration + 'ms' })
  235. .addClass(inClass)
  236. .animateCallback(function(){
  237. $this
  238. .trigger(__.settings.events.inEnd);
  239. });
  240. },
  241. out: function($self, url){
  242. var _this = this;
  243. var $this = $(this);
  244. var options = $this.data(namespace).options;
  245. var selfOutClass = $self.data(__.settings.data.outClass);
  246. var thisOutClass = $this.data(__.settings.data.outClass);
  247. var selfOutDuration = $self.data(__.settings.data.outDuration);
  248. var thisOutDuration = $this.data(__.settings.data.outDuration);
  249. var isOutClass = selfOutClass ? selfOutClass : thisOutClass;
  250. var isOutDuration = selfOutDuration ? selfOutDuration : thisOutDuration;
  251. var outClass = __.animationCheck.call(_this, isOutClass, true, false);
  252. var outDuration = __.animationCheck.call(_this, isOutDuration, false, false);
  253. var overlayMode = __.optionCheck.call(_this, options);
  254. $this.data(namespace).outClass = outClass;
  255. if(overlayMode) {
  256. __.outOverlay.call(_this, outClass, outDuration, url);
  257. } else {
  258. __.outDefault.call(_this, outClass, outDuration, url);
  259. }
  260. },
  261. outDefault: function(outClass, outDuration, url){
  262. var $this = $(this);
  263. var options = $this.data(namespace).options;
  264. // (outDuration + 1) | #55 outDuration: 0 crashes on Safari only
  265. $this
  266. .css({ 'animation-duration' : (outDuration + 1) + 'ms' })
  267. .addClass(outClass)
  268. .trigger(__.settings.events.outStart)
  269. .animateCallback(function(){
  270. $this.trigger(__.settings.events.outEnd);
  271. options.transition(url);
  272. });
  273. },
  274. outOverlay: function(outClass, outDuration, url){
  275. var _this = this;
  276. var $this = $(this);
  277. var options = $this.data(namespace).options;
  278. var thisInClass = $this.data(__.settings.data.inClass);
  279. var inClass = __.animationCheck.call(_this, thisInClass, true, true);
  280. // (outDuration + 1) | #55 outDuration: 0 crashes animsition on Safari only
  281. $(options.overlayParentElement)
  282. .children('.' + options.overlayClass)
  283. .css({ 'animation-duration' : (outDuration + 1) + 'ms' })
  284. .removeClass(inClass)
  285. .addClass(outClass)
  286. .trigger(__.settings.events.outStart)
  287. .animateCallback(function(){
  288. $this.trigger(__.settings.events.outEnd);
  289. options.transition(url);
  290. });
  291. },
  292. destroy: function(){
  293. return this.each(function(){
  294. var $this = $(this);
  295. $(window).off('.'+ namespace);
  296. $this
  297. .css({'opacity': 1})
  298. .removeData(namespace);
  299. });
  300. }
  301. };
  302. $.fn.animateCallback = function(callback){
  303. var end = 'animationend webkitAnimationEnd';
  304. return this.each(function() {
  305. var $this = $(this);
  306. $this.on(end, function(){
  307. $this.off(end);
  308. return callback.call(this);
  309. });
  310. });
  311. };
  312. $.fn.animsition = function(method){
  313. if ( __[method] ) {
  314. return __[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  315. } else if ( typeof method === 'object' || ! method ) {
  316. return __.init.apply( this, arguments );
  317. } else {
  318. $.error( 'Method ' + method + ' does not exist on jQuery.'+namespace);
  319. }
  320. };
  321. }));