index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. },
  15. lifetimes: {
  16. ready: function () {
  17. const that = this;
  18. let query = wx.createSelectorQuery().in(this)
  19. query.select('.switch-box').boundingClientRect();
  20. query.exec(function (res) {
  21. that.setData({
  22. baseLeft: res[0].left
  23. })
  24. that.chengeSelect()
  25. })
  26. }
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. checkedItem: 0, //选中项
  33. animationData: {}, //横线平移动画
  34. baseLeft: 0,
  35. },
  36. /**
  37. * 组件的方法列表
  38. */
  39. methods: {
  40. changeSwitch(e) {
  41. this.setData({
  42. checkedItem: this.data.checkedItem == 0 ? 1 : 0
  43. })
  44. this.triggerEvent('change', this.data.list[this.data.checkedItem])
  45. this.chengeSelect()
  46. },
  47. chengeSelect() {
  48. let animation = wx.createAnimation({
  49. duration: 1000,
  50. timingFunction: 'ease',
  51. })
  52. let that = this;
  53. let query = wx.createSelectorQuery().in(this)
  54. query.select('.active').boundingClientRect();
  55. query.exec(function (res) {
  56. animation.translate(res[0].left - that.data.baseLeft + 2).step({
  57. duration: 300
  58. })
  59. that.setData({
  60. animationData: animation.export()
  61. })
  62. })
  63. }
  64. }
  65. })