index.js 2.1 KB

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