四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.4 KiB

  1. var max = 200
  2. var refreshHeight = 80
  3. function touchstart(event, ins) {
  4. console.log('touchstart');
  5. var state = ins.getState()
  6. if ( state.refresh ) return
  7. var touch = event.touches[0] || event.changedTouches[0]
  8. state.startX = touch.pageX
  9. state.startY = touch.pageY
  10. }
  11. function touchmove(event, ins) {
  12. console.log('touchmove');
  13. var state = ins.getState()
  14. if ( state.refresh ) return
  15. if ( state.startY > 0) {
  16. var touch = event.touches[0] || event.changedTouches[0]
  17. if ((Math.abs(touch.pageY - state.startY) > Math.abs(touch.pageX - state.startX)) && Math.abs(touch.pageY - state.startY) > 20) {
  18. var pageY = touch.pageY
  19. var rate = max / (max + Math.abs(pageY - state.startY))
  20. state.threshold = rate * (pageY - state.startY)
  21. if ( state.threshold > 0 && !state.pulldownable ) return
  22. if ( state.threshold < 0 && !state.pullupable ) return
  23. if ( state.threshold > max ) {
  24. state.threshold = max
  25. }
  26. if ( state.threshold < -max ) {
  27. state.threshold = -max
  28. }
  29. ins.selectComponent('.yingbing-scroller-wrapper').setStyle({
  30. transform: 'translateY(' + state.threshold + 'px)',
  31. transition: ''
  32. })
  33. if ( state.threshold > 0 ) {
  34. ins.callMethod('pullingdown', state.threshold)
  35. } else {
  36. ins.callMethod('pullingup', Math.abs(state.threshold))
  37. }
  38. }
  39. }
  40. }
  41. function touchend(event, ins) {
  42. touchaction(event, ins)
  43. }
  44. function touchcancel(event, ins) {
  45. touchaction(event, ins)
  46. }
  47. function touchaction (event, ins) {
  48. var state = ins.getState()
  49. if ( state.refresh ) return
  50. if ( state.threshold > 0 && state.pulldownable ) {//下拉
  51. if ( state.threshold > refreshHeight ) {//满足下拉条件
  52. ins.selectComponent('.yingbing-scroller-wrapper').setStyle({
  53. transform: 'translateY(' + refreshHeight + 'px)',
  54. transition: 'transform .1s'
  55. })
  56. ins.callMethod('pulldown')
  57. state.refresh = true
  58. } else {//不满足下拉条件恢复样式
  59. stop(ins)
  60. }
  61. }
  62. if ( state.threshold < 0 && state.pullupable ) {//上拉
  63. if ( Math.abs(state.threshold) > refreshHeight ) {//满足上拉条件
  64. ins.selectComponent('.yingbing-scroller-wrapper').setStyle({
  65. transform: 'translateY(-' + refreshHeight + 'px)',
  66. transition: 'transform .1s'
  67. })
  68. ins.callMethod('pullup')
  69. state.refresh = true
  70. } else {//不满足上拉条件恢复样式
  71. stop(ins)
  72. }
  73. }
  74. }
  75. function stop (ins) {
  76. var state = ins.getState()
  77. ins.selectComponent('.yingbing-scroller-wrapper').setStyle({
  78. transform: 'translateY(0)',
  79. transition: 'transform .1s'
  80. })
  81. state.threshold = 0
  82. state.startY = 0
  83. state.refresh = false
  84. }
  85. function refreshStateWatcher (newVal, oldVal, ins) {
  86. if ( newVal == 'pulldown' ) {
  87. var state = ins.getState()
  88. state.threshold = refreshHeight + 1
  89. state.refresh = false
  90. touchaction(null, ins)
  91. }
  92. if ( newVal == 'pullup' ) {
  93. var state = ins.getState()
  94. state.threshold = -(refreshHeight + 1)
  95. state.refresh = false
  96. touchaction(null, ins)
  97. }
  98. if ( newVal == 'stop' ) stop(ins)
  99. }
  100. function pulldownableWatcher (newVal, oldVal, ins) {
  101. var state = ins.getState()
  102. if ( state ) state.pulldownable = newVal
  103. }
  104. function pullupableWatcher (newVal, oldVal, ins) {
  105. var state = ins.getState()
  106. if ( state ) state.pullupable = newVal
  107. }
  108. module.exports = {
  109. refreshStateWatcher: refreshStateWatcher,
  110. pulldownableWatcher: pulldownableWatcher,
  111. pullupableWatcher: pullupableWatcher,
  112. touchstart: touchstart,
  113. touchmove: touchmove,
  114. touchend: touchend,
  115. touchcancel: touchcancel
  116. }