zoukankan      html  css  js  c++  java
  • IntersectionObserver 实现无限滚动

    observerInfiniteScroll.vue

    <template>
      <div>
        <ul>
          <li class="list-item" v-for="item in items" :key="item.id">{{item.title}}</li>
        </ul>
        <Observer @intersect="intersected" />
      </div>
    </template>
     <script>
    import Observer from 'src/components/observer/observer';
    import axios from 'axios';
    export default {
      data: () => ({ page: 1, items: [] }),
      components: {
        Observer,
      },
      methods: {
        async intersected() {
          await axios
            .get('https://cnodejs.org/api/v1/topics', {
              params: {
                page: this.page,
                limit: 20,
              },
            })
            .then(res => {
              this.page++;
              this.items = [...this.items, ...res.data.data];
            });
        },
      },
    };
    </script>
    <style lang="scss">
    .list-item {
      padding: 0.3rem;
    }
    </style>
    

    observer.vue

    <template>
      <div class="observer" />
    </template>
    <script>
    export default {
      props: ['options'],
      data: () => ({ observer: null }),
      mounted() {
        const options = this.options || {};
        this.observer = new IntersectionObserver(([entry]) => {
          if (entry && entry.isIntersecting) {
            this.$emit('intersect');
          }
        }, options);
        this.observer.observe(this.$el);
      },
      destroyed() {
        this.observer.disconnect();
      },
    };
    </script>
    

    .

  • 相关阅读:
    windows cmd command line 命令
    windows cmd color setup
    二进制中1的个数
    斐波拉契数列
    旋转数组的最小数字
    用两个栈实现队列
    重建二叉树
    Bootstrap学习笔记(三) 网格系统
    Bootstrap学习笔记(二) 表单
    Bootstrap学习笔记(一) 排版
  • 原文地址:https://www.cnblogs.com/crazycode2/p/12221460.html
Copyright © 2011-2022 走看看