mescroll-body.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view
  3. class="mescroll-body"
  4. :style="{ minHeight: minHeight, 'padding-top': padTop, 'padding-bottom': padBottom, 'padding-bottom': padBottomConstant, 'padding-bottom': padBottomEnv }"
  5. @touchstart="touchstartEvent"
  6. @touchmove="touchmoveEvent"
  7. @touchend="touchendEvent"
  8. @touchcancel="touchendEvent"
  9. >
  10. <view class="mescroll-body-content mescroll-touch" :style="{ transform: translateY, transition: transition }">
  11. <!-- 下拉加载区域 (支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-down组件实现)-->
  12. <!-- <mescroll-down :option="mescroll.optDown" :type="downLoadType" :rate="downRate"></mescroll-down> -->
  13. <view v-if="mescroll.optDown.use" class="mescroll-downwarp">
  14. <view class="downwarp-content">
  15. <view class="downwarp-progress" :class="{ 'mescroll-rotate': isDownLoading }" :style="{ transform: downRotate }"></view>
  16. <view class="downwarp-tip">{{ downText }}</view>
  17. </view>
  18. </view>
  19. <!-- 列表内容 -->
  20. <slot></slot>
  21. <!-- 空布局 -->
  22. <!-- <mescroll-empty v-if="isShowEmpty" :option="mescroll.optUp.empty" @emptyclick="emptyClick"></mescroll-empty> -->
  23. <!-- 上拉加载区域 (下拉刷新时不显示, 支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-up组件实现)-->
  24. <!-- <mescroll-up v-if="mescroll.optUp.use && !isDownLoading" :option="mescroll.optUp" :type="upLoadType"></mescroll-up> -->
  25. <view v-if="mescroll.optUp.use && !isDownLoading" class="mescroll-upwarp">
  26. <!-- 加载中 (此处不能用v-if,否则android小程序快速上拉可能会不断触发上拉回调) -->
  27. <view v-show="upLoadType === 1">
  28. <view class="upwarp-progress mescroll-rotate"></view>
  29. <view class="upwarp-tip">{{ mescroll.optUp.textLoading }}</view>
  30. </view>
  31. <!-- 无数据 -->
  32. <view v-if="upLoadType === 2" class="upwarp-nodata">{{ mescroll.optUp.textNoMore }}</view>
  33. </view>
  34. </view>
  35. <!-- 回到顶部按钮 (fixed元素需写在transform外面,防止降级为absolute)-->
  36. <mescroll-top v-model="isShowToTop" :option="mescroll.optUp.toTop" @click="toTopClick" v-if="showTop"></mescroll-top>
  37. </view>
  38. </template>
  39. <script>
  40. // 引入mescroll-uni.js,处理核心逻辑
  41. import MeScroll from './mescroll-uni.js';
  42. // 引入全局配置
  43. import GlobalOption from './mescroll-uni-option.js';
  44. // 引入空布局组件
  45. import MescrollEmpty from './components/mescroll-empty.vue';
  46. // 引入回到顶部组件
  47. import MescrollTop from './components/mescroll-top.vue';
  48. export default {
  49. components: {
  50. MescrollEmpty,
  51. MescrollTop
  52. },
  53. data() {
  54. return {
  55. mescroll: { optDown: {}, optUp: {} }, // mescroll实例
  56. downHight: 0, //下拉刷新: 容器高度
  57. downRate: 0, // 下拉比率(inOffset: rate<1; outOffset: rate>=1)
  58. downLoadType: 4, // 下拉刷新状态 (inOffset:1, outOffset:2, showLoading:3, endDownScroll:4)
  59. upLoadType: 0, // 上拉加载状态:0(loading前),1(loading中),2(没有更多了)
  60. isShowEmpty: false, // 是否显示空布局
  61. isShowToTop: false, // 是否显示回到顶部按钮
  62. windowHeight: 0, // 可使用窗口的高度
  63. statusBarHeight: 0 // 状态栏高度
  64. };
  65. },
  66. props: {
  67. down: Object, // 下拉刷新的参数配置
  68. up: Object, // 上拉加载的参数配置
  69. top: [String, Number], // 下拉布局往下的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  70. topbar: Boolean, // top的偏移量是否加上状态栏高度, 默认false (使用场景:取消原生导航栏时,配置此项可自动加上状态栏高度的偏移量)
  71. bottom: [String, Number], // 上拉布局往上的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  72. safearea: Boolean, // bottom的偏移量是否加上底部安全区的距离, 默认false (需要适配iPhoneX时使用)
  73. height: [String, Number], // 指定mescroll最小高度,默认windowHeight,使列表不满屏仍可下拉
  74. showTop: {
  75. type: Boolean,
  76. default: true
  77. }
  78. },
  79. computed: {
  80. // mescroll最小高度,默认windowHeight,使列表不满屏仍可下拉
  81. minHeight() {
  82. return this.toPx(this.height || '100%') + 'px';
  83. },
  84. // 下拉布局往下偏移的距离 (px)
  85. numTop() {
  86. return this.toPx(this.top) + (this.topbar ? this.statusBarHeight : 0);
  87. },
  88. padTop() {
  89. return this.numTop + 'px';
  90. },
  91. // 上拉布局往上偏移 (px)
  92. numBottom() {
  93. return this.toPx(this.bottom);
  94. },
  95. padBottom() {
  96. return this.numBottom + 'px';
  97. },
  98. padBottomConstant() {
  99. return this.safearea ? 'calc(' + this.padBottom + ' + constant(safe-area-inset-bottom))' : this.padBottom;
  100. },
  101. padBottomEnv() {
  102. return this.safearea ? 'calc(' + this.padBottom + ' + env(safe-area-inset-bottom))' : this.padBottom;
  103. },
  104. // 是否为重置下拉的状态
  105. isDownReset() {
  106. return this.downLoadType === 3 || this.downLoadType === 4;
  107. },
  108. // 过渡
  109. transition() {
  110. return this.isDownReset ? 'transform 300ms' : '';
  111. },
  112. translateY() {
  113. return this.downHight > 0 ? 'translateY(' + this.downHight + 'px)' : ''; // transform会使fixed失效,需注意把fixed元素写在mescroll之外
  114. },
  115. // 是否在加载中
  116. isDownLoading() {
  117. return this.downLoadType === 3;
  118. },
  119. // 旋转的角度
  120. downRotate() {
  121. return 'rotate(' + 360 * this.downRate + 'deg)';
  122. },
  123. // 文本提示
  124. downText() {
  125. switch (this.downLoadType) {
  126. case 1:
  127. return this.mescroll.optDown.textInOffset;
  128. case 2:
  129. return this.mescroll.optDown.textOutOffset;
  130. case 3:
  131. return this.mescroll.optDown.textLoading;
  132. case 4:
  133. return this.mescroll.optDown.textLoading;
  134. default:
  135. return this.mescroll.optDown.textInOffset;
  136. }
  137. }
  138. },
  139. methods: {
  140. //number,rpx,upx,px,% --> px的数值
  141. toPx(num) {
  142. if (typeof num === 'string') {
  143. if (num.indexOf('px') !== -1) {
  144. if (num.indexOf('rpx') !== -1) {
  145. // "10rpx"
  146. num = num.replace('rpx', '');
  147. } else if (num.indexOf('upx') !== -1) {
  148. // "10upx"
  149. num = num.replace('upx', '');
  150. } else {
  151. // "10px"
  152. return Number(num.replace('px', ''));
  153. }
  154. } else if (num.indexOf('%') !== -1) {
  155. // 传百分比,则相对于windowHeight,传"10%"则等于windowHeight的10%
  156. let rate = Number(num.replace('%', '')) / 100;
  157. return this.windowHeight * rate;
  158. }
  159. }
  160. return num ? uni.upx2px(Number(num)) : 0;
  161. },
  162. //注册列表touchstart事件,用于下拉刷新
  163. touchstartEvent(e) {
  164. this.mescroll.touchstartEvent(e);
  165. },
  166. //注册列表touchmove事件,用于下拉刷新
  167. touchmoveEvent(e) {
  168. this.mescroll.touchmoveEvent(e);
  169. },
  170. //注册列表touchend事件,用于下拉刷新
  171. touchendEvent(e) {
  172. this.mescroll.touchendEvent(e);
  173. },
  174. // 点击空布局的按钮回调
  175. emptyClick() {
  176. this.$emit('emptyclick', this.mescroll);
  177. },
  178. // 点击回到顶部的按钮回调
  179. toTopClick() {
  180. this.mescroll.scrollTo(0, this.mescroll.optUp.toTop.duration); // 执行回到顶部
  181. this.$emit('topclick', this.mescroll); // 派发点击回到顶部按钮的回调
  182. }
  183. },
  184. // 使用created初始化mescroll对象; 如果用mounted部分css样式编译到H5会失效
  185. created() {
  186. let vm = this;
  187. let diyOption = {
  188. // 下拉刷新的配置
  189. down: {
  190. inOffset(mescroll) {
  191. vm.downLoadType = 1; // 下拉的距离进入offset范围内那一刻的回调 (自定义mescroll组件时,此行不可删)
  192. },
  193. outOffset(mescroll) {
  194. vm.downLoadType = 2; // 下拉的距离大于offset那一刻的回调 (自定义mescroll组件时,此行不可删)
  195. },
  196. onMoving(mescroll, rate, downHight) {
  197. // 下拉过程中的回调,滑动过程一直在执行;
  198. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  199. vm.downRate = rate; //下拉比率 (inOffset: rate<1; outOffset: rate>=1)
  200. },
  201. showLoading(mescroll, downHight) {
  202. vm.downLoadType = 3; // 显示下拉刷新进度的回调 (自定义mescroll组件时,此行不可删)
  203. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  204. },
  205. endDownScroll(mescroll) {
  206. vm.downLoadType = 4; // 结束下拉 (自定义mescroll组件时,此行不可删)
  207. vm.downHight = 0; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  208. },
  209. // 派发下拉刷新的回调
  210. callback: function(mescroll) {
  211. vm.$emit('down', mescroll);
  212. }
  213. },
  214. // 上拉加载的配置
  215. up: {
  216. // 显示加载中的回调
  217. showLoading() {
  218. vm.upLoadType = 1;
  219. },
  220. // 显示无更多数据的回调
  221. showNoMore() {
  222. vm.upLoadType = 2;
  223. },
  224. // 隐藏上拉加载的回调
  225. hideUpScroll() {
  226. vm.upLoadType = 0;
  227. },
  228. // 空布局
  229. empty: {
  230. onShow(isShow) {
  231. // 显示隐藏的回调
  232. vm.isShowEmpty = isShow;
  233. }
  234. },
  235. // 回到顶部
  236. toTop: {
  237. onShow(isShow) {
  238. // 显示隐藏的回调
  239. vm.isShowToTop = isShow;
  240. }
  241. },
  242. // 派发上拉加载的回调
  243. callback: function(mescroll) {
  244. vm.$emit('up', mescroll);
  245. }
  246. }
  247. };
  248. MeScroll.extend(diyOption, GlobalOption); // 混入全局的配置
  249. let myOption = JSON.parse(
  250. JSON.stringify({
  251. down: vm.down,
  252. up: vm.up
  253. })
  254. ); // 深拷贝,避免对props的影响
  255. MeScroll.extend(myOption, diyOption); // 混入具体界面的配置
  256. // 初始化MeScroll对象
  257. vm.mescroll = new MeScroll(myOption, true); // 传入true,标记body为滚动区域
  258. // init回调mescroll对象
  259. vm.$emit('init', vm.mescroll);
  260. // 设置高度
  261. const sys = uni.getSystemInfoSync();
  262. if (sys.windowHeight) vm.windowHeight = sys.windowHeight;
  263. if (sys.statusBarHeight) vm.statusBarHeight = sys.statusBarHeight;
  264. // 使down的bottomOffset生效
  265. vm.mescroll.setBodyHeight(sys.windowHeight);
  266. // 因为使用的是page的scroll,这里需自定义scrollTo
  267. vm.mescroll.resetScrollTo((y, t) => {
  268. uni.pageScrollTo({
  269. scrollTop: y,
  270. duration: t
  271. });
  272. });
  273. // 具体的界面如果不配置up.toTop.safearea,则取本vue的safearea值
  274. if (vm.up && vm.up.toTop && vm.up.toTop.safearea != null) {
  275. } else {
  276. vm.mescroll.optUp.toTop.safearea = vm.safearea;
  277. }
  278. }
  279. };
  280. </script>
  281. <style>
  282. @import './mescroll-body.css';
  283. @import './components/mescroll-down.css';
  284. @import './components/mescroll-up.css';
  285. </style>