jquery.switchable-2.0.min.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. (function($, w) {
  2. function Switchable(container, o) {
  3. if (arguments.length !== 0) {
  4. o = o || {};
  5. this.ID = container;
  6. this.NAVCLS = o.navCls || "js_switch_nav";
  7. this.CONTCLS = o.contentCls || "js_switch_ct";
  8. this.TRIGGERCLS = o.triggerCls || "js_switch_trigger";
  9. this.PREVCLS = o.prevCls || "js_switch_prev";
  10. this.NEXTCLS = o.nextCls || "js_switch_next";
  11. this.PANELCLS = o.panelCls || "js_switch_panel";
  12. this.PREVCLS = o.prevCls || "js_switch_prev";
  13. this.ACTIVEYRIGGERCLS = o.activeTriggerCls || "selected";
  14. this.DISABLEBTNCLS = o.disableBtnCls || "js_switch_off";
  15. this.TRIGGERTYPE = o.triggerType || "click";
  16. this.EFFECT = o.effect || "normal";
  17. this.HASTRIGGERS = (o.hasTriggers !== undefined) ? o.HASTRIGGERS : true;
  18. this.AUTOPLAY = (o.autoPlay !== undefined) ? o.autoPlay : false;
  19. this.CIRCULAR = (o.circular !== undefined) ? o.circular : true;
  20. this.DELAY = o.delay || 200;
  21. this.STEPS = o.steps || 1;
  22. this.VIEWSIZE = o.viewSize || 1;
  23. this.INTERVAL = o.interval || 5000;
  24. this.DURATION = o.duration || 500;
  25. this.EASING = o.easing || "swing";
  26. this._activeIndex = o.activeIndex || 0;
  27. this.CALLFUN = o.func || null;
  28. this._autoPlayTimer;
  29. this._eventTimer;
  30. }
  31. }
  32. Switchable.prototype = {
  33. _struct: function() {
  34. var $cont = $("#" + this.ID),
  35. $panel = $cont.find("." + this.PANELCLS),
  36. $content = $cont.find("." + this.CONTCLS);
  37. switch (this.EFFECT) {
  38. case "scrollx":
  39. var contWidth = $panel.outerWidth(true) * $panel.length + "px";
  40. $content.css({
  41. "position": "absolute",
  42. "width": contWidth
  43. });
  44. $panel.css({
  45. "float": "left"
  46. });
  47. break;
  48. case "scrolly":
  49. $content.css({
  50. "position": "absolute"
  51. });
  52. break;
  53. case "fade":
  54. $panel.css({
  55. "position": "absolute",
  56. "left": "0",
  57. "top": "0",
  58. "z-index": "0",
  59. "opacity": "0"
  60. });
  61. break;
  62. }
  63. (this.AUTOPLAY === true) && this._auto();
  64. },
  65. _switch: function(index) {
  66. switch (this.EFFECT) {
  67. case "scrollx":
  68. this._switchScrollx(index);
  69. break;
  70. case "scrolly":
  71. this._switchScrolly(index);
  72. break;
  73. case "fade":
  74. this._switchFade(index);
  75. break;
  76. default:
  77. this._switchNormal(index);
  78. }
  79. },
  80. _switchNormal: function(index) {
  81. var $cont = $("#" + this.ID),
  82. $panel = $cont.find("." + this.PANELCLS);
  83. this._setActiveTriggerCls(index);
  84. $panel.hide();
  85. $panel.eq(index).show();
  86. this._activeIndex = index;
  87. (this.CALLFUN !== null) && this.CALLFUN.call(this, index);
  88. },
  89. _switchScrolly: function(index) {
  90. var $cont = $("#" + this.ID),
  91. $panel = $cont.find("." + this.PANELCLS),
  92. $content = $cont.find("." + this.CONTCLS),
  93. panelWidth = $panel.outerHeight(true),
  94. moveWidth = -index * this.STEPS * panelWidth;
  95. this._setActiveTriggerCls(index);
  96. $content.stop().animate({
  97. "top": moveWidth
  98. }, this.DURATION, this.EASING);
  99. this._activeIndex = index;
  100. (this.CIRCULAR === false) && this._setDisableBtnCls(index);
  101. (this.CALLFUN !== null) && this.CALLFUN.call(this, index);
  102. },
  103. _switchScrollx: function(index) {
  104. var $cont = $("#" + this.ID),
  105. $panel = $cont.find("." + this.PANELCLS),
  106. $content = $cont.find("." + this.CONTCLS),
  107. panelWidth = $panel.outerWidth(true),
  108. moveWidth = -index * this.STEPS * panelWidth;
  109. this._setActiveTriggerCls(index);
  110. $content.stop().animate({
  111. "left": moveWidth
  112. }, this.DURATION, this.EASING);
  113. this._activeIndex = index;
  114. (this.CIRCULAR === false) && this._setDisableBtnCls(index);
  115. (this.CALLFUN !== null) && this.CALLFUN.call(this, index);
  116. },
  117. _switchFade: function(index) {
  118. var $cont = $("#" + this.ID),
  119. $panel = $cont.find("." + this.PANELCLS);
  120. this._setActiveTriggerCls(index);
  121. $panel.eq(this._activeIndex).stop().animate({
  122. opacity: 0
  123. }, this.DURATION, this.EASING).css("z-index", 0);
  124. $panel.eq(index).stop().animate({
  125. opacity: 1
  126. }, this.DURATION, this.EASING).css("z-index", 1);
  127. this._activeIndex = index;
  128. (this.CALLFUN !== null) && this.CALLFUN.call(this, index);
  129. },
  130. _setActiveTriggerCls: function(index) {
  131. var $cont = $("#" + this.ID),
  132. $trigger = $cont.find("." + this.TRIGGERCLS),
  133. index = index || 0;
  134. $trigger.removeClass(this.ACTIVEYRIGGERCLS);
  135. $trigger.eq(index).addClass(this.ACTIVEYRIGGERCLS);
  136. },
  137. _setDisableBtnCls: function(index) {
  138. var $cont = $("#" + this.ID),
  139. $panel = $cont.find("." + this.PANELCLS),
  140. $prev = $cont.find("." + this.PREVCLS),
  141. $next = $cont.find("." + this.NEXTCLS),
  142. maxIndex = Math.ceil(($panel.length - this.VIEWSIZE) / this.STEPS);
  143. if (index == 0) {
  144. $prev.addClass(this.DISABLEBTNCLS);
  145. if ($panel.length <= this.VIEWSIZE) {
  146. $next.addClass(this.DISABLEBTNCLS);
  147. } else {
  148. $next.removeClass(this.DISABLEBTNCLS);
  149. }
  150. } else if (index == maxIndex) {
  151. $next.addClass(this.DISABLEBTNCLS);
  152. $prev.removeClass(this.DISABLEBTNCLS);
  153. } else {
  154. $prev.removeClass(this.DISABLEBTNCLS);
  155. $next.removeClass(this.DISABLEBTNCLS);
  156. }
  157. },
  158. _auto: function() {
  159. //alert(33);
  160. var self = this,
  161. $cont = $("#" + this.ID);
  162. clearInterval(this._autoPlayTimer);
  163. self._autoPlayTimer = setInterval(function() {
  164. self._doPlay();
  165. }, self.INTERVAL);
  166. $cont.unbind("mouseenter");
  167. $cont.unbind("mouseleave");
  168. $cont.mouseenter(function() {
  169. clearInterval(self._autoPlayTimer);
  170. });
  171. $cont.mouseleave(function() {
  172. self._autoPlayTimer = setInterval(function() {
  173. self._doPlay();
  174. }, self.INTERVAL);
  175. });
  176. },
  177. _doPlay: function() {
  178. this._switch(this._checkActiveIndex(this._activeIndex + 1));
  179. },
  180. _checkActiveIndex: function(index) {
  181. var $cont = $("#" + this.ID),
  182. $panel = $cont.find("." + this.PANELCLS),
  183. maxIndex = Math.ceil(($panel.length - this.VIEWSIZE) / this.STEPS);
  184. if (index < 0) {
  185. return maxIndex;
  186. } else if (index > maxIndex) {
  187. return 0
  188. }
  189. return index;
  190. },
  191. stop: function() {
  192. var $cont = $("#" + this.ID);
  193. $cont.unbind("mouseenter");
  194. $cont.unbind("mouseleave");
  195. clearInterval(this._autoPlayTimer);
  196. },
  197. start: function() {
  198. this._auto();
  199. },
  200. switchTo: function(index) {
  201. this._switch(index);
  202. },
  203. prev: function() {
  204. this._switch(this._checkActiveIndex(this._activeIndex - 1));
  205. },
  206. next: function() {
  207. this._switch(this._checkActiveIndex(this._activeIndex + 1));
  208. },
  209. _bindEvent: function() {
  210. var self = this,
  211. $cont = $("#" + this.ID),
  212. $trigger = $cont.find("." + this.TRIGGERCLS),
  213. $prev = $cont.find("." + this.PREVCLS),
  214. $next = $cont.find("." + this.NEXTCLS);
  215. $trigger.each(function(index, elem) {
  216. $(elem)[self.TRIGGERTYPE](function() {
  217. if (self.TRIGGERTYPE === "click") {
  218. self._switch(index);
  219. } else {
  220. self._eventTimer = setTimeout(function() {
  221. self._switch(index);
  222. }, self.DELAY);
  223. }
  224. });
  225. });
  226. if (self.TRIGGERTYPE === "mouseenter") {
  227. $trigger.bind("mouseleave", function() {
  228. clearTimeout(self._eventTimer);
  229. });
  230. }
  231. $next[self.TRIGGERTYPE](function() {
  232. if ($(this).hasClass(self.DISABLEBTNCLS)) return;
  233. self.next();
  234. });
  235. $prev[self.TRIGGERTYPE](function() {
  236. if ($(this).hasClass(self.DISABLEBTNCLS)) return;
  237. self.prev();
  238. });
  239. },
  240. _init: function(cont, config) {
  241. this._struct();
  242. this._bindEvent();
  243. this._switch(this._activeIndex);
  244. }
  245. };
  246. var Switch = w.Switch = {};
  247. Switch.Tabs = function(container, config) {
  248. Switchable.call(this, container, config);
  249. this._init(container, config);
  250. }
  251. Switch.Tabs.prototype = new Switchable();
  252. Switch.Tabs.prototype.constructor = Switch.Tabs;
  253. Switch.Slide = function(container, config) {
  254. Switchable.call(this, container, config);
  255. this._init(container, config)
  256. }
  257. Switch.Slide.prototype = new Switchable();
  258. Switch.Slide.prototype.constructor = Switch.Slide;
  259. Switch.Carousel = function(container, config) {
  260. Switchable.call(this, container, config);
  261. this._init(container, config)
  262. };
  263. Switch.Carousel.prototype = new Switchable();
  264. Switch.Carousel.prototype.constructor = Switch.Carousel;
  265. Switch.Carousel.prototype._init = function(cont, config) {
  266. this._struct();
  267. (this.CIRCULAR === false) && this._setDisableBtnCls(this._activeIndex);
  268. this._setTrigger(config);
  269. this._bindEvent();
  270. this._switch(this._activeIndex);
  271. };
  272. Switch.Carousel.prototype._setTrigger = function(o) {
  273. var trigger = "",
  274. $cont = $("#" + this.ID),
  275. $trigger = $cont.find("." + this.TRIGGERCLS),
  276. $panel = $cont.find("." + this.PANELCLS),
  277. $nav = $cont.find("." + this.NAVCLS),
  278. maxIndex = Math.ceil(($panel.length - this.VIEWSIZE) / this.STEPS);
  279. for (var i = 1; i <= maxIndex + 1; i++) {
  280. trigger = trigger + '<a class="js_switch_trigger" href="javascript:void(0)"></a>';
  281. }
  282. $nav.html(trigger);
  283. $trigger = $cont.find("." + (o.triggerCls || "js_switch_trigger"));
  284. };
  285. })(jQuery, window);
  286. jQuery.easing['jswing'] = jQuery.easing['swing'];
  287. jQuery.extend(jQuery.easing, {
  288. def: 'easeOutQuad',
  289. swing: function(x, t, b, c, d) {
  290. return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
  291. },
  292. easeOutQuad: function(x, t, b, c, d) {
  293. return -c * (t /= d) * (t - 2) + b;
  294. },
  295. easeOutExpo: function(x, t, b, c, d) {
  296. return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
  297. },
  298. easeInOutExpo: function(x, t, b, c, d) {
  299. if (t == 0) return b;
  300. if (t == d) return b + c;
  301. if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
  302. return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
  303. },
  304. easeOutCirc: function(x, t, b, c, d) {
  305. return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
  306. },
  307. easeInOutCirc: function(x, t, b, c, d) {
  308. if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
  309. return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  310. },
  311. easeOutElastic: function(x, t, b, c, d) {
  312. var s = 1.70158;
  313. var p = 0;
  314. var a = c;
  315. if (t == 0) return b;
  316. if ((t /= d) == 1) return b + c;
  317. if (!p) p = d * .3;
  318. if (a < Math.abs(c)) {
  319. a = c;
  320. var s = p / 4;
  321. } else var s = p / (2 * Math.PI) * Math.asin(c / a);
  322. return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
  323. },
  324. easeOutBack: function(x, t, b, c, d, s) {
  325. if (s == undefined) s = 1.70158;
  326. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  327. }
  328. });