时空网前端
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.

424 lines
16 KiB

  1. <template>
  2. <view class="mescroll-uni-warp">
  3. <scroll-view :id="viewId" class="mescroll-uni" :class="{'mescroll-uni-fixed':isFixed}" :style="{'height':scrollHeight,'padding-top':padTop,'padding-bottom':padBottom,'top':fixedTop,'bottom':fixedBottom}"
  4. :scroll-top="scrollTop" :scroll-into-view="scrollToViewId" :scroll-with-animation="scrollAnim" @scroll="scroll"
  5. :scroll-y='scrollable' :enable-back-to-top="true">
  6. <view class="mescroll-uni-content mescroll-render-touch" @touchstart="wxsBiz.touchstartEvent" @touchmove="wxsBiz.touchmoveEvent"
  7. @touchend="wxsBiz.touchendEvent" @touchcancel="wxsBiz.touchendEvent" :change:prop="wxsBiz.propObserver" :prop="wxsProp">
  8. <!-- 状态栏 -->
  9. <view v-if="topbar&&statusBarHeight" class="mescroll-topbar" :style="{height: statusBarHeight+'px', background: topbar}"></view>
  10. <view class="mescroll-wxs-content" :style="{'transform': translateY, 'transition': transition}" :change:prop="wxsBiz.callObserver"
  11. :prop="callProp">
  12. <!-- 下拉加载区域 (支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-down组件实现)-->
  13. <!-- <mescroll-down :option="mescroll.optDown" :type="downLoadType" :rate="downRate"></mescroll-down> -->
  14. <view v-if="mescroll.optDown.use" class="mescroll-downwarp" :style="{'background':mescroll.optDown.bgColor,'color':mescroll.optDown.textColor}">
  15. <view class="downwarp-content">
  16. <!-- <image style="width: 160rpx; height: 40rpx;" src="/static/images/logo.png" mode="widthFix"></image> -->
  17. <view class="downwarp-progress mescroll-wxs-progress" :class="{'mescroll-rotate': isDownLoading}" :style="{'border-color':mescroll.optDown.textColor, 'transform': downRotate}"></view>
  18. <view class="downwarp-tip">{{downText}}</view>
  19. </view>
  20. </view>
  21. <!-- 列表内容 -->
  22. <slot></slot>
  23. <!-- 空布局 -->
  24. <mescroll-empty v-if="isShowEmpty" :option="mescroll.optUp.empty" @emptyclick="emptyClick"></mescroll-empty>
  25. <!-- 上拉加载区域 (下拉刷新时不显示, 支付宝小程序子组件传参给子子组件仍报单项数据流的异常,暂时不通过mescroll-up组件实现)-->
  26. <!-- <mescroll-up v-if="mescroll.optUp.use && !isDownLoading && upLoadType!==3" :option="mescroll.optUp" :type="upLoadType"></mescroll-up> -->
  27. <view v-if="mescroll.optUp.use && !isDownLoading && upLoadType!==3" class="mescroll-upwarp" :style="{'background':mescroll.optUp.bgColor,'color':mescroll.optUp.textColor}">
  28. <!-- 加载中 (此处不能用v-if,否则android小程序快速上拉可能会不断触发上拉回调) -->
  29. <view v-show="upLoadType===1">
  30. <view class="upwarp-progress mescroll-rotate" :style="{'border-color':mescroll.optUp.textColor}"></view>
  31. <view class="upwarp-tip">{{ mescroll.optUp.textLoading }}</view>
  32. </view>
  33. <!-- 无数据 -->
  34. <view v-if="upLoadType===2" class="upwarp-nodata">{{ mescroll.optUp.textNoMore }}</view>
  35. </view>
  36. </view>
  37. <!-- 底部是否偏移TabBar的高度(默认仅在H5端的tab页生效) -->
  38. <!-- #ifdef H5 -->
  39. <view v-if="bottombar && windowBottom>0" class="mescroll-bottombar" :style="{height: windowBottom+'px'}"></view>
  40. <!-- #endif -->
  41. <!-- 适配iPhoneX -->
  42. <view v-if="safearea" class="mescroll-safearea"></view>
  43. </view>
  44. </scroll-view>
  45. <!-- 回到顶部按钮 (fixed元素,需写在scroll-view外面,防止滚动的时候抖动)-->
  46. <mescroll-top v-model="isShowToTop" :option="mescroll.optUp.toTop" @click="toTopClick"></mescroll-top>
  47. <!-- #ifdef MP-WEIXIN || APP-PLUS || H5 -->
  48. <!-- renderjs的数据载体,不可写在mescroll-downwarp内部,避免use为false时,载体丢失,无法更新数据 -->
  49. <view :change:prop="renderBiz.propObserver" :prop="wxsProp"></view>
  50. <!-- #endif -->
  51. </view>
  52. </template>
  53. <!-- 微信小程序, app, h5使用wxs -->
  54. <!-- #ifdef MP-WEIXIN || APP-PLUS || H5-->
  55. <script src="./wxs/wxs.wxs" module="wxsBiz" lang="wxs"></script>
  56. <!-- #endif -->
  57. <!-- app, h5使用renderjs -->
  58. <!-- #ifdef APP-PLUS || H5 -->
  59. <script module="renderBiz" lang="renderjs">
  60. import renderBiz from './wxs/renderjs.js';
  61. export default {
  62. mixins: [renderBiz]
  63. }
  64. </script>
  65. <!-- #endif -->
  66. <script>
  67. // 引入mescroll-uni.js,处理核心逻辑
  68. import MeScroll from './mescroll-uni.js';
  69. // 引入全局配置
  70. import GlobalOption from './mescroll-uni-option.js';
  71. // 引入空布局组件
  72. import MescrollEmpty from './components/mescroll-empty.vue';
  73. // 引入回到顶部组件
  74. import MescrollTop from './components/mescroll-top.vue';
  75. // 引入兼容wxs(含renderjs)写法的mixins
  76. import WxsMixin from './wxs/mixins.js';
  77. export default {
  78. mixins: [WxsMixin],
  79. components: {
  80. MescrollEmpty,
  81. MescrollTop
  82. },
  83. data() {
  84. return {
  85. mescroll: {
  86. optDown: {},
  87. optUp: {}
  88. }, // mescroll实例
  89. viewId: 'id_' + Math.random().toString(36).substr(2, 16), // 随机生成mescroll的id(不能数字开头,否则找不到元素)
  90. downHight: 0, //下拉刷新: 容器高度
  91. downRate: 0, // 下拉比率(inOffset: rate<1; outOffset: rate>=1)
  92. downLoadType: 0, // 下拉刷新状态: 0(loading前), 1(inOffset), 2(outOffset), 3(showLoading), 4(endDownScroll)
  93. upLoadType: 0, // 上拉加载状态: 0(loading前), 1loading中, 2没有更多了,显示END文本提示, 3(没有更多了,不显示END文本提示)
  94. isShowEmpty: false, // 是否显示空布局
  95. isShowToTop: false, // 是否显示回到顶部按钮
  96. scrollTop: 0, // 滚动条的位置
  97. scrollAnim: false, // 是否开启滚动动画
  98. windowTop: 0, // 可使用窗口的顶部位置
  99. windowBottom: 0, // 可使用窗口的底部位置
  100. windowHeight: 0, // 可使用窗口的高度
  101. statusBarHeight: 0, // 状态栏高度
  102. scrollToViewId: '' // 滚动到指定view的id
  103. }
  104. },
  105. props: {
  106. down: Object, // 下拉刷新的参数配置
  107. up: Object, // 上拉加载的参数配置
  108. top: [String, Number], // 下拉布局往下的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  109. topbar: [Boolean, String], // top的偏移量是否加上状态栏高度, 默认false (使用场景:取消原生导航栏时,配置此项可留出状态栏的占位, 支持传入字符串背景,如色值,背景图,渐变)
  110. bottom: [String, Number], // 上拉布局往上的偏移量 (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  111. safearea: Boolean, // bottom的偏移量是否加上底部安全区的距离, 默认false (需要适配iPhoneX时使用)
  112. fixed: { // 是否通过fixed固定mescroll的高度, 默认true
  113. type: Boolean,
  114. default: true
  115. },
  116. height: [String, Number], // 指定mescroll的高度, 此项有值,则不使用fixed. (支持20, "20rpx", "20px", "20%"格式的值, 其中纯数字则默认单位rpx, 百分比则相对于windowHeight)
  117. bottombar: { // 底部是否偏移TabBar的高度(默认仅在H5端的tab页生效)
  118. type: Boolean,
  119. default: true
  120. }
  121. },
  122. computed: {
  123. // 是否使用fixed定位 (当height有值,则不使用)
  124. isFixed() {
  125. return !this.height && this.fixed
  126. },
  127. // mescroll的高度
  128. scrollHeight() {
  129. if (this.isFixed) {
  130. return "auto"
  131. } else if (this.height) {
  132. return this.toPx(this.height) + 'px'
  133. } else {
  134. return "100%"
  135. }
  136. },
  137. // 下拉布局往下偏移的距离 (px)
  138. numTop() {
  139. return this.toPx(this.top)
  140. },
  141. fixedTop() {
  142. return this.isFixed ? (this.numTop + this.windowTop) + 'px' : 0
  143. },
  144. padTop() {
  145. return !this.isFixed ? this.numTop + 'px' : 0
  146. },
  147. // 上拉布局往上偏移 (px)
  148. numBottom() {
  149. return this.toPx(this.bottom)
  150. },
  151. fixedBottom() {
  152. return this.isFixed ? (this.numBottom + this.windowBottom) + 'px' : 0
  153. },
  154. padBottom() {
  155. return !this.isFixed ? this.numBottom + 'px' : 0
  156. },
  157. // 是否为重置下拉的状态
  158. isDownReset() {
  159. return this.downLoadType === 3 || this.downLoadType === 4
  160. },
  161. // 过渡
  162. transition() {
  163. return this.isDownReset ? 'transform 300ms' : '';
  164. },
  165. translateY() {
  166. return this.downHight > 0 ? 'translateY(' + this.downHight + 'px)' : ''; // transform会使fixed失效,需注意把fixed元素写在mescroll之外
  167. },
  168. // 列表是否可滑动
  169. scrollable() {
  170. return this.downLoadType === 0 || this.isDownReset
  171. },
  172. // 是否在加载中
  173. isDownLoading() {
  174. return this.downLoadType === 3
  175. },
  176. // 旋转的角度
  177. downRotate() {
  178. return 'rotate(' + 360 * this.downRate + 'deg)'
  179. },
  180. // 文本提示
  181. downText() {
  182. if (!this.mescroll) return ""; // 避免头条小程序初始化时报错
  183. switch (this.downLoadType) {
  184. case 1:
  185. return this.mescroll.optDown.textInOffset;
  186. case 2:
  187. return this.mescroll.optDown.textOutOffset;
  188. case 3:
  189. return this.mescroll.optDown.textLoading;
  190. case 4:
  191. return this.mescroll.optDown.textLoading;
  192. default:
  193. return this.mescroll.optDown.textInOffset;
  194. }
  195. }
  196. },
  197. methods: {
  198. //number,rpx,upx,px,% --> px的数值
  199. toPx(num) {
  200. if (typeof num === "string") {
  201. if (num.indexOf('px') !== -1) {
  202. if (num.indexOf('rpx') !== -1) { // "10rpx"
  203. num = num.replace('rpx', '');
  204. } else if (num.indexOf('upx') !== -1) { // "10upx"
  205. num = num.replace('upx', '');
  206. } else { // "10px"
  207. return Number(num.replace('px', ''))
  208. }
  209. } else if (num.indexOf('%') !== -1) {
  210. // 传百分比,则相对于windowHeight,传"10%"则等于windowHeight的10%
  211. let rate = Number(num.replace("%", "")) / 100
  212. return this.windowHeight * rate
  213. }
  214. }
  215. return num ? uni.upx2px(Number(num)) : 0
  216. },
  217. //注册列表滚动事件,用于下拉刷新和上拉加载
  218. scroll(e) {
  219. this.mescroll.scroll(e.detail, () => {
  220. this.$emit('scroll', this.mescroll) // 此时可直接通过 this.mescroll.scrollTop获取滚动条位置; this.mescroll.isScrollUp获取是否向上滑动
  221. })
  222. },
  223. // 点击空布局的按钮回调
  224. emptyClick() {
  225. this.$emit('emptyclick', this.mescroll)
  226. },
  227. // 点击回到顶部的按钮回调
  228. toTopClick() {
  229. this.mescroll.scrollTo(0, this.mescroll.optUp.toTop.duration); // 执行回到顶部
  230. this.$emit('topclick', this.mescroll); // 派发点击回到顶部按钮的回调
  231. },
  232. // 更新滚动区域的高度 (使内容不满屏和到底,都可继续翻页)
  233. setClientHeight() {
  234. if (this.mescroll.getClientHeight(true) === 0 && !this.isExec) {
  235. this.isExec = true; // 避免多次获取
  236. this.$nextTick(() => { // 确保dom已渲染
  237. let query = uni.createSelectorQuery();
  238. // #ifndef MP-ALIPAY
  239. query = query.in(this) // 支付宝小程序不支持in(this),而字节跳动小程序必须写in(this), 否则都取不到值
  240. // #endif
  241. let view = query.select('#' + this.viewId);
  242. view.boundingClientRect(data => {
  243. this.isExec = false;
  244. if (data) {
  245. this.mescroll.setClientHeight(data.height);
  246. } else if (this.clientNum != 3) { // 极少部分情况,可能dom还未渲染完毕,递归获取,最多重试3次
  247. this.clientNum = this.clientNum == null ? 1 : this.clientNum + 1;
  248. setTimeout(() => {
  249. this.setClientHeight()
  250. }, this.clientNum * 100)
  251. }
  252. }).exec();
  253. })
  254. }
  255. }
  256. },
  257. // 使用created初始化mescroll对象; 如果用mounted部分css样式编译到H5会失效
  258. created() {
  259. let vm = this;
  260. let diyOption = {
  261. // 下拉刷新的配置
  262. down: {
  263. inOffset() {
  264. vm.downLoadType = 1; // 下拉的距离进入offset范围内那一刻的回调 (自定义mescroll组件时,此行不可删)
  265. },
  266. outOffset() {
  267. vm.downLoadType = 2; // 下拉的距离大于offset那一刻的回调 (自定义mescroll组件时,此行不可删)
  268. },
  269. onMoving(mescroll, rate, downHight) {
  270. // 下拉过程中的回调,滑动过程一直在执行;
  271. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  272. vm.downRate = rate; //下拉比率 (inOffset: rate<1; outOffset: rate>=1)
  273. },
  274. showLoading(mescroll, downHight) {
  275. vm.downLoadType = 3; // 显示下拉刷新进度的回调 (自定义mescroll组件时,此行不可删)
  276. vm.downHight = downHight; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  277. },
  278. endDownScroll() {
  279. vm.downLoadType = 4; // 结束下拉 (自定义mescroll组件时,此行不可删)
  280. vm.downHight = 0; // 设置下拉区域的高度 (自定义mescroll组件时,此行不可删)
  281. vm.downResetTimer && clearTimeout(vm.downResetTimer)
  282. vm.downResetTimer = setTimeout(() => { // 过渡动画执行完毕后,需重置为0的状态,以便置空this.transition,避免iOS小程序列表渲染不完整
  283. if (vm.downLoadType === 4) vm.downLoadType = 0
  284. }, 300)
  285. },
  286. // 派发下拉刷新的回调
  287. callback: function(mescroll) {
  288. vm.$emit('down', mescroll)
  289. }
  290. },
  291. // 上拉加载的配置
  292. up: {
  293. // 显示加载中的回调
  294. showLoading() {
  295. vm.upLoadType = 1;
  296. },
  297. // 显示无更多数据的回调
  298. showNoMore() {
  299. vm.upLoadType = 2;
  300. },
  301. // 隐藏上拉加载的回调
  302. hideUpScroll(mescroll) {
  303. vm.upLoadType = mescroll.optUp.hasNext ? 0 : 3;
  304. },
  305. // 空布局
  306. empty: {
  307. onShow(isShow) { // 显示隐藏的回调
  308. vm.isShowEmpty = isShow;
  309. }
  310. },
  311. // 回到顶部
  312. toTop: {
  313. onShow(isShow) { // 显示隐藏的回调
  314. vm.isShowToTop = isShow;
  315. }
  316. },
  317. // 派发上拉加载的回调
  318. callback: function(mescroll) {
  319. vm.$emit('up', mescroll);
  320. // 更新容器的高度 (多mescroll的情况)
  321. vm.setClientHeight()
  322. }
  323. }
  324. }
  325. MeScroll.extend(diyOption, GlobalOption); // 混入全局的配置
  326. let myOption = JSON.parse(JSON.stringify({
  327. 'down': vm.down,
  328. 'up': vm.up
  329. })) // 深拷贝,避免对props的影响
  330. MeScroll.extend(myOption, diyOption); // 混入具体界面的配置
  331. // 初始化MeScroll对象
  332. vm.mescroll = new MeScroll(myOption);
  333. vm.mescroll.viewId = vm.viewId; // 附带id
  334. // init回调mescroll对象
  335. vm.$emit('init', vm.mescroll);
  336. // 设置高度
  337. const sys = uni.getSystemInfoSync();
  338. if (sys.windowTop) vm.windowTop = sys.windowTop;
  339. if (sys.windowBottom) vm.windowBottom = sys.windowBottom;
  340. if (sys.windowHeight) vm.windowHeight = sys.windowHeight;
  341. if (sys.statusBarHeight) vm.statusBarHeight = sys.statusBarHeight;
  342. // 使down的bottomOffset生效
  343. vm.mescroll.setBodyHeight(sys.windowHeight);
  344. // 因为使用的是scrollview,这里需自定义scrollTo
  345. vm.mescroll.resetScrollTo((y, t) => {
  346. vm.scrollAnim = (t !== 0); // t为0,则不使用动画过渡
  347. if (typeof y === 'string') { // 第一个参数如果为字符串,则使用scroll-into-view
  348. // #ifdef MP-WEIXIN
  349. // 微信小程序暂不支持slot里面的scroll-into-view,只能计算位置实现
  350. uni.createSelectorQuery().select('#' + vm.viewId).boundingClientRect(function(rect) {
  351. let mescrollTop = rect.top // mescroll到顶部的距离
  352. uni.createSelectorQuery().select('#' + y).boundingClientRect(function(rect) {
  353. let curY = vm.mescroll.getScrollTop()
  354. let top = rect.top - mescrollTop
  355. top += curY
  356. if (!vm.isFixed) top -= vm.numTop
  357. vm.scrollTop = curY;
  358. vm.$nextTick(function() {
  359. vm.scrollTop = top
  360. })
  361. }).exec()
  362. }).exec()
  363. // #endif
  364. // #ifndef MP-WEIXIN
  365. if (vm.scrollToViewId != y) {
  366. vm.scrollToViewId = y;
  367. } else {
  368. vm.scrollToViewId = ''; // scrollToViewId必须变化才会生效,所以此处先置空再赋值
  369. vm.$nextTick(function() {
  370. vm.scrollToViewId = y;
  371. })
  372. }
  373. // #endif
  374. return;
  375. }
  376. let curY = vm.mescroll.getScrollTop()
  377. if (t === 0 || t === 300) { // 当t使用默认配置的300时,则使用系统自带的动画过渡
  378. vm.scrollTop = curY;
  379. vm.$nextTick(function() {
  380. vm.scrollTop = y
  381. })
  382. } else {
  383. vm.mescroll.getStep(curY, y, step => { // 此写法可支持配置t
  384. vm.scrollTop = step
  385. }, t)
  386. }
  387. })
  388. // 具体的界面如果不配置up.toTop.safearea,则取本vue的safearea值
  389. if (vm.up && vm.up.toTop && vm.up.toTop.safearea != null) {} else {
  390. vm.mescroll.optUp.toTop.safearea = vm.safearea;
  391. }
  392. },
  393. mounted() {
  394. // 设置容器的高度
  395. this.setClientHeight()
  396. }
  397. }
  398. </script>
  399. <style>
  400. @import "./mescroll-uni.css";
  401. @import "./components/mescroll-down.css";
  402. @import './components/mescroll-up.css';
  403. </style>