idealsteps.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*!
  2. * Ideal Steps
  3. */
  4. (function($, win, doc, undefined) {
  5. var plugin = {};
  6. plugin.name = 'idealsteps';
  7. plugin.defaults = {
  8. nav: '.idealsteps-nav',
  9. navItems: 'li',
  10. buildNavItems: true,
  11. wrap: '.idealsteps-wrap',
  12. step: '.idealsteps-step',
  13. activeClass: 'idealsteps-step-active',
  14. before: null,
  15. after: null,
  16. fadeSpeed: 0
  17. };
  18. plugin.methods = {
  19. _init: function() {
  20. var self = this,
  21. active = this.opts.activeClass;
  22. this.$el = $(this.el);
  23. this.$nav = this.$el.find(this.opts.nav);
  24. this.$navItems = this.$nav.find(this.opts.navItems);
  25. this.$wrap = this.$el.find(this.opts.wrap);
  26. this.$steps = this.$wrap.find(this.opts.step);
  27. if (this.opts.buildNavItems) this._buildNavItems();
  28. this.$steps.hide().first().show();
  29. this.$navItems.removeClass(active).first().addClass(active);
  30. this.$navItems.click(function() {
  31. self.go(self.$navItems.index(this));
  32. });
  33. },
  34. _buildNavItems: function() {
  35. var self = this,
  36. isCustom = typeof this.opts.buildNavItems == 'function',
  37. item = function(val){ return '<li><a href="#" tabindex="-1">'+ val +'</a></li>'; },
  38. items;
  39. items = isCustom ?
  40. this.$steps.map(function(i){ return item(self.opts.buildNavItems.call(self, i)) }).get() :
  41. this.$steps.map(function(i){ return item(++i); }).get();
  42. this.$navItems = $(items.join(''));
  43. this.$nav.append($('<ul/>').append(this.$navItems));
  44. },
  45. _getCurIdx: function() {
  46. return this.$steps.index(this.$steps.filter(':visible'));
  47. },
  48. go: function(idx) {
  49. var active = this.opts.activeClass,
  50. fadeSpeed = this.opts.fadeSpeed;
  51. if (typeof idx == 'function') idx = idx.call(this, this._getCurIdx());
  52. if (idx >= this.$steps.length) idx = 0;
  53. if (idx < 0) idx = this.$steps.length-1;
  54. if (this.opts.before) this.opts.before.call(this, idx);
  55. this.$navItems.removeClass(active).eq(idx).addClass(active);
  56. this.$steps.fadeOut(fadeSpeed).eq(idx).fadeIn(fadeSpeed);
  57. if (this.opts.after) this.opts.after.call(this, idx);
  58. },
  59. prev: function() {
  60. this.go(this._getCurIdx() - 1);
  61. },
  62. next: function() {
  63. this.go(this._getCurIdx() + 1);
  64. },
  65. first: function() {
  66. this.go(0);
  67. },
  68. last: function() {
  69. this.go(this.$steps.length-1);
  70. }
  71. };
  72. require('../../plugin')(plugin);
  73. }(jQuery, window, document));