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.

56 lines
1.8 KiB

3 years ago
  1. /**
  2. * mescroll-body写在子组件时,需通过mescroll的mixins补充子组件缺少的生命周期:
  3. * 当一个页面只有一个mescroll-body写在子组件时, 则使用mescroll-comp.js (参考 mescroll-comp 案例)
  4. * 当一个页面有多个mescroll-body写在子组件时, 则使用mescroll-more.js (参考 mescroll-more 案例)
  5. */
  6. const MescrollMoreMixin = {
  7. data() {
  8. return {
  9. tabIndex: 0 // 当前tab下标
  10. }
  11. },
  12. // 因为子组件无onPageScroll和onReachBottom的页面生命周期,需在页面传递进到子组件
  13. onPageScroll(e) {
  14. let mescroll = this.getMescroll(this.tabIndex);
  15. mescroll && mescroll.onPageScroll(e);
  16. },
  17. onReachBottom() {
  18. let mescroll = this.getMescroll(this.tabIndex);
  19. mescroll && mescroll.onReachBottom();
  20. },
  21. // 当down的native: true时, 还需传递此方法进到子组件
  22. onPullDownRefresh(){
  23. let mescroll = this.getMescroll(this.tabIndex);
  24. mescroll && mescroll.onPullDownRefresh();
  25. },
  26. methods:{
  27. // 根据下标获取对应子组件的mescroll
  28. getMescroll(i){
  29. if(!this.mescrollItems) this.mescrollItems = [];
  30. if(!this.mescrollItems[i]) {
  31. // v-for中的refs
  32. let vForItem = this.$refs["mescrollItem"];
  33. if(vForItem){
  34. this.mescrollItems[i] = vForItem[i]
  35. }else{
  36. // 普通的refs,不可重复
  37. this.mescrollItems[i] = this.$refs["mescrollItem"+i];
  38. }
  39. }
  40. let item = this.mescrollItems[i]
  41. return item ? item.mescroll : null
  42. },
  43. // 切换tab,恢复滚动条位置
  44. tabChange(i){
  45. let mescroll = this.getMescroll(i);
  46. if(mescroll){
  47. // 延时(比$nextTick靠谱一些),确保元素已渲染
  48. setTimeout(()=>{
  49. mescroll.scrollTo(mescroll.getScrollTop(),0)
  50. },30)
  51. }
  52. }
  53. }
  54. }
  55. export default MescrollMoreMixin;