uni-list.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <!-- #ifdef APP-VUE -->
  3. <view class="uni-list">
  4. <slot />
  5. </list>
  6. <!-- #endif -->
  7. <!-- #ifdef APP-NVUE -->
  8. <list ref="list" class="uni-list" :show-scrollbar="true" :enableBackToTop="enableBackToTop" loadmoreoffset="15" :scroll-y="scrollY" @loadmore="loadMore">
  9. <slot />
  10. </list>
  11. <!-- #endif -->
  12. <!-- #ifdef H5 || MP-WEIXIN || MP-QQ -->
  13. <scroll-view class="uni-list" :enableBackToTop="enableBackToTop" :scroll-y="scrollY" @scrolltolower="loadMore">
  14. <slot />
  15. </scroll-view>
  16. <!-- #endif -->
  17. <!-- #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO -->
  18. <scroll-view class="uni-list" :scroll-y="scrollY" @scrolltolower="loadMore">
  19. <slot />
  20. </scroll-view>
  21. <!-- #endif -->
  22. </template>
  23. <script>
  24. export default {
  25. name: 'UniList',
  26. 'mp-weixin': {
  27. options: {
  28. multipleSlots: false
  29. }
  30. },
  31. data() {
  32. return {}
  33. },
  34. props: {
  35. enableBackToTop: {
  36. type: [Boolean, String],
  37. default: false
  38. },
  39. scrollY: {
  40. type: [Boolean, String],
  41. default: true
  42. }
  43. },
  44. created() {
  45. // #ifndef APP-NVUE
  46. this.pullDown = {
  47. threshold: 95,
  48. maxHeight: 200,
  49. callRefresh: 'onrefresh',
  50. callPullingDown: 'onpullingdown',
  51. refreshSelector: '.uni-refresh'
  52. };
  53. this.height = 0;
  54. this.canPullDown = false;
  55. this.refreshInstance = {};
  56. // #endif
  57. },
  58. methods: {
  59. loadMore(e) {
  60. this.$emit("scrolltolower");
  61. },
  62. resetLoadmore() {
  63. this.$refs.list.resetLoadmore();
  64. },
  65. ontouchstart(e) {
  66. if (!this.canPullDown) {
  67. console.log("canPullDown", this.canPullDown);
  68. return
  69. }
  70. this.height = 0;
  71. this.touchStartY = e.touches[0].pageY || e.changedTouches[0].pageY;
  72. this._updateRefresh(0);
  73. this.refreshInstance.callMethod("onchange", true);
  74. },
  75. ontouchmove(e) {
  76. if (!this.canPullDown) {
  77. return
  78. }
  79. var oldHeight = this.height;
  80. var endY = e.touches[0].pageY || e.changedTouches[0].pageY;
  81. var newHeight = endY - this.touchStartY;
  82. if (newHeight > this.pullDown.maxHeight) {
  83. return;
  84. }
  85. this._updateRefresh(newHeight);
  86. newHeight = newHeight < this.pullDown.maxHeight ? newHeight : this.pullDown.maxHeight;
  87. this.height = newHeight;
  88. this.refreshInstance.callMethod(this.pullDown.callPullingDown, {
  89. height: newHeight
  90. });
  91. },
  92. ontouchend(e) {
  93. if (!this.canPullDown) {
  94. return
  95. }
  96. this.refreshInstance.callMethod("onchange", false);
  97. if (this.height > this.pullDown.threshold) {
  98. refreshInstance.callMethod(this.pullDown.callRefresh);
  99. return;
  100. }
  101. this._updateRefresh(0);
  102. },
  103. _updateRefresh(height) {
  104. this.refreshInstance.setStyle({
  105. 'height': height
  106. });
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped>
  112. .uni-list {
  113. flex: 1;
  114. /* #ifndef APP-NVUE */
  115. display: flex;
  116. /* #endif */
  117. position: relative;
  118. flex-direction: column;
  119. }
  120. </style>