index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. getApp().globalData.Language.getLanguagePackage(this)
  21. },
  22. ready: function () {
  23. const that = this;
  24. let query = wx.createSelectorQuery().in(this)
  25. query.select('.switch-box').boundingClientRect();
  26. query.exec(function (res) {
  27. that.setData({
  28. baseLeft: res[0].left
  29. })
  30. that.chengeSelect()
  31. })
  32. }
  33. },
  34. data: {
  35. checkedItem: 0, //选中项
  36. animationData: {}, //横线平移动画
  37. baseLeft: 0,
  38. },
  39. methods: {
  40. changeSwitch(e) {
  41. this.setData({
  42. checkedItem: this.data.checkedItem == 0 ? 1 : 0
  43. })
  44. let name = this.data.list[this.data.checkedItem],
  45. sort = this.data.sort;
  46. for (let i = 0; i < sort.length; i++) {
  47. sort[i].sorted = sort[i].sortname == name ? 1 : 0;
  48. sort[i].reversed = this.data.reversed
  49. }
  50. this.triggerEvent('change', sort)
  51. this.chengeSelect()
  52. },
  53. chengeSelect() {
  54. let animation = wx.createAnimation({
  55. duration: 1000,
  56. timingFunction: 'ease',
  57. })
  58. let that = this;
  59. let query = wx.createSelectorQuery().in(this)
  60. query.select('.active').boundingClientRect();
  61. query.exec(function (res) {
  62. animation.translate(res[0].left - that.data.baseLeft + 2).step({
  63. duration: 300
  64. })
  65. that.setData({
  66. animationData: animation.export()
  67. })
  68. })
  69. }
  70. }
  71. })