Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. $(window)
  2. .on('load', function () {
  3. var swipes = $('[data-ride="swipe"]')
  4. swipes.each(function (index, item) {
  5. var swipe = new Swipe(item)
  6. swipe.init()
  7. });
  8. })
  9. /*
  10. @param el :要进行轮转的元素
  11. */
  12. var Swipe = function (element) {
  13. this.$element = $(element)
  14. this.$item = $(element)
  15. .children('.item')
  16. this.$length = $(element)
  17. .children('.item')
  18. .length
  19. this.$itemIndexArr = []
  20. for (var i = 0, len = this.$length; i < len; i++) {
  21. if (i < Math.floor(len / 2))
  22. this.$itemIndexArr[i] = 0
  23. else if (i > Math.floor(len / 2))
  24. this.$itemIndexArr[i] = 4
  25. if (i == Math.floor(len / 2) - 1)
  26. this.$itemIndexArr[i] = 1
  27. if (i == Math.floor(len / 2))
  28. this.$itemIndexArr[i] = 2
  29. if (i == Math.floor(len / 2) + 1)
  30. this.$itemIndexArr[i] = 3
  31. }
  32. }
  33. Swipe.prototype.DEFAULTS = {
  34. interval: 3000,
  35. pause: 'hover'
  36. }
  37. Swipe.prototype.init = function () {
  38.  
  39. var that = this
  40. that.$item.each(function (index, item) {
  41. $(item)
  42. .addClass('p' + that.$itemIndexArr[index])
  43. })
  44. that.cycle()
  45. var links = this.$element.siblings('.slide_action')
  46. .find('a')
  47. links.each(function (index, item) {
  48. var type = $(item)
  49. .attr('class')
  50. .match(/left/) ? 'prev' : 'next'
  51. switch (type) {
  52. case 'prev':
  53. $(this)
  54. .bind('click', function () {
  55. that.prev()
  56. })
  57. break;
  58. case 'next':
  59. $(this)
  60. .bind('click', function () {
  61. that.next()
  62. })
  63. break;
  64. }
  65. });
  66. }
  67. Swipe.prototype.cycle = function () {
  68. var that = this
  69. var cycleInterval = setInterval(function () {
  70. return that.next()
  71. }, that.DEFAULTS.interval)
  72. }
  73. Swipe.prototype.next = function () {
  74. var len = this.$length
  75. var indexArr = this.$itemIndexArr
  76. this.$itemIndexArr = indexArr.map(function (item, index) {
  77. if (item === 4 && indexArr[(index + 1) % len] === 0) {
  78. return 0;
  79. } else if (item === 4) {
  80. return 4;
  81. }
  82. return item + 1;
  83. })
  84. return this.slide();
  85. }
  86. Swipe.prototype.prev = function () {
  87. var len = this.$length
  88. var indexArr = this.$itemIndexArr
  89. this.$itemIndexArr = indexArr.map(function (item, index) {
  90. if (item === 0 && indexArr[(index + len - 1) % len] === 4) {
  91. return 4;
  92. } else if (item === 0) {
  93. return 0;
  94. }
  95. return item - 1;
  96. })
  97. return this.slide();
  98. }
  99. Swipe.prototype.slide = function () {
  100. var indexArr = this.$itemIndexArr
  101. this.$item.removeClass('p0 p1 p2 p3 p4')
  102. this.$item.each(function (index, item) {
  103. $(item)
  104. .addClass('p' + indexArr[index])
  105. })
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement