index.js 1.3 KB

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