nearby.wxs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var func = function (e, instance) {
  2. var dataset = e.instance.getDataset()
  3. var st = e.instance.getState()
  4. var current = st.current || 0
  5. var imgsize = dataset.imgsize
  6. var width = dataset.width
  7. var detail = e.detail
  8. var dx = e.detail.dx
  9. // 相比上次没有任何改变, 直接返回
  10. var diff = typeof st.lastx !== 'undefined' ? (dx - st.lastx) : (dx - 0)
  11. if (diff === 0) return
  12. st.continueCount = st.continueCount || 1
  13. // 连续滑动过程中, 且到达下一个swiper-item的transition开始了, 修正一下current值
  14. if (Math.abs(dx) > width * st.continueCount && st.tmpcurrent != -1) {
  15. console.log('mod is 0 some info', st.tmpcurrent, current)
  16. current = st.tmpcurrent
  17. st.current = st.tmpcurrent
  18. st.tmpcurrent = -1
  19. st.continueCount++
  20. }
  21. // 连续滑动dx的值会比width大,这时候要减去多余的width值
  22. var setToWidth = false
  23. var dir = dx > 0
  24. if (dx !== 0 && Math.abs(dx) >= width) {
  25. setToWidth = true
  26. }
  27. dx = dx - width * parseInt(dx / width)
  28. if (dx === 0 && setToWidth) {
  29. dx = dir ? width : -width
  30. }
  31. // 先判断下方向对不对
  32. if (current >= imgsize.length - 1 && dx > 0) return
  33. if (current <= 0 && dx < 0) return
  34. var currentSize = imgsize[current]
  35. var nextSize = dx > 0 ? imgsize[current + 1] : imgsize[current - 1]
  36. var currentHeight = st.currentHeight || currentSize.height
  37. var diffHeight = Math.abs((nextSize.height - currentSize.height) * (dx / width))
  38. var realheight = currentSize.height + (nextSize.height - currentSize.height > 0 ? diffHeight : -diffHeight)
  39. st.currentHeight = realheight
  40. e.instance.setStyle({
  41. height: realheight + 'px'
  42. })
  43. st.lastdir = dx > 0
  44. console.log('111', realheight, dx, current, nextSize.height, currentSize.height)
  45. }
  46. module.exports = {
  47. func: func,
  48. change: function(e, instance) {
  49. var st = e.instance.getState()
  50. console.log('=====change detail is', e.detail.current)
  51. st.tmpcurrent = e.detail.current
  52. },
  53. animationFinish: function(e) {
  54. var st = e.instance.getState()
  55. if (typeof st.tmpcurrent === 'undefined' || st.tmpcurrent === -1) return
  56. console.log('====animation finish is', st.tmpcurrent)
  57. st.current = st.tmpcurrent
  58. st.tmpcurrent = -1
  59. st.continueCount = 1
  60. }
  61. }