JSPM

  • Created
  • Published
  • Downloads 578
  • Score
    100M100P100Q92948F

Vue component for smooth scrolling, pull to refresh & infinite loading.

Package Exports

  • vue-scroller
  • vue-scroller/src/components/Scroller.vue

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-scroller) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Vue Scroller version

Vue component for smooth scrolling, pull to refresh & infinite loading.

Demo

How To Use

创建vue项目, 并安装插件

$ vue init webpack-simple#1.0 my-project
$ cd my-project
$ npm install
$ npm install vue-scroller

在webpack.config.js中添加resolve和loader

module.exports = {
  // ...
  
  resolve: {
    extensions: ['', '.js', '.vue'],
    fallback: [path.join(__dirname, './node_modules')]
  },

  // ...
  
  module: {
    loaders: [
      // ...
      
      {
        test: /vue-scroller.src.*?js$/,
        loader: 'babel'
      }
    ]
  },
  
  // ...

}

粘贴下面代码覆盖 App.vue

<template>
  <scroller :on-refresh="refresh"
            :on-infinite="loadMore"
            v-ref:my_scroller>
    <div v-for="(index, item) in items" @click="onItemClick(index, item)"
         class="row" :class="{'grey-bg': index % 2 == 0}">
      {{ item }}
    </div>
  </scroller>
</template>

<script>
  import {Scroller} from 'vue-scroller'

  export default {
    components: {
      Scroller
    },

    data () {
      return {
        items: []
      }
    },

    ready() {

      for (let i = 1; i <= 20; i++) {
        this.items.push(i + ' - keep walking, be 2 with you.')
      }
      this.top = 1
      this.bottom = 20

      setTimeout(() => {
        /* 下面2种方式都可以调用 resize 方法 */

        // $scrollerDelegate.resize()

        this.$refs.my_scroller.resize()
      })
    },

    methods: {
      refresh() {
        setTimeout(() => {
          let start = this.top - 1

          for (let i = start; i > start - 10; i--) {
            this.items.splice(0, 0, i + ' - keep walking, be 2 with you.')
          }

          this.top = this.top - 10;

          /* 下面3种方式都可以调用 finishPullToRefresh 方法 */

          this.$broadcast('$finishPullToRefresh')
          // $scrollerDelegate.finishPullToRefresh()
          // this.$refs.my_scroller.finishPullToRefresh()

        }, 1500)
      },

      loadMore() {
        setTimeout(() => {

          let start = this.bottom + 1

          for (let i = start; i < start + 10; i++) {
            this.items.push(i + ' - keep walking, be 2 with you.')
          }

          this.bottom = this.bottom + 10;


          setTimeout(() => {
            $scrollerDelegate.resize()
          })
        }, 1500)
      },

      onItemClick(index, item) {
        console.log(index)
      }
    }

  }
</script>

<style>

  .row {
    width: 100%;
    height: 50px;
    padding: 10px 0;
    font-size: 16px;
    line-height: 30px;
    text-align: center;
    color: #444;
    background-color: #fff;
  }

  .grey-bg {
    background-color: #eee;
  }

</style>

在index.html中添加

<meta name="viewport" content="width=device-width, user-scalable=no">

运行

$ npm run dev

That's it, have fun.