老师你好~~

老师你好~~

http://img1.sycdn.imooc.com//climg/5e67611b095c17ad05290276.jpg

<template>

  <swiper :options="swiperOption" ref="swiper">

        <div class="mine-scroll-pull-down" v-if="pullDown">

         <me-loading :text="pullDownText" inline ref="pullDownLoading"></me-loading>

         </div> 

    <swiper-slide>

      <slot></slot>

    </swiper-slide>

    <div class="mine-scroll-pull-up" v-if="pullUp">

         <me-loading :text="pullDownText" inline ref="pullDownLoading"></me-loading>

         </div> 

    <div class="swiper-scrollbar" v-if="scrollbar" slot="scrollbar"></div>

  </swiper>

</template>

<script>

import { swiper, swiperSlide } from 'vue-awesome-swiper';

 import MeLoading from 'base/loading';

 import {

    PULL_DOWN_HEIGHT,

    PULL_DOWN_TEXT_INIT,

    PULL_DOWN_TEXT_START,

    PULL_DOWN_TEXT_ING,

    PULL_DOWN_TEXT_END,

    PULL_UP_HEIGHT,

    PULL_UP_TEXT_INIT,

    PULL_UP_TEXT_START,

    PULL_UP_TEXT_ING,

    PULL_UP_TEXT_END

  } from './config';

export default {

  name: 'MeScroll',

  components: {

    swiper,

    swiperSlide,

    MeLoading

  },

  props: {

    scrollbar: {

      type: Boolean,

      default: true

    },

    pullDown: {

           type: Boolean,

           default: false

       },

    pullUp: {

        type: Boolean,

        default: false

      }

  },

  data: {

    type: [Array, Object]

  },

  data() {

    return {

     pulling: false,

     pullDownText:PULL_DOWN_TEXT_INIT,  

     pullUpText: PULL_DOWN_TEXT_INIT,

      swiperOption: {

        direction: 'vertical',

        slidesPerView: 'auto',

        freeMode: true,

        setWrapperSize: true,

        scrollbar: {

          el: this.scrollbar ? '.swiper-scrollbar' : null,

          hide: true

        },

        on: {

          sliderMove: this.scroll,

          touchEnd: this.touchEnd

        }

      }

    }

  },

  watch: {

    data() {

      this.updata();

    }

  },

  methods: {

    updata() {

      this.$refs.swiper && this.$refs.swiper.swiper.update();

    },

     scroll() {

       const swiper = this.$refs.swiper.swiper;

       if(this.pulling) {

         return;

       }

       if(swiper.translate>0){

         if(!this.pullDown){

           return;

         }

         if(swiper.translate > PULL_DOWN_HEIGHT){

            this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_START);

         }else{

           this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_INIT);

         }

          } else if (swiper.isEnd) { // 上拉

          if (!this.pullUp) {

            return;

          }

          const isPullUp = Math.abs(swiper.translate) + swiper.height - PULL_UP_HEIGHT > parseInt(swiper.$wrapperEl.css('height'));


          if (isPullUp) {

            this.$refs.pullUpLoading.setText(PULL_UP_TEXT_START);

          } else {

            this.$refs.pullUpLoading.setText(PULL_UP_TEXT_INIT);

          }

       }

     },

     touchEnd() {

       const swiper = this.$refs.swiper.swiper;


       if(swiper.translate> PULL_DOWN_HEIGHT) {

         if(!this.pullDown){

           return;

         }

          this.pulling= true;

          swiper.allowTouchMove = false;// 禁止触摸

          swiper.setTransition(swiper.params.speed);

          swiper.setTranslate(PULL_DOWN_HEIGHT);

          swiper.params.virtualTranslate = true;// 定住不给回弹

          this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_ING);

          this.$emit('pull-down', this.pullDownEnd);// 触发一个事件

       }else if (swiper.isEnd) { // 底部

          const totalHeight = parseInt(swiper.$wrapperEl.css('height'));

          const isPullUp = Math.abs(swiper.translate) + swiper.height - PULL_UP_HEIGHT > totalHeight;


          if (isPullUp) { // 上拉

            if (!this.pullUp) {

              return;

            }

            this.pulling = true;

            swiper.allowTouchMove = false; // 禁止触摸

            swiper.setTransition(swiper.params.speed);

            swiper.setTranslate(-(totalHeight + PULL_UP_HEIGHT - swiper.height));

            swiper.params.virtualTranslate = true; // 定住不给回弹

            this.$refs.pullUpLoading.setText(PULL_UP_TEXT_ING);

            this.$emit('pull-up', this.pullUpEnd);

          }

        }

     },

     pullDownEnd() {

        const swiper = this.$refs.swiper.swiper;

        this.pulling = false;

        this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_END);

        swiper.params.virtualTranslate = false;

        swiper.allowTouchMove = true;

        swiper.setTransition(swiper.params.speed);

        swiper.setTranslate(0);

     }

  }

}


</script>

<style lang="scss" scoped>

.swiper-container {

  overflow: hidden;

  width: 100%;

  height: 100%;

}


.swiper-slide {

  height: auto;

}

.mine-scroll-pull-up,

  .mine-scroll-pull-down {

    position: absolute;

    left: 0;

    width: 100%;

  }

  .mine-scroll-pull-down {

    bottom: 100%;

    height: 80px;

  }


  .mine-scroll-pull-up {

    top: 100%;

    height: 30px;

  }

</style>



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕夭夭 2020-03-10 18:14:43

同学你好,老师这边测试没有报错,可能是其他页面的问题。请同学查看私信,以便老师进一步帮助你解决。

祝学习愉快 !

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师