index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. Component({
  2. properties: {
  3. list: {
  4. type: Array,
  5. value: ['最新', '最热']
  6. },
  7. change: {
  8. type: Function
  9. },
  10. sort: {
  11. type: Array
  12. },
  13. reversed: {
  14. type: Number,
  15. value: 0
  16. }
  17. },
  18. lifetimes: {
  19. attached() {},
  20. ready: function () {
  21. const that = this;
  22. let query = wx.createSelectorQuery().in(this)
  23. query.select('.switch-box').boundingClientRect();
  24. query.exec(function (res) {
  25. that.setData({
  26. baseLeft: res[0].left
  27. })
  28. that.chengeSelect()
  29. })
  30. }
  31. },
  32. data: {
  33. checkedItem: 0, //选中项
  34. animationData: {}, //横线平移动画
  35. baseLeft: 0,
  36. },
  37. methods: {
  38. changeSwitch(e) {
  39. let checkedItem = this.data.checkedItem == 0 ? 1 : 0
  40. this.setData({
  41. checkedItem
  42. })
  43. let name = this.data.list[this.data.checkedItem],
  44. sort = this.data.sort;
  45. for (let i = 0; i < sort.length; i++) {
  46. sort[i].sorted = sort[i].sortname == name ? 1 : 0;
  47. sort[i].reversed = this.data.reversed
  48. }
  49. this.triggerEvent('change', checkedItem)
  50. this.chengeSelect()
  51. },
  52. chengeSelect() {
  53. let animation = wx.createAnimation({
  54. duration: 1000,
  55. timingFunction: 'ease',
  56. })
  57. let that = this;
  58. let query = wx.createSelectorQuery().in(this)
  59. query.select('.active').boundingClientRect();
  60. query.exec(function (res) {
  61. let baseLeft = that.data.baseLeft == res[0].width ? 0 : res[0].width;
  62. animation.translate(baseLeft).step({
  63. duration: 300
  64. })
  65. that.setData({
  66. animationData: animation.export(),
  67. baseLeft
  68. })
  69. })
  70. }
  71. }
  72. })