mescroll-uni.vue 14 KB

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