tablelayout.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Component({
  2. options: {
  3. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  4. },
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. titles:{
  10. type:Array,
  11. value: ["点菜","评价"],
  12. }
  13. },
  14. /**
  15. * 组件的初始(内部)数据
  16. */
  17. data: {
  18. //标题 swiper-item 所在位置
  19. titleIndex: 0,
  20. //indiator 动画
  21. animation: "",
  22. //屏幕宽度 px
  23. screenWidth:"",
  24. //微信规定的屏幕宽度750 rpx
  25. wxScreenWidth:750,
  26. //指示器滑动范围宽度,单位宽度
  27. indicatorLayoutWidth:100,
  28. //swiper高度
  29. swiperHeight:"",
  30. //滑动状态:滑动到左边(1)、滑动到右边(2)、其他位置(0)
  31. scrollStatus:1,
  32. //swiper当前位置
  33. swiperIndex:0,
  34. },
  35. /**
  36. * 组件的方法列表
  37. */
  38. methods: {
  39. clickTitle(e) {
  40. //点击切换卡片
  41. var that = this;
  42. console.log(e.currentTarget.dataset.index);
  43. that.setData({
  44. swiperIndex: e.currentTarget.dataset.index
  45. });
  46. },
  47. swiperTrans:function (e) {
  48. var that = this;
  49. console.log("scroll-status " + that.data.scrollStatus)
  50. //todo 存在dx绝对值大于屏幕宽度的情况,会导致indicator移动到固定边界外,该情况真机是否也存在?
  51. // swipter位移 中间变量
  52. var dx;
  53. if (e.detail.dx >= 0)
  54. if (that.data.scrollStatus == 2)
  55. dx = that.data.screenWidth * that.data.titleIndex;
  56. else
  57. dx = e.detail.dx + that.data.screenWidth * that.data.titleIndex;
  58. else if (that.data.scrollStatus == 1)
  59. dx = 0
  60. else
  61. dx = e.detail.dx + that.data.screenWidth * that.data.titleIndex;
  62. //indicator与swipter之间移动比例
  63. var scale = (that.data.indicatorLayoutWidth / that.data.wxScreenWidth).toFixed(2);//保留两位小数,否则indicator有误差
  64. //indicator 位移
  65. console.log("dx " + dx)
  66. var ds = dx * scale;
  67. console.log("ds " + ds);
  68. this.transIndicator(ds);
  69. },
  70. /**
  71. * indicator 平移动画
  72. */
  73. transIndicator(x) {
  74. let option = {
  75. duration: 100,
  76. timingFunction: 'linear'
  77. };
  78. this.animation = wx.createAnimation(option);
  79. this.animation.translateX(x).step();
  80. this.setData({
  81. animation: this.animation.export()
  82. })
  83. },
  84. swiperAnimationfinish: function(e) {
  85. // console.log("e.detail.dx " + e.detail.current);//current:当前选中页面 0 ,1, 2
  86. var that = this;
  87. that.setData({
  88. titleIndex: e.detail.current
  89. });
  90. //计算指示器位移状态
  91. if (that.data.titleIndex == (that.data.titles.length-1)) {
  92. // console.log("move to the right")
  93. that.setData({ scrollStatus: 2 });
  94. } else if (that.data.titleIndex == 0) {
  95. // console.log("move to the left")
  96. that.setData({ scrollStatus: 1 });
  97. } else {
  98. that.setData({ scrollStatus: 0 });
  99. }
  100. },
  101. /**
  102. * current改变时,触发change事件
  103. */
  104. swiperChange: function(e){
  105. console.log("change " + e.detail.current)
  106. }
  107. },
  108. lifetimes: {
  109. attached() {
  110. // 初始化数据
  111. var that = this;
  112. that.setData({
  113. screenWidth: wx.getSystemInfoSync().screenWidth ,//px
  114. });
  115. }
  116. },
  117. })