时空网前端
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.

74 lines
2.3 KiB

  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. // 上拉
  13. upOpt: {
  14. },
  15. // 下拉
  16. downOpt: {
  17. auto: false,
  18. inOffsetRate: 0.9
  19. },
  20. }
  21. },
  22. // 注册系统自带的下拉刷新 (配置down.native为true时生效, 还需在pages配置enablePullDownRefresh:true;详请参考mescroll-native的案例)
  23. onPullDownRefresh(){
  24. this.mescroll && this.mescroll.onPullDownRefresh();
  25. },
  26. // 注册列表滚动事件,用于判定在顶部可下拉刷新,在指定位置可显示隐藏回到顶部按钮 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  27. onPageScroll(e) {
  28. this.mescroll && this.mescroll.onPageScroll(e);
  29. },
  30. // 注册滚动到底部的事件,用于上拉加载 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  31. onReachBottom() {
  32. this.mescroll && this.mescroll.onReachBottom();
  33. },
  34. methods: {
  35. // mescroll组件初始化的回调,可获取到mescroll对象
  36. mescrollInit(mescroll) {
  37. this.mescroll = mescroll;
  38. this.mescrollInitByRef(); // 兼容字节跳动小程序
  39. },
  40. // 以ref的方式初始化mescroll对象 (兼容字节跳动小程序: http://www.mescroll.com/qa.html?v=20200107#q26)
  41. mescrollInitByRef() {
  42. if(!this.mescroll || !this.mescroll.resetUpScroll){
  43. let mescrollRef = this.$refs.mescrollRef;
  44. if(mescrollRef) this.mescroll = mescrollRef.mescroll
  45. }
  46. },
  47. // 下拉刷新的回调 (mixin默认resetUpScroll)
  48. downCallback() {
  49. if(this.mescroll.optUp.use){
  50. this.mescroll.resetUpScroll()
  51. }else{
  52. setTimeout(()=>{
  53. this.mescroll.endSuccess();
  54. }, 500)
  55. }
  56. },
  57. // 上拉加载的回调
  58. upCallback() {
  59. // mixin默认延时500自动结束加载
  60. setTimeout(()=>{
  61. this.mescroll.endErr();
  62. }, 500)
  63. }
  64. },
  65. mounted() {
  66. this.mescrollInitByRef(); // 兼容字节跳动小程序, 避免未设置@init或@init此时未能取到ref的情况
  67. }
  68. }
  69. export default MescrollMixin;