mescroll-uni.vue 16 KB

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