swiper.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const app = getApp();
  2. Page({
  3. data: {
  4. StatusBar: app.globalData.StatusBar,
  5. CustomBar: app.globalData.CustomBar,
  6. cardCur: 0,
  7. tower: [{
  8. id: 0,
  9. url: 'https://image.weilanwl.com/img/4x3-1.jpg'
  10. }, {
  11. id: 1,
  12. url: 'https://image.weilanwl.com/img/4x3-2.jpg'
  13. }, {
  14. id: 2,
  15. url: 'https://image.weilanwl.com/img/4x3-3.jpg'
  16. }, {
  17. id: 3,
  18. url: 'https://image.weilanwl.com/img/4x3-4.jpg'
  19. }, {
  20. id: 4,
  21. url: 'https://image.weilanwl.com/img/4x3-2.jpg'
  22. }, {
  23. id: 5,
  24. url: 'https://image.weilanwl.com/img/4x3-4.jpg'
  25. }, {
  26. id: 6,
  27. url: 'https://image.weilanwl.com/img/4x3-2.jpg'
  28. }]
  29. },
  30. onLoad() {
  31. this.towerSwiper('tower');
  32. // 初始化towerSwiper 传已有的数组名即可
  33. },
  34. DotStyle(e) {
  35. this.setData({
  36. DotStyle: e.detail.value
  37. })
  38. },
  39. // cardSwiper
  40. cardSwiper(e) {
  41. this.setData({
  42. cardCur: e.detail.current
  43. })
  44. },
  45. // towerSwiper
  46. // 初始化towerSwiper
  47. towerSwiper(name) {
  48. let list = this.data[name];
  49. for (let i = 0; i < list.length; i++) {
  50. list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
  51. list[i].mLeft = i - parseInt(list.length / 2)
  52. }
  53. this.setData({
  54. towerList: list
  55. })
  56. },
  57. // towerSwiper触摸开始
  58. towerStart(e) {
  59. this.setData({
  60. towerStart: e.touches[0].pageX
  61. })
  62. },
  63. // towerSwiper计算方向
  64. towerMove(e) {
  65. this.setData({
  66. direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
  67. })
  68. },
  69. // towerSwiper计算滚动
  70. towerEnd(e) {
  71. let direction = this.data.direction;
  72. let list = this.data.towerList;
  73. if (direction == 'right') {
  74. let mLeft = list[0].mLeft;
  75. let zIndex = list[0].zIndex;
  76. for (let i = 1; i < list.length; i++) {
  77. list[i - 1].mLeft = list[i].mLeft
  78. list[i - 1].zIndex = list[i].zIndex
  79. }
  80. list[list.length - 1].mLeft = mLeft;
  81. list[list.length - 1].zIndex = zIndex;
  82. this.setData({
  83. towerList: list
  84. })
  85. } else {
  86. let mLeft = list[list.length - 1].mLeft;
  87. let zIndex = list[list.length - 1].zIndex;
  88. for (let i = list.length - 1; i > 0; i--) {
  89. list[i].mLeft = list[i - 1].mLeft
  90. list[i].zIndex = list[i - 1].zIndex
  91. }
  92. list[0].mLeft = mLeft;
  93. list[0].zIndex = zIndex;
  94. this.setData({
  95. towerList: list
  96. })
  97. }
  98. },
  99. });