ScrollerY.vue 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div ref="scrollerView" class="app-scroller" :class="[static ? 'app-scroller-static' : '']">
  3. <slot/>
  4. </div>
  5. </template>
  6. <style lang="scss" scoped>
  7. .app-scroller {
  8. position: absolute;
  9. top: 0;
  10. left: 0;
  11. right: 0;
  12. bottom: 0;
  13. overflow-x: hidden;
  14. overflow-y: auto;
  15. -webkit-overflow-scrolling: touch;
  16. }
  17. .app-scroller-static {
  18. position: static;
  19. flex: 1;
  20. }
  21. </style>
  22. <script>
  23. export default {
  24. name: 'ScrollerY',
  25. props: {
  26. static: {
  27. type: Boolean,
  28. default: false
  29. },
  30. },
  31. data() {
  32. return {
  33. scrollY: 0,
  34. scrollDiff: 0,
  35. scrollInfo: {},
  36. }
  37. },
  38. mounted() {
  39. this.$nextTick(() => {
  40. let scrollListener = typeof this.$listeners['on-scroll'] === "function";
  41. let scrollerView = $A(this.$refs.scrollerView);
  42. scrollerView.scroll(() => {
  43. let wInnerH = Math.round(scrollerView.innerHeight());
  44. let wScrollY = scrollerView.scrollTop();
  45. let bScrollH = this.$refs.scrollerView.scrollHeight;
  46. this.scrollY = wScrollY;
  47. if (scrollListener) {
  48. let direction = 'static';
  49. let directionreal = 'static';
  50. if (this.scrollDiff - wScrollY > 50) {
  51. this.scrollDiff = wScrollY;
  52. direction = 'down';
  53. } else if (this.scrollDiff - wScrollY < -100) {
  54. this.scrollDiff = wScrollY;
  55. direction = 'up';
  56. }
  57. if (this.scrollDiff - wScrollY > 1) {
  58. this.scrollDiff = wScrollY;
  59. directionreal = 'down';
  60. } else if (this.scrollDiff - wScrollY < -1) {
  61. this.scrollDiff = wScrollY;
  62. directionreal = 'up';
  63. }
  64. this.$emit('on-scroll', {
  65. scale: wScrollY / (bScrollH - wInnerH), //已滚动比例
  66. scrollY: wScrollY, //滚动的距离
  67. scrollE: bScrollH - wInnerH - wScrollY, //与底部距离
  68. direction: direction, //滚动方向
  69. directionreal: directionreal, //滚动方向(即时)
  70. });
  71. }
  72. });
  73. });
  74. },
  75. activated() {
  76. if (this.scrollY > 0) {
  77. this.$nextTick(() => {
  78. this.scrollTo(this.scrollY);
  79. });
  80. }
  81. },
  82. methods: {
  83. scrollTo(top, animate) {
  84. if (animate === false) {
  85. $A(this.$refs.scrollerView).stop().scrollTop(top);
  86. } else {
  87. $A(this.$refs.scrollerView).stop().animate({"scrollTop": top});
  88. }
  89. },
  90. scrollToBottom(animate) {
  91. this.scrollTo(this.$refs.scrollerView.scrollHeight, animate);
  92. }
  93. }
  94. }
  95. </script>