jquery.animsition.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*!
  2. * animsition v3.4.3
  3. * http://blivesta.github.io/animsition/
  4. * Licensed under MIT
  5. * Author : blivesta
  6. * http://blivesta.com/
  7. */
  8. (function($) {
  9. "use strict";
  10. var namespace = "animsition";
  11. var methods = {
  12. init: function(options) {
  13. options = $.extend({
  14. inClass: "fade-in",
  15. outClass: "fade-out",
  16. inDuration: 1500,
  17. outDuration: 800,
  18. linkElement: ".animsition-link",
  19. loading: true,
  20. loadingParentElement: "body",
  21. loadingClass: "animsition-loading",
  22. unSupportCss: [ "animation-duration", "-webkit-animation-duration", "-o-animation-duration" ],
  23. overlay: false,
  24. overlayClass: "animsition-overlay-slide",
  25. overlayParentElement: "body"
  26. }, options);
  27. var support = methods.supportCheck.call(this, options);
  28. if (!support) {
  29. if (!("console" in window)) {
  30. window.console = {};
  31. window.console.log = function(str) {
  32. return str;
  33. };
  34. }
  35. console.log("Animsition does not support this browser.");
  36. return methods.destroy.call(this);
  37. }
  38. var overlayMode = methods.optionCheck.call(this, options);
  39. if (overlayMode) {
  40. methods.addOverlay.call(this, options);
  41. }
  42. if (options.loading) {
  43. methods.addLoading.call(this, options);
  44. }
  45. return this.each(function() {
  46. var _this = this;
  47. var $this = $(this);
  48. var $window = $(window);
  49. var data = $this.data(namespace);
  50. if (!data) {
  51. options = $.extend({}, options);
  52. $this.data(namespace, {
  53. options: options
  54. });
  55. $window.on("load." + namespace + " pageshow." + namespace, function() {
  56. methods.pageIn.call(_this);
  57. });
  58. $window.on("unload." + namespace, function() {});
  59. $(options.linkElement).on("click." + namespace, function(event) {
  60. event.preventDefault();
  61. var $self = $(this);
  62. methods.pageOut.call(_this, $self);
  63. });
  64. }
  65. });
  66. },
  67. addOverlay: function(options) {
  68. $(options.overlayParentElement).prepend('<div class="' + options.overlayClass + '"></div>');
  69. },
  70. addLoading: function(options) {
  71. $(options.loadingParentElement).append('<div class="' + options.loadingClass + '"></div>');
  72. },
  73. removeLoading: function() {
  74. var $this = $(this);
  75. var options = $this.data(namespace).options;
  76. var $loading = $(options.loadingParentElement).children("." + options.loadingClass);
  77. $loading.fadeOut().remove();
  78. },
  79. supportCheck: function(options) {
  80. var $this = $(this);
  81. var props = options.unSupportCss;
  82. var propsNum = props.length;
  83. var support = false;
  84. if (propsNum === 0) {
  85. support = true;
  86. }
  87. for (var i = 0; i < propsNum; i++) {
  88. if (typeof $this.css(props[i]) === "string") {
  89. support = true;
  90. break;
  91. }
  92. }
  93. return support;
  94. },
  95. optionCheck: function(options) {
  96. var $this = $(this);
  97. var overlayMode;
  98. if (options.overlay || $this.data("animsition-overlay")) {
  99. overlayMode = true;
  100. } else {
  101. overlayMode = false;
  102. }
  103. return overlayMode;
  104. },
  105. animationCheck: function(data, stateClass, stateIn) {
  106. var $this = $(this);
  107. var options = $this.data(namespace).options;
  108. var dataType = typeof data;
  109. var dataDuration = !stateClass && dataType === "number";
  110. var dataClass = stateClass && dataType === "string" && data.length > 0;
  111. if (dataDuration || dataClass) {
  112. data = data;
  113. } else if (stateClass && stateIn) {
  114. data = options.inClass;
  115. } else if (!stateClass && stateIn) {
  116. data = options.inDuration;
  117. } else if (stateClass && !stateIn) {
  118. data = options.outClass;
  119. } else if (!stateClass && !stateIn) {
  120. data = options.outDuration;
  121. }
  122. return data;
  123. },
  124. pageIn: function() {
  125. var _this = this;
  126. var $this = $(this);
  127. var options = $this.data(namespace).options;
  128. var thisInDuration = $this.data("animsition-in-duration");
  129. var thisInClass = $this.data("animsition-in");
  130. var inDuration = methods.animationCheck.call(_this, thisInDuration, false, true);
  131. var inClass = methods.animationCheck.call(_this, thisInClass, true, true);
  132. var overlayMode = methods.optionCheck.call(_this, options);
  133. if (options.loading) {
  134. methods.removeLoading.call(_this);
  135. }
  136. if (overlayMode) {
  137. methods.pageInOverlay.call(_this, inClass, inDuration);
  138. } else {
  139. methods.pageInBasic.call(_this, inClass, inDuration);
  140. }
  141. },
  142. pageInBasic: function(inClass, inDuration) {
  143. var $this = $(this);
  144. $this.css({
  145. "animation-duration": inDuration / 1e3 + "s"
  146. }).addClass(inClass).animateCallback(function() {
  147. $this.removeClass(inClass).css({
  148. opacity: 1
  149. });
  150. });
  151. },
  152. pageInOverlay: function(inClass, inDuration) {
  153. var $this = $(this);
  154. var options = $this.data(namespace).options;
  155. $this.css({
  156. opacity: 1
  157. });
  158. $(options.overlayParentElement).children("." + options.overlayClass).css({
  159. "animation-duration": inDuration / 1e3 + "s"
  160. }).addClass(inClass);
  161. },
  162. pageOut: function($self) {
  163. var _this = this;
  164. var $this = $(this);
  165. var options = $this.data(namespace).options;
  166. var selfOutClass = $self.data("animsition-out");
  167. var thisOutClass = $this.data("animsition-out");
  168. var selfOutDuration = $self.data("animsition-out-duration");
  169. var thisOutDuration = $this.data("animsition-out-duration");
  170. var isOutClass = selfOutClass ? selfOutClass : thisOutClass;
  171. var isOutDuration = selfOutDuration ? selfOutDuration : thisOutDuration;
  172. var outClass = methods.animationCheck.call(_this, isOutClass, true, false);
  173. var outDuration = methods.animationCheck.call(_this, isOutDuration, false, false);
  174. var overlayMode = methods.optionCheck.call(_this, options);
  175. var url = $self.attr("href");
  176. if (overlayMode) {
  177. methods.pageOutOverlay.call(_this, outClass, outDuration, url);
  178. } else {
  179. methods.pageOutBasic.call(_this, outClass, outDuration, url);
  180. }
  181. },
  182. pageOutBasic: function(outClass, outDuration, url) {
  183. var $this = $(this);
  184. $this.css({
  185. "animation-duration": outDuration / 1e3 + "s"
  186. }).addClass(outClass).animateCallback(function() {
  187. location.href = url;
  188. });
  189. },
  190. pageOutOverlay: function(outClass, outDuration, url) {
  191. var _this = this;
  192. var $this = $(this);
  193. var options = $this.data(namespace).options;
  194. var thisInClass = $this.data("animsition-in");
  195. var inClass = methods.animationCheck.call(_this, thisInClass, true, true);
  196. $(options.overlayParentElement).children("." + options.overlayClass).css({
  197. "animation-duration": outDuration / 1e3 + "s"
  198. }).removeClass(inClass).addClass(outClass).animateCallback(function() {
  199. $this.css({
  200. opacity: 0
  201. });
  202. location.href = url;
  203. });
  204. },
  205. destroy: function() {
  206. return this.each(function() {
  207. var $this = $(this);
  208. $(window).unbind("." + namespace);
  209. $this.css({
  210. opacity: 1
  211. }).removeData(namespace);
  212. });
  213. }
  214. };
  215. $.fn.animateCallback = function(callback) {
  216. var end = "animationend webkitAnimationEnd mozAnimationEnd oAnimationEnd MSAnimationEnd";
  217. return this.each(function() {
  218. $(this).bind(end, function() {
  219. $(this).unbind(end);
  220. return callback.call(this);
  221. });
  222. });
  223. };
  224. $.fn.animsition = function(method) {
  225. if (methods[method]) {
  226. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  227. } else if (typeof method === "object" || !method) {
  228. return methods.init.apply(this, arguments);
  229. } else {
  230. $.error("Method " + method + " does not exist on jQuery." + namespace);
  231. }
  232. };
  233. })(jQuery);