Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement infinite scroll in my vue js

I currently learning combining laravel with vue. This page should be fetching post data from server and showing in user timeline. I am successfully get all data and display it. But i want to implement a infinite scroll into it but i had no idea how to do it. i had been tried many different way also not working. Maybe is my knowledge about vue is still fresh. Any suggestion for me?

Here is my original code :jsfiddle

Here is code i try to implement infinite scroll with this example .

jsfiddle 2

The scrolling symbol is showing but seem like the array did't not passing , the data still appear all in one time.

Once submitted /feed the server will return array which contain post information. But i don't know how to passing into the list array.

Returned Array

In vue enter image description here

In route enter image description here

like image 848
masterhunter Avatar asked Apr 04 '17 04:04

masterhunter


People also ask

How do I add infinite scroll?

Method 1: Adding Infinite Scroll with Catch Infinite ScrollYou need to click on it to configure the plugin settings. To start with, you need to choose a trigger option for loading articles. You should select the 'Scroll' option to trigger autoload with scrolling.

How do you implement infinite scroll in backend?

Setting up backend First create a new Node project. Open up your terminal and create a new project folder. Install the dependencies, we need express module for now. Now we have a back end simulation ready which will keep on giving us data for any page number and any page size request.

How does Javascript infinite scroll work?

Infinite scrolling is a feature that allows you to load some pics on a website and then load more once the user reaches the end of the webpage (like we see on Pinterest). We will start off by creating three files, index. html , style. css , and app.


2 Answers

Installation:

npm install vue-infinite-scroll --save

Registration in your main.js:

// register globally
var infiniteScroll =  require('vue-infinite-scroll');
Vue.use(infiniteScroll)

// or for a single instance
var infiniteScroll = require('vue-infinite-scroll');
new Vue({
  directives: {infiniteScroll}
})

Your html:

<div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
  ...
</div>

You component:

var count = 0;

new Vue({
  el: '#app',
  data: {
    data: [],
    busy: false
  },
  methods: {
    loadMore: function() {
      this.busy = true;

      setTimeout(() => {
        for (var i = 0, j = 10; i < j; i++) {
          this.data.push({ name: count++ });
        }
        this.busy = false;
      }, 1000);
    }
  }
});
like image 101
Gabriel Robert Avatar answered Sep 19 '22 14:09

Gabriel Robert


I also tried the Vue-infinite-scroll, but it's not working properly when aligning with Vue-router, at least in my code. So I came up with my own solution.

<template>
  <div ref="loadmore" class="infinite-container">
      <!-- the inifite container -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      timer: null,
      // check if is in infinite procees
      busy: false
    }
  },
  methods: {
    infiniteScrollHandler() {
        //check if container's bttom is overflow screen
      let bottomOff =
        this.$refs.loadmore.getBoundingClientRect().bottom - screen.height;
      if (bottomOff < 10 && !this.busy) {
        console.log("loading... " + new Date());
        this.busy = true;
        // do something 
        this.busy = false;
      }
    },
    setIntervalTimer() {
      //check every 500 milliseconds
      this.timer = setInterval(() => {
        this.infiniteScrollHandler();
      }, 500);
    }
  },
  mounted() {
      // set up timer on mounted
    this.setIntervalTimer();
  },
  beforeDestroy() {
      // do not forget clear the interval timer
    clearInterval(this.timer);
  }
};
</script>
like image 24
Rui Wang Avatar answered Sep 16 '22 14:09

Rui Wang