vue-waterfall-easy

repository·master·Indexed 21 days ago

https://github.com/lfyfly/vue-waterfall-easy

A Vue.js component for creating responsive waterfall flow layouts with built-in infinite scroll loading. It automatically determines image dimensions via preloading, removing the need for manual width/height data. Features include customizable gaps, column limits, router-link support, and slots for custom loading and head/footer content. Version 2.4.4.

Tokens
4.4K
Snippets
16
Records
19
Agent score
70%

What's inside vue-waterfall-easy

  1. Customize the waterfall using slots

    master

    The component provides several slots for customization:

    • default: Custom picture description element. Provides props.index and props.value (the item from imgsArr).
    • loading: Custom loading element. Provides { isFirstLoad }.
    • waterfall-head: Content to be placed at the top of the waterfall container.
    • waterfall-over: Content to be shown when the waterfallOver method is called.
    <!-- Default Slot Example -->
    <vue-waterfall-easy :imgsArr="imgsArr" @scrollReachBottom="getData">
      <div class="img-info" slot-scope="props">
        <p class="some-info">picture index: {{props.index}}</p>
        <p class="some-info">{{props.value.info}}</p>
      </div>
    </vue-waterfall-easy
    
    <!-- Loading Slot Example -->
    <div slot="loading" slot-scope="{isFirstLoad}">
      <div v-if="isFirstLoad">first-loading...</div>
      <div v-else>loading...</div>
    </div>
    
    <!-- Waterfall Over Slot Example -->
    <div slot="waterfall-over">No more data</div>
  2. Import vue-waterfall-easy via script tags

    master

    For non-module environments, include Vue and the vueWaterfallEasy.js script in your HTML. Then register the component in your Vue instance.

    <script src="path/to/vue/vue.js"></script>
    <script src="path/to/vueWaterfallEasy.js"></script>
    
    <script>
    new Vue({
      el: '#app',
      components: {
        vueWaterfallEasy
      }
    })
    </script>
  3. Ensure correct CSS for container height

    master

    Because the component is responsive and relies on parent dimensions, ensure that the ancestor elements (including html, body, and #app) have defined heights. If the component is not set to a fixed width, its parent must be 100% of the viewport width.

    html, body, #app {
      height: 100%;
      width: 100%;
    }
  4. Build and run the vue_origin project

    master

    This project uses standard Vue.js build scripts. You can install dependencies, start a development server with hot reloading, or build the project for production using the following commands.

    # install dependencies
    npm install
    
    # serve with hot reload at localhost:8080
    npm run dev
    
    # build for production with minification
    npm run build
    
    # build for production and view the bundle analyzer report
    npm run build --report
  5. Import vue-waterfall-easy via scripts

    master

    If you are not using a module bundler, download vueWaterfallEasy.js and include it via script tags after Vue.js:

    <script src="path/to/vue/vue.js"></script>
    <script src="path/to/vueWaterfallEasy.js"></script>
    
    <script>
    new Vue({
      el: '#app',
      components: {
        vueWaterfallEasy
      }
    })
    </script>
  6. Implement infinite scroll with vue-waterfall-easy

    master

    The component supports infinite scrolling via the @scrollReachBottom event.

    Important Data Handling Patterns:

    • Incremental Update (Recommended): When fetching new data, use .concat() to merge the new items with the existing imgsArr. This is more performant.
    • Replacement Update: Overwriting the entire imgsArr with new data is possible but less efficient for continuous scrolling.

    To ensure mobile responsiveness, add this meta tag to your index.html <head>: <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <vue-waterfall-easy :imgsArr="imgsArr" @scrollReachBottom="getData"></vue-waterfall-easy>
    export default {
      data() {
        return {
          imgsArr: [],
          group: 0,
        }
      },
      methods: {
        getData() {
          axios.get('./static/mock/data.json?group=' + this.group)
            .then(res => {
              // Incremental update pattern
              this.imgsArr = this.imgsArr.concat(res.data)
              this.group++
            })
        },
      },
      created() {
        this.getData()
      }
    }
  7. Configure vue-waterfall-easy component props

    master

    The following props are available to customize the waterfall layout:

    PropTypeDefaultDescription
    imgsArrArray[]Required. Array of objects. Each object must have src and href properties.
    srcKeyString'src'Key name for the image source if not 'src'.
    hrefKeyString'href'Key name for the link if not 'href'.
    widthNumber-Container width (px). If fixed, this must be set. If not set, defaults to 100% of parent.
    heightNumber/String-Container height. If not set, defaults to 100% of parent (parent must have height).
    gapNumber20Spacing between images on PC (px).
    mobileGapNumber8Spacing between images on mobile (px).
    imgWidthNumber240Image width (px).
    maxColsNumber5Maximum number of columns.
    linkRangeString'card'Clickable area: 'card' (whole card), 'img' (image only), or 'custom' (via slot).
    isRouterLinkBooleanfalseIf true, uses router-link instead of <a> tags.
    reachBottomDistanceNumber0Distance from bottom (px) to trigger scrollReachBottom event.
    loadingDotCountNumber3Number of dots in loading animation.
    loadingDotStyleObjectnullCustom style for loading dots.
    loadingTimeOutNumber500If preload < this value (ms), loading animation is hidden.
    cardAnimationClassString'default-card-animation'CSS class for entry animation. Set to "" to disable.
    enablePullDownEventBooleanfalseEnables pull-down events.
  8. Configure vue-waterfall-easy props

    master

    Use the following props to customize the waterfall layout and behavior:

    PropTypeDefaultDescription
    imgsArrArray[]Required. Array of objects. Each must have src and href.
    srcKeyString'src'Key name for the image source if not src.
    hrefKeyString'href'Key name for the link if not href.
    widthNumber-Container width (px). If using responsive layout, parent must be 100% width. If fixed, you must set this prop.
    heightNumber|String-Container height. If not specified, defaults to 100% of parent height.
    gapNumber20Space between pictures on PC (px).
    mobileGapNumber8Space between pictures on Mobile (px).
    imgWidthNumber240Width of the picture (px).
    maxColsNumber5Maximum number of columns.
    linkRangeString'card'Click trigger range: 'card' (whole card), 'img' (image only), or 'custom' (via slots).
    isRouterLinkBooleanfalseIf true, renders router-link instead of <a> tag.
    reachBottomDistanceNumber0Distance (px) from bottom to trigger @scrollReachBottom.
    loadingDotCountNumber3Number of dots in loading animation.
    loadingDotStyleObjectnullStyle object for loading dots.
    loadingTimeOutNumber500If preloading < this ms, don't show loading animation.
    cardAnimationClassString'default-card-animation'Animation class name for the card.
    enablePullDownEventBooleanfalseEnable drop-down events.