ホームページ ウェブフロントエンド jsチュートリアル vue2にプルアップロード機能を実装する方法

vue2にプルアップロード機能を実装する方法

Jun 23, 2018 pm 06:02 PM
vue vue2 プルアップローディング

この記事では、主に vue2 に基づくプルアップ読み込み関数を詳しく紹介します。興味のある方は参考にしてください。

この記事のサンプルでは、​​プルアップを実装するための具体的なコードを共有します。ローディング表示。参考までに、具体的な内容は次のとおりです

私たちのプロジェクトでもスワイパーを使用しているためです。それらの多くは切り替えのためにスライドしますが、ロードするにはプルアップする必要があるため、私たちが使用した多くの UI フレームワークにはさまざまなバグがあり、最終的には 1 つを作成するしかありませんでした。コードは次のとおりです (これは多くの場所で使用されるため、components/common の下に配置することをお勧めします):

<template>
  <p class="loadmore">
    <slot></slot>
    <slot name="bottom">
    </slot>
  </p>
</template>

<style>
  .loadmore{
    width:100%;
  }
</style>

<script>
  export default {
    name: &#39;loadmore&#39;,
    props: {
      maxDistance: {
        type: Number,
        default: 0
      },
      autoFill: {
        type: Boolean,
        default: true
      },
      distanceIndex: {
        type: Number,
        default: 2
      },
      bottomPullText: {
        type: String,
        default: &#39;上拉刷新&#39;
      },
      bottomDropText: {
        type: String,
        default: &#39;释放更新&#39;
      },
      bottomLoadingText: {
        type: String,
        default: &#39;加载中...&#39;
      },
      bottomDistance: {
        type: Number,
        default: 70
      },
      bottomMethod: {
        type: Function
      },
      bottomAllLoaded: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        // 最下面出现的p的位移
        translate: 0,
        // 选择滚动事件的监听对象
        scrollEventTarget: null,
        containerFilled: false,
        bottomText: &#39;&#39;,
        // class类名
        bottomDropped: false,
        // 获取监听滚动元素的scrollTop
        bottomReached: false,
        // 滑动的方向  down---向下互动;up---向上滑动
        direction: &#39;&#39;,
        startY: 0,
        startScrollTop: 0,
        // 实时的clientY位置
        currentY: 0,
        topStatus: &#39;&#39;,
        // 上拉加载的状态  &#39;&#39;   pull: 上拉中
        bottomStatus: &#39;&#39;,
      };
    },
    watch: {
      // 改变当前加载在状态
      bottomStatus(val) {
        this.$emit(&#39;bottom-status-change&#39;, val);
        switch (val) {
          case &#39;pull&#39;:
            this.bottomText = this.bottomPullText;
            break;
          case &#39;drop&#39;:
            this.bottomText = this.bottomDropText;
            break;
          case &#39;loading&#39;:
            this.bottomText = this.bottomLoadingText;
            break;
        }
      }
    },
    methods: {
      onBottomLoaded() {
        this.bottomStatus = &#39;pull&#39;;
        this.bottomDropped = false;
        this.$nextTick(() => {
          if (this.scrollEventTarget === window) {
          document.body.scrollTop += 50;
        } else {
          this.scrollEventTarget.scrollTop += 50;
        }
        this.translate = 0;
      });
        // 注释
        if (!this.bottomAllLoaded && !this.containerFilled) {
          this.fillContainer();
        }
      },

      getScrollEventTarget(element) {
        let currentNode = element;
        while (currentNode && currentNode.tagName !== &#39;HTML&#39; &&
        currentNode.tagName !== &#39;BODY&#39; && currentNode.nodeType === 1) {
          let overflowY = document.defaultView.getComputedStyle(currentNode).overflowY;
          if (overflowY === &#39;scroll&#39; || overflowY === &#39;auto&#39;) {
            return currentNode;
          }
          currentNode = currentNode.parentNode;
        }
        return window;
      },
      // 获取scrollTop
      getScrollTop(element) {
        if (element === window) {
          return Math.max(window.pageYOffset || 0, document.documentElement.scrollTop);
        } else {
          return element.scrollTop;
        }
      },
      bindTouchEvents() {
        this.$el.addEventListener(&#39;touchstart&#39;, this.handleTouchStart);
        this.$el.addEventListener(&#39;touchmove&#39;, this.handleTouchMove);
        this.$el.addEventListener(&#39;touchend&#39;, this.handleTouchEnd);
      },
      init() {
        this.bottomStatus = &#39;pull&#39;;
        // 选择滚动事件的监听对象
        this.scrollEventTarget = this.getScrollEventTarget(this.$el);
        if (typeof this.bottomMethod === &#39;function&#39;) {
          // autoFill 属性的实现  注释
          this.fillContainer();
          // 绑定滑动事件
          this.bindTouchEvents();
        }
      },
      // autoFill 属性的实现  注释
      fillContainer() {
        if (this.autoFill) {
          this.$nextTick(() => {
            if (this.scrollEventTarget === window) {
            this.containerFilled = this.$el.getBoundingClientRect().bottom >=
                document.documentElement.getBoundingClientRect().bottom;
          } else {
            this.containerFilled = this.$el.getBoundingClientRect().bottom >=
                this.scrollEventTarget.getBoundingClientRect().bottom;
          }
          if (!this.containerFilled) {
            this.bottomStatus = &#39;loading&#39;;
            this.bottomMethod();
          }
        });
        }
      },
      // 获取监听滚动元素的scrollTop
      checkBottomReached() {
        if (this.scrollEventTarget === window) {
          return document.body.scrollTop + document.documentElement.clientHeight >= document.body.scrollHeight;
        } else {
          // getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。 right是指元素右边界距窗口最左边的距离,bottom是指元素下边界距窗口最上面的距离。
          return this.$el.getBoundingClientRect().bottom <= this.scrollEventTarget.getBoundingClientRect().bottom + 1;
        }
      },
      // ontouchstart 事件
      handleTouchStart(event) {
        // 获取起点的y坐标
        this.startY = event.touches[0].clientY;
        this.startScrollTop = this.getScrollTop(this.scrollEventTarget);
        this.bottomReached = false;
        if (this.bottomStatus !== &#39;loading&#39;) {
          this.bottomStatus = &#39;pull&#39;;
          this.bottomDropped = false;
        }
      },
      // ontouchmove事件
      handleTouchMove(event) {
        if (this.startY < this.$el.getBoundingClientRect().top && this.startY > this.$el.getBoundingClientRect().bottom) {
          // 没有在需要滚动的范围内滚动,不再监听scroll
          return;
        }
        // 实时的clientY位置
        this.currentY = event.touches[0].clientY;
        // distance 移动位置和开始位置的差值    distanceIndex---
        let distance = (this.currentY - this.startY) / this.distanceIndex;
        // 根据 distance 判断滑动的方向 并赋予变量  direction down---向下互动;up---向上滑动
        this.direction = distance > 0 ? &#39;down&#39; : &#39;up&#39;;
        if (this.direction === &#39;up&#39;) {
          // 获取监听滚动元素的scrollTop
          this.bottomReached = this.bottomReached || this.checkBottomReached();
        }
        if (typeof this.bottomMethod === &#39;function&#39; && this.direction === &#39;up&#39; &&
            this.bottomReached && this.bottomStatus !== &#39;loading&#39; && !this.bottomAllLoaded) {
          // 有加载函数,是向上拉,有滚动距离,不是正在加载ajax,没有加载到最后一页
          event.preventDefault();
          event.stopPropagation();
          if (this.maxDistance > 0) {
            this.translate = Math.abs(distance) <= this.maxDistance
                ? this.getScrollTop(this.scrollEventTarget) - this.startScrollTop + distance : this.translate;
          } else {
            this.translate = this.getScrollTop(this.scrollEventTarget) - this.startScrollTop + distance;
          }
          if (this.translate > 0) {
            this.translate = 0;
          }
          this.bottomStatus = -this.translate >= this.bottomDistance ? &#39;drop&#39; : &#39;pull&#39;;
        }
      },
      // ontouchend事件
      handleTouchEnd() {
        if (this.direction === &#39;up&#39; && this.bottomReached && this.translate < 0) {
          this.bottomDropped = true;
          this.bottomReached = false;
          if (this.bottomStatus === &#39;drop&#39;) {
            this.translate = &#39;-50&#39;;
            this.bottomStatus = &#39;loading&#39;;
            this.bottomMethod();
          } else {
            this.translate = &#39;0&#39;;
            this.bottomStatus = &#39;pull&#39;;
          }
        }
        this.direction = &#39;&#39;;
      }
    },
    mounted() {
      this.init();
    }
  };
</script>
ログイン後にコピー

次に、必要なページにインポートします: import LoadMore from './../common/ loadmore.vue' ; あなたが彼を紹介する必要があるページは次のように書かれています:

<template>
 <section class="finan">
  <!-- 上拉加载更多 -->
  <load-more
  :bottom-method="loadBottom"
  :bottom-all-loaded="allLoaded"
  :bottomPullText=&#39;bottomText&#39;
  :auto-fill="false"
  @bottom-status-change="handleBottomChange"
  ref="loadmore">
    <p>
  这里写你需要的另外的模块
    </p>
    <p v-show="loading" slot="bottom" class="loading"> 这个p是为让上拉加载的时候显示一张加载的gif图
     <img src="./../../assets/main/uploading.gif">
    </p>
  </load-more>
 </section>
</template>
ログイン後にコピー

次に、このページに次のようにデータとメソッドを設定します:

  export default {
    name: &#39;FinancialGroup&#39;,
    props:{
 
    },
    data () {
      return {
        // 上拉加载数据
        scrollHeight: 0,
        scrollTop: 0,
        containerHeight: 0,
        loading: false,
        allLoaded: false,
        bottomText: &#39;上拉加载更多...&#39;,
        bottomStatus: &#39;&#39;,
        pageNo: 1,
        totalCount: &#39;&#39;,
      }
    },
    methods: {
    /* 下拉加载 */
    _scroll: function(ev) {
      ev = ev || event;
      this.scrollHeight = this.$refs.innerScroll.scrollHeight;
      this.scrollTop = this.$refs.innerScroll.scrollTop;
      this.containerHeight = this.$refs.innerScroll.offsetHeight;
    },
    loadBottom: function() {
      this.loading = true;
      this.pageNo += 1;  // 每次更迭加载的页数
      if (this.pageNo == this.totalGetCount) {
        // 当allLoaded = true时上拉加载停止
        this.loading = false;
        this.allLoaded = true;
      }
      api.commonApi(后台接口,请求参数) 这个api是封装的axios有不懂的可以看vue2+vuex+axios那篇文章
          .then(res => {
        setTimeout(() => {
      要使用的后台返回的数据写在setTimeout里面
         this.$nextTick(() => {
          this.loading = false;
        })
      }, 1000)
     });
    },
    handleBottomChange(status) {
      this.bottomStatus = status;
    },
  }
ログイン後にコピー

以上が私が皆さんのためにまとめたものです。将来すべての人に役立ちます。

関連記事:

JavaScriptを使用して寄生組み合わせ継承を実装する方法

JSで非ファーストスクリーン画像の遅延読み込みを実装する方法

jQueryのライブラリの参照メソッドとは何ですか

jsでWord画像を生成する方法

以上がvue2にプルアップロード機能を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

vueでechartを使用する方法 vueでechartを使用する方法 May 09, 2024 pm 04:24 PM

Vue で ECharts を使用すると、アプリケーションにデータ視覚化機能を簡単に追加できます。具体的な手順には、ECharts および Vue ECharts パッケージのインストール、ECharts の導入、チャート コンポーネントの作成、オプションの構成、チャート コンポーネントの使用、Vue データに対応したチャートの作成、対話型機能の追加、および高度な使用法の使用が含まれます。

vue におけるエクスポートのデフォルトの役割 vue におけるエクスポートのデフォルトの役割 May 09, 2024 pm 06:48 PM

質問: Vue におけるエクスポートのデフォルトの役割は何ですか?詳細説明: エクスポートデフォルトは、コンポーネントのデフォルトのエクスポートを定義します。インポートすると、コンポーネントが自動的にインポートされます。インポートプロセスを簡素化し、明確さを改善し、競合を防ぎます。一般に、名前付きエクスポートとデフォルト エクスポートの両方を使用して、個々のコンポーネントをエクスポートし、グローバル コンポーネントを登録するために使用されます。

vueでのmap関数の使い方 vueでのmap関数の使い方 May 09, 2024 pm 06:54 PM

Vue.js マップ関数は、各要素が元の配列の各要素の変換結果である新しい配列を作成する組み込みの高階関数です。構文は、map(callbackFn) です。callbackFn は、配列内の各要素を最初の引数として受け取り、オプションでインデックスを 2 番目の引数として受け取り、値を返します。 map 関数は元の配列を変更しません。

vueのeventと$eventの違い vueのeventと$eventの違い May 08, 2024 pm 04:42 PM

Vue.js では、event はブラウザによってトリガーされるネイティブ JavaScript イベントですが、$event は Vue コンポーネントで使用される Vue 固有の抽象イベント オブジェクトです。 $event はデータ バインディングをサポートするようにフォーマットおよび拡張されているため、一般に $event を使用する方が便利です。ネイティブ イベント オブジェクトの特定の機能にアクセスする必要がある場合は、event を使用します。

vueのエクスポートとデフォルトのエクスポートの違い vueのエクスポートとデフォルトのエクスポートの違い May 08, 2024 pm 05:27 PM

Vue.js でモジュールをエクスポートするには、エクスポートとデフォルトのエクスポートの 2 つの方法があります。 export は名前付きエンティティのエクスポートに使用され、中括弧の使用が必要です。export default はデフォルト エンティティのエクスポートに使用され、中括弧は必要ありません。インポートする場合、エクスポートによってエクスポートされたエンティティはその名前を使用する必要がありますが、エクスポートのデフォルトによってエクスポートされたエンティティは暗黙的に使用できます。複数回インポートする必要があるモジュールにはデフォルトのエクスポートを使用し、一度だけエクスポートするモジュールにはエクスポートを使用することをお勧めします。

Vue における onmounted の役割 Vue における onmounted の役割 May 09, 2024 pm 02:51 PM

onMounted は、Vue のコンポーネント マウント ライフ サイクル フックです。その機能は、コンポーネントが DOM にマウントされた後に、DOM 要素への参照の取得、データの設定、HTTP リクエストの送信、イベント リスナーの登録などの初期化操作を実行することです。コンポーネントが更新された後、またはコンポーネントが破棄される前に操作を実行する必要がある場合は、他のライフサイクル フックを使用できます。

Vueのフックとは何ですか Vueのフックとは何ですか May 09, 2024 pm 06:33 PM

Vue フックは、特定のイベントまたはライフサイクル ステージでアクションを実行するコールバック関数です。これらには、ライフサイクル フック (beforeCreate、mounted、beforeDestroy など)、イベント処理フック (クリック、入力、キーダウンなど)、およびカスタム フックが含まれます。フックはコンポーネントの制御を強化し、コンポーネントのライフサイクルに対応し、ユーザーの操作を処理し、コンポーネントの再利用性を向上させます。フックを使用するには、フック関数を定義し、ロジックを実行してオプションの値を返すだけです。

vue のイベント修飾子はどのようなシナリオに使用できますか? vue のイベント修飾子はどのようなシナリオに使用できますか? May 09, 2024 pm 02:33 PM

Vue.js イベント修飾子は、次のような特定の動作を追加するために使用されます。 デフォルト動作の防止 (.prevent) イベント バブリングの停止 (.stop) ワンタイム イベント (.once) イベントのキャプチャ (.capture) パッシブ イベント リスニング (.passive) アダプティブ修飾子 (.self)キー修飾子 (.key)

See all articles