uni-list.vue 2.7 KB

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