mescroll-more.js 1.9 KB

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