index.js 1.6 KB

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