mescroll-mixins.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // mescroll-body 和 mescroll-uni 通用
  2. // import MescrollUni from "./mescroll-uni.vue";
  3. // import MescrollBody from "./mescroll-body.vue";
  4. const MescrollMixin = {
  5. // components: { // 非H5端无法通过mixin注册组件, 只能在main.js中注册全局组件或具体界面中注册
  6. // MescrollUni,
  7. // MescrollBody
  8. // },
  9. data() {
  10. return {
  11. mescroll: null, //mescroll实例对象
  12. downOption:{
  13. auto:false
  14. }
  15. }
  16. },
  17. // 注册系统自带的下拉刷新 (配置down.native为true时生效, 还需在pages配置enablePullDownRefresh:true;详请参考mescroll-native的案例)
  18. onPullDownRefresh(){
  19. this.mescroll && this.mescroll.onPullDownRefresh();
  20. },
  21. // 注册列表滚动事件,用于判定在顶部可下拉刷新,在指定位置可显示隐藏回到顶部按钮 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  22. onPageScroll(e) {
  23. this.mescroll && this.mescroll.onPageScroll(e);
  24. },
  25. // 注册滚动到底部的事件,用于上拉加载 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  26. onReachBottom() {
  27. this.mescroll && this.mescroll.onReachBottom();
  28. },
  29. methods: {
  30. // mescroll组件初始化的回调,可获取到mescroll对象
  31. mescrollInit(mescroll) {
  32. this.mescroll = mescroll;
  33. this.mescrollInitByRef(); // 兼容字节跳动小程序
  34. },
  35. // 以ref的方式初始化mescroll对象 (兼容字节跳动小程序)
  36. mescrollInitByRef() {
  37. if(!this.mescroll || !this.mescroll.resetUpScroll){
  38. let mescrollRef = this.$refs.mescrollRef;
  39. if(mescrollRef) this.mescroll = mescrollRef.mescroll
  40. }
  41. },
  42. // 下拉刷新的回调 (mixin默认resetUpScroll)
  43. downCallback() {
  44. if(this.mescroll.optUp.use){
  45. this.mescroll.resetUpScroll()
  46. }else{
  47. setTimeout(()=>{
  48. this.mescroll.endSuccess();
  49. }, 500)
  50. }
  51. },
  52. // 上拉加载的回调
  53. upCallback() {
  54. // mixin默认延时500自动结束加载
  55. setTimeout(()=>{
  56. this.mescroll.endErr();
  57. }, 500)
  58. }
  59. },
  60. mounted() {
  61. this.mescrollInitByRef(); // 兼容字节跳动小程序, 避免未设置@init或@init此时未能取到ref的情况
  62. }
  63. }
  64. export default MescrollMixin;