You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
14 KiB

3 years ago
  1. <template>
  2. <view
  3. class="mescroll-body mescroll-render-touch"
  4. :style="{'minHeight':minHeight, 'padding-top': padTop, 'padding-bottom': padBottom}"
  5. @touchstart="wxsBiz.touchstartEvent"
  6. @touchmove="wxsBiz.touchmoveEvent"
  7. @touchend="wxsBiz.touchendEvent"
  8. @touchcancel="wxsBiz.touchendEvent"
  9. :change:prop="wxsBiz.propObserver"
  10. :prop="wxsProp"
  11. >
  12. <!-- 状态栏 -->
  13. <view v-if="topbar&&statusBarHeight" class="mescroll-topbar" :style="{height: statusBarHeight+'px', background: topbar}"></view>
  14. <view class="mescroll-body-content mescroll-wxs-content" :style="{ transform: translateY, transition: transition }" :change:prop="wxsBiz.callObserver" :prop="callProp">
  15. <!-- 下拉加载区域 (支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-down组件实现)-->
  16. <!-- <mescroll-down :option="mescroll.optDown" :type="downLoadType" :rate="downRate"></mescroll-down> -->
  17. <view v-if="mescroll.optDown.use" class="mescroll-downwarp" :style="{'background':mescroll.optDown.bgColor,'color':mescroll.optDown.textColor}">
  18. <view class="downwarp-content" :change:prop="renderBiz.propObserver" :prop="wxsProp">
  19. <!-- <view class="downwarp-progress mescroll-wxs-progress" :class="{'mescroll-rotate': isDownLoading}" :style="{'border-color':mescroll.optDown.textColor, 'transform': downRotate}"></view> -->
  20. <view class="downwarp-tip">
  21. <image v-show="downText === '下拉刷新'" style="width:80rpx;height:86rpx" src="/static/loading/hamster.png"></image>
  22. <image v-show="downText !== '下拉刷新'" style="width:80rpx;height:86rpx" src="/static/loading/hamster.gif"></image>
  23. </view>
  24. <!-- {{downText}} -->
  25. </view>
  26. </view>
  27. <!-- 列表内容 -->
  28. <slot></slot>
  29. <!-- 空布局 -->
  30. <mescroll-empty v-if="isShowEmpty" :option="mescroll.optUp.empty" @emptyclick="emptyClick"></mescroll-empty>
  31. <!-- 上拉加载区域 (下拉刷新时不显示, 支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-up组件实现)-->
  32. <!-- <mescroll-up v-if="mescroll.optUp.use && !isDownLoading && upLoadType!==3" :option="mescroll.optUp" :type="upLoadType"></mescroll-up> -->
  33. <view v-if="mescroll.optUp.use && !isDownLoading && upLoadType!==3" class="mescroll-upwarp" :style="{'background':mescroll.optUp.bgColor,'color':mescroll.optUp.textColor}">
  34. <!-- 加载中 (此处不能用v-if,否则android小程序快速上拉可能会不断触发上拉回调) -->
  35. <view v-show="upLoadType===1" :style="{height: upLoadType===1 ? 'auto' : 0}" style="display: flex;align-items: center;justify-content: center;overflow: hidden">
  36. <!-- <view class="upwarp-progress mescroll-rotate" :style="{'border-color':mescroll.optUp.textColor}"></view> -->
  37. <image style="width: 64rpx;height: 68rpx" src="/static/loading/hamster.gif"></image>
  38. <view class="upwarp-tip">{{ mescroll.optUp.textLoading }}</view>
  39. </view>
  40. <!-- 无数据 -->
  41. <!-- <view v-if="upLoadType===2" class="upwarp-nodata">{{ mescroll.optUp.textNoMore }}</view> -->
  42. <view v-if="upLoadType===2" class="mix-nodata center">
  43. <image class="logo" src="/static/logo-b-w.png"></image>
  44. <text>国云网络提供技术支持</text>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 底部是否偏移TabBar的高度(默认仅在H5端的tab页生效) -->
  49. <!-- #ifdef H5 -->
  50. <!-- <view v-if="bottombar && windowBottom>0" class="mescroll-bottombar" :style="{height: windowBottom+'px'}"></view> -->
  51. <!-- #endif -->
  52. <!-- 适配iPhoneX -->
  53. <view v-if="safearea" class="mescroll-safearea"></view>
  54. <!-- 回到顶部按钮 (fixed元素需写在transform外面,防止降级为absolute)-->
  55. <!-- <mescroll-top v-model="isShowToTop" :option="mescroll.optUp.toTop" @click="toTopClick"></mescroll-top> -->
  56. </view>
  57. </template>
  58. <!-- 微信小程序, app, h5使用wxs -->
  59. <!-- #ifdef MP-WEIXIN || APP-PLUS || H5-->
  60. <script src="./wxs/wxs.wxs" module="wxsBiz" lang="wxs"></script>
  61. <!-- #endif -->
  62. <!-- app, h5使用renderjs -->
  63. <!-- #ifdef APP-PLUS || H5 -->
  64. <script module="renderBiz" lang="renderjs">
  65. import renderBiz from './wxs/renderjs.js';
  66. export default {
  67. mixins: [renderBiz]
  68. }
  69. </script>
  70. <!-- #endif -->
  71. <script>
  72. // 引入mescroll-uni.js,处理核心逻辑
  73. import MeScroll from './mescroll-uni.js';
  74. // 引入全局配置
  75. import GlobalOption from './mescroll-uni-option.js';
  76. // 引入空布局组件
  77. import MescrollEmpty from './components/mescroll-empty.vue';
  78. // 引入回到顶部组件
  79. import MescrollTop from './components/mescroll-top.vue';
  80. // 引入兼容wxs(含renderjs)写法的mixins
  81. import WxsMixin from './wxs/mixins.js';
  82. export default {
  83. mixins: [WxsMixin],
  84. components: {
  85. MescrollEmpty,
  86. MescrollTop
  87. },
  88. data() {
  89. return {
  90. mescroll: {optDown:{},optUp:{}}, // mescroll实例
  91. downHight: 0, //下拉刷新: 容器高度
  92. downRate: 0, // 下拉比率(inOffset: rate<1; outOffset: rate>=1)
  93. downLoadType: 0, // 下拉刷新状态: 0(loading前), 1(inOffset), 2(outOffset), 3(showLoading), 4(endDownScroll)
  94. upLoadType: 0, // 上拉加载状态:0(loading前),1(loading中),2(没有更多了,显示END文本提示),3(没有更多了,不显示END文本提示)
  95. isShowEmpty: false, // 是否显示空布局
  96. isShowToTop: false, // 是否显示回到顶部按钮
  97. windowHeight: 0, // 可使用窗口的高度
  98. windowBottom: 0, // 可使用窗口的底部位置
  99. statusBarHeight: 0 // 状态栏高度
  100. };
  101. },
  102. props: {
  103. down: Object, // 下拉刷新的参数配置
  104. up: Object, // 上拉加载的参数配置
  105. top: [String, Number], // 下拉布局往下的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  106. topbar: [Boolean, String], // top的偏移量是否加上状态栏高度, 默认false (使用场景:取消原生导航栏时,配置此项可留出状态栏的占位, 支持传入字符串背景,如色值,背景图,渐变)
  107. bottom: [String, Number], // 上拉布局往上的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  108. safearea: Boolean, // bottom的偏移量是否加上底部安全区的距离, 默认false (需要适配iPhoneX时使用)
  109. height: [String, Number], // 指定mescroll最小高度,默认windowHeight,使列表不满屏仍可下拉
  110. bottombar:{ // 底部是否偏移TabBar的高度(默认仅在H5端的tab页生效)
  111. type: Boolean,
  112. default: true
  113. }
  114. },
  115. computed: {
  116. // mescroll最小高度,默认windowHeight,使列表不满屏仍可下拉
  117. minHeight(){
  118. return this.toPx(this.height || '100%') + 'px'
  119. },
  120. // 下拉布局往下偏移的距离 (px)
  121. numTop() {
  122. return this.toPx(this.top)
  123. },
  124. padTop() {
  125. return this.numTop + 'px';
  126. },
  127. // 上拉布局往上偏移 (px)
  128. numBottom() {
  129. return this.toPx(this.bottom);
  130. },
  131. padBottom() {
  132. return this.numBottom + 'px';
  133. },
  134. // 是否为重置下拉的状态
  135. isDownReset() {
  136. return this.downLoadType === 3 || this.downLoadType === 4;
  137. },
  138. // 过渡
  139. transition() {
  140. return this.isDownReset ? 'transform 300ms' : '';
  141. },
  142. translateY() {
  143. return this.downHight > 0 ? 'translateY(' + this.downHight + 'px)' : ''; // transform会使fixed失效,需注意把fixed元素写在mescroll之外
  144. },
  145. // 是否在加载中
  146. isDownLoading(){
  147. return this.downLoadType === 3
  148. },
  149. // 旋转的角度
  150. downRotate(){
  151. return 'rotate(' + 360 * this.downRate + 'deg)'
  152. },
  153. // 文本提示
  154. downText(){
  155. if(!this.mescroll) return ""; // 避免头条小程序初始化时报错
  156. switch (this.downLoadType){
  157. case 1: return this.mescroll.optDown.textInOffset;
  158. case 2: return this.mescroll.optDown.textOutOffset;
  159. case 3: return this.mescroll.optDown.textLoading;
  160. case 4: return this.mescroll.optDown.textLoading;
  161. default: return this.mescroll.optDown.textInOffset;
  162. }
  163. }
  164. },
  165. methods: {
  166. //number,rpx,upx,px,% --> px的数值
  167. toPx(num) {
  168. if (typeof num === 'string') {
  169. if (num.indexOf('px') !== -1) {
  170. if (num.indexOf('rpx') !== -1) {
  171. // "10rpx"
  172. num = num.replace('rpx', '');
  173. } else if (num.indexOf('upx') !== -1) {
  174. // "10upx"
  175. num = num.replace('upx', '');
  176. } else {
  177. // "10px"
  178. return Number(num.replace('px', ''));
  179. }
  180. } else if (num.indexOf('%') !== -1) {
  181. // 传百分比,则相对于windowHeight,传"10%"则等于windowHeight的10%
  182. let rate = Number(num.replace('%', '')) / 100;
  183. return this.windowHeight * rate;
  184. }
  185. }
  186. return num ? uni.upx2px(Number(num)) : 0;
  187. },
  188. // 点击空布局的按钮回调
  189. emptyClick() {
  190. this.$emit('emptyclick', this.mescroll);
  191. },
  192. // 点击回到顶部的按钮回调
  193. toTopClick() {
  194. this.mescroll.scrollTo(0, this.mescroll.optUp.toTop.duration); // 执行回到顶部
  195. this.$emit('topclick', this.mescroll); // 派发点击回到顶部按钮的回调
  196. }
  197. },
  198. // 使用created初始化mescroll对象; 如果用mounted部分css样式编译到H5会失效
  199. created() {
  200. let vm = this;
  201. let diyOption = {
  202. // 下拉刷新的配置
  203. down: {
  204. auto: false,
  205. inOffset() {
  206. vm.downLoadType = 1; // 下拉的距离进入offset范围内那一刻的回调 (自定义mescroll组件时,此行不可删)
  207. },
  208. outOffset() {
  209. vm.downLoadType = 2; // 下拉的距离大于offset那一刻的回调 (自定义mescroll组件时,此行不可删)
  210. },
  211. onMoving(mescroll, rate, downHight) {
  212. // 下拉过程中的回调,滑动过程一直在执行;
  213. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  214. vm.downRate = rate; //下拉比率 (inOffset: rate<1; outOffset: rate>=1)
  215. },
  216. showLoading(mescroll, downHight) {
  217. vm.downLoadType = 3; // 显示下拉刷新进度的回调 (自定义mescroll组件时,此行不可删)
  218. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  219. },
  220. endDownScroll() {
  221. vm.downLoadType = 4; // 结束下拉 (自定义mescroll组件时,此行不可删)
  222. vm.downHight = 0; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  223. if(vm.downResetTimer) {clearTimeout(vm.downResetTimer); vm.downResetTimer = null} // 移除重置倒计时
  224. vm.downResetTimer = setTimeout(()=>{ // 过渡动画执行完毕后,需重置为0的状态,避免下次inOffset不及时显示textInOffset
  225. if(vm.downLoadType === 4) vm.downLoadType = 0
  226. },300)
  227. },
  228. // 派发下拉刷新的回调
  229. callback: function(mescroll) {
  230. vm.$emit('down', mescroll);
  231. }
  232. },
  233. // 上拉加载的配置
  234. up: {
  235. // 显示加载中的回调
  236. showLoading() {
  237. vm.upLoadType = 1;
  238. },
  239. // 显示无更多数据的回调
  240. showNoMore() {
  241. vm.upLoadType = 2;
  242. },
  243. // 隐藏上拉加载的回调
  244. hideUpScroll(mescroll) {
  245. vm.upLoadType = mescroll.optUp.hasNext ? 0 : 3;
  246. },
  247. // 空布局
  248. empty: {
  249. onShow(isShow) {
  250. // 显示隐藏的回调
  251. vm.isShowEmpty = isShow;
  252. }
  253. },
  254. // 回到顶部
  255. toTop: {
  256. onShow(isShow) {
  257. // 显示隐藏的回调
  258. vm.isShowToTop = isShow;
  259. }
  260. },
  261. // 派发上拉加载的回调
  262. callback: function(mescroll) {
  263. vm.$emit('up', mescroll);
  264. }
  265. }
  266. };
  267. MeScroll.extend(diyOption, GlobalOption); // 混入全局的配置
  268. let myOption = JSON.parse(JSON.stringify({down: vm.down,up: vm.up})); // 深拷贝,避免对props的影响
  269. MeScroll.extend(myOption, diyOption); // 混入具体界面的配置
  270. // 初始化MeScroll对象
  271. vm.mescroll = new MeScroll(myOption, true); // 传入true,标记body为滚动区域
  272. // init回调mescroll对象
  273. vm.$emit('init', vm.mescroll);
  274. // 设置高度
  275. const sys = uni.getSystemInfoSync();
  276. if (sys.windowHeight) vm.windowHeight = sys.windowHeight;
  277. if (sys.windowBottom) vm.windowBottom = sys.windowBottom;
  278. if (sys.statusBarHeight) vm.statusBarHeight = sys.statusBarHeight;
  279. // 使down的bottomOffset生效
  280. vm.mescroll.setBodyHeight(sys.windowHeight);
  281. // 因为使用的是page的scroll,这里需自定义scrollTo
  282. vm.mescroll.resetScrollTo((y, t) => {
  283. if(typeof y === 'string'){
  284. // 滚动到指定view (y必须为元素的id,不带#)
  285. setTimeout(()=>{ // 延时确保view已渲染; 不使用$nextTick
  286. uni.createSelectorQuery().select('#'+y).boundingClientRect(function(rect){
  287. let top = rect.top
  288. top += vm.mescroll.getScrollTop()
  289. uni.pageScrollTo({
  290. scrollTop: top,
  291. duration: t
  292. })
  293. }).exec()
  294. },30)
  295. } else{
  296. // 滚动到指定位置 (y必须为数字)
  297. uni.pageScrollTo({
  298. scrollTop: y,
  299. duration: t
  300. })
  301. }
  302. });
  303. // 具体的界面如果不配置up.toTop.safearea,则取本vue的safearea值
  304. if (vm.up && vm.up.toTop && vm.up.toTop.safearea != null) {} else {
  305. vm.mescroll.optUp.toTop.safearea = vm.safearea;
  306. }
  307. }
  308. };
  309. </script>
  310. <style lang="scss">
  311. @import "./mescroll-body.css";
  312. @import "./components/mescroll-down.css";
  313. @import './components/mescroll-up.css';
  314. .mix-nodata{
  315. height: 52rpx;
  316. font-size: 26rpx;
  317. color: #999;
  318. .logo{
  319. width: 34rpx;
  320. height: 34rpx;
  321. margin-right: 12rpx;
  322. }
  323. }
  324. </style>