Coder Social home page Coder Social logo

staticdeng / vuejs-loadmore Goto Github PK

View Code? Open in Web Editor NEW
79.0 3.0 12.0 765 KB

A pull-down refresh and pull-up loadmore scroll component for Vue.js. Vue上拉加载下拉刷新组件

License: MIT License

JavaScript 40.18% HTML 2.39% Vue 43.26% SCSS 14.17%
vue-refresh vue-loadmore vue-scroll vue-scroller refresh pull-refresh loadmore scroll infinite-scroll infinite

vuejs-loadmore's Introduction

vuejs-loadmore

Npm Version Build Status

NPM

一个Vue.js 的下拉刷新和上拉加载组件。

通过提供简单的api易于使用。与其他组件库不同,它使用浏览器本身而不是js来作滚动容器,因此它的代码量更小,但不损失用户体验。

中文 | English

预览

在线demo

也可以扫描以下二维码访问演示:

安装 & 使用

安装 npm 包

# npm
npm install vuejs-loadmore --save

全局导入

import Vue from 'vue';
import VueLoadmore from 'vuejs-loadmore';

Vue.use(VueLoadmore);

国际化支持

支持中文 zh-CN 和英文 en-US, 默认为 zh-CN。

import VueLoadmore from 'vuejs-loadmore';

Vue.use(VueLoadmore, {
  lang: 'en-US'
})

你也可以使用 locale.use() 指定语言。

import VueLoadmore, { locale } from 'vuejs-loadmore';

Vue.use(VueLoadmore);
locale.use('en-US');

用法

基础用法

<vue-loadmore 
  :on-refresh="onRefresh" 
  :on-loadmore="onLoad"
  :finished="finished">
  <div v-for="(item, i) of list" :key="i"></div>
</vue-loadmore>

on-refreshon-loadmore 会在下拉刷新或滚动到底部时触发,需要在处理完数据请求后执行回调函数 done()

如果你不需要刷新,只需要不绑定on-refresh

当数据请求完成后,你可以将finished的数据改为true,这样就会显示没有更多了

export default {
  data() {
    return {
      list: [],
      page: 1,
      pageSize: 10,
      finished: false
    };
  },
  mounted() {
    this.fetch();
  },
  methods: {
    onRefresh(done) {
      this.list = [];
      this.page = 1;
      this.finished = false;
      this.fetch();

      done();
    },

    onLoad(done) {
      if (this.page <= 10) {
        this.fetch();
      } else {
        // all data loaded
        this.finished = true;
      }
      done();
    },

    fetch() {
      for (let i = 0; i < this.pageSize; i++) {
        this.list.push(this.list.length + 1);
      }
      this.page++;
    }
  },
}

错误提示

<vue-loadmore 
  :on-loadmore="onLoad"
  :finished="finished"
  :error.sync="error">
  <div v-for="(item, i) of list" :key="i"></div>
</vue-loadmore>
export default {
  data() {
    return {
      list: [],
      finished: false,
      error: false,
    };
  },
  methods: {
    onLoad() {
      fetchSomeThing().catch(() => {
        this.error = true;
      });
    },
  },
};

API

Props

Attribute Description Type Default
on-refresh 顶部下拉触发 function -
pulling-text 下拉显示文本 string 下拉刷新
loosing-text 释放显示文本 string 释放刷新
refresh-text 正在刷新显示文本 string 正在刷新
success-text 刷新完成显示文本 string 刷新完成
show-success-text 是否显示success-text boolean true
pull-distance 触发正在刷新状态的距离 number | string 50
head-height 正在刷新显示区域的高度 number | string 50
animation-duration 下拉刷新动画持续时间 number | string 200
on-loadmore 滚动到底部触发 function -
immediate-check 是否立即触发数据加载;默认是,否的话则自己定义触发数据加载时机 boolean true
load-offset 当滚动条到底部的距离小于 load-offset 时,会发出 on-loadmore number | string 50
finished 数据是否加载完毕,改变为true,则会显示finished-text boolean false
error 数据是否加载错误,on-loadmore只有在点击错误文本时才会触发,需要sync修饰符 boolean false
loading-text 滚动到底部正在加载显示文本 string 正在加载
finished-text 滚动到底部加载完毕的显示文本 string 没有更多了
error-text 加载错误显示文本 string 请求失败,点击重新加载

方法

使用 ref 获取 List 实例并调用实例方法。

Name Description
checkScroll 检查当前的滚动位置,若已滚动至底部,则会触发 on-loadmore

例子

查看demo以快速了解如何使用此包。

git clone [email protected]:staticdeng/vuejs-loadmore.git
yarn install
yarn example:dev

vuejs-loadmore's People

Contributors

staticdeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vuejs-loadmore's Issues

ERROR in xxx.js from UglifyJs Unexpected token: name (hook)

When I use this component library, it runs normally local development, but an error is reported when using uglifyjs webpack plugin for webpack packaging.

The following is the detailed error reporting information:

ERROR in js/vendors.bc580846f352f4ad3188.js from UglifyJs Unexpected token: name (hook) [js/vendors.bc580846f352f4ad3188.js:56427,10] Child extract-text-webpack-plugin ../../../node_modules/extract-text-webpack-plugin/dist ../../../node_modules/css-loader/index.js!../../../node_modules/postcss-loader/lib/index.js??ref--1-3!../../../node_modules/less-loader/dist/cjs.js!../stylesheets/application.less:

vue3支持

东西很不错,能支持vue3吗?

To bad I can not get this to work in Vue 3

Trying to use this in Vue 3 gives errors about no default import from vue and once you work around that, it gives other errors about missing things as well.

A proper example of how to use this in Vue 3 ( with <script setup> syntax ) would be great.

load-offset not working

onLoadMore does not work when load-offset > 500, and works normally when load-offset < 500. I dont' understand why.
Please help me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.