mescroll-uni.vue 17 KB

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