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.

408 lines
16 KiB

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