【简单好用,支持图片懒加载】 vue-waterfall2 基于Vue.js 瀑布流 懒加载 组件

vue-waterfall2

  • 1.不需知道元素宽高,可宽高自适应
  • 2.支持无图模式,内容自定义程度高
  • 3.支持懒加载(lazy-src)
  • 4.提供Event:loadmore (pc/android端滑动到底部触发,ios端需要上拉触发)
  • 5.使用极为简便,适用于PC/ios/android

有问题欢迎提issues、suggestions;Thank you for your Star !
welcome to my blog(JS/前端工程化/Python/算法) !!!

移动端效果

Demo

Common Demo
Lazyload Demo
Code Demo

GITHUB

npm i 
npm run dev

Installation

 npm i vue-waterfall2@latest --save

Usage

注意:

  1. gutterWidth需要与width一起使用才会生效,否则会进行自适应宽度(使用rem布局时,需先计算出自适应后的宽度再传值)
  2. 使用了waterfall的父组件,如果样式存在问题,可去掉css scoped尝试一下
main.js
import waterfall from 'vue-waterfall2'
Vue.use(waterfall)
app.vue(此组件 style不使用 scoped)
<template>
  <div class="container-water-fall">
    <div><button  @click="loadmore">loadmore</button> <button @click="mix">mix</button> <button @click="switchCol('5')">5列</button> <button @click="switchCol('8')">8列</button> <button @click="switchCol('10')">10列</button> </div>

    <waterfall :col='col' :width="itemWidth" :gutterWidth="gutterWidth"  :data="data"  @loadmore="loadmore"  @scroll="scroll"  >
      <template >
        <div class="cell-item" v-for="(item,index) in data">
          <img v-if="item.img" :src="item.img" alt="加载错误"  /> 
          <div class="item-body">
              <div class="item-desc">{{item.title}}</div>
              <div class="item-footer">
                  <div class="avatar" :style="{backgroundImage : `url(${item.avatar})` }"></div>
                  <div class="name">{{item.user}}</div>
                  <div class="like" :class="item.liked?'active':''" >
                      <i ></i>
                      <div class="like-total">{{item.liked}}</div>  
                  </div>
              </div>
          </div>
        </div>
      </template>
    </waterfall>
    
  </div>
</template>


/*
  注意:
  1. gutterWidth需要与width一起使用才会生效,否则会进行自适应宽度(使用rem布局时,需先计算出自适应后的宽度再传值)
  2. 使用了`waterfall`的父组件,如果样式存在问题,可去掉css `scoped`尝试一下
*/
import Vue from 'vue'
	export default{
	    data(){
	        return{
	            data:[],
	            col:5,
	        }
	    },
	    computed:{
	      itemWidth(){  
	            return (138*0.5*(document.documentElement.clientWidth/375))  #rem布局 计算宽度
	      },
	      gutterWidth(){
	            return (9*0.5*(document.documentElement.clientWidth/375))	#rem布局 计算x轴方向margin(y轴方向的margin自定义在css中即可)
	      }
	    },
	    methods:{
            scroll(scrollData){
                console.log(scrollData)
            },
	        switchCol(col){
	            this.col = col
	            console.log(this.col)
	      },
	      loadmore(index){
	            this.data = this.data.concat(this.data)
	      }
	    }
	}

属性

NameDefaultTypeDesc
col2Number列数
widthnullNumber宽度
gutterWidth10Number间隔的宽度
data[]Array数据
isTransitiontrueBoolean加载图片是否使用过渡动画
lazyDistance300Number图片进行懒加载的距离
loadDistance300Number上拉加载更多的距离

懒加载

对于需要使用懒加载的图片,需要使用lazy-src属性

<waterfall :col='col'   :data="data"     >
  <template>
     <img v-if="item.img" :lazy-src="item.img" alt="加载错误"  />
  </template>
</waterfall>

Events

NameDataDesc
loadmorenull滚动到底部触发
scrollobj获取滚动时的event对象

$waterfall API

this.$waterfall.forceUpdate()   //forceUpdate
  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
vue-waterfall2 是一个 Vue.js瀑布流组件,可以实现类似 Pinterest 的布局效果。要实现触底加载功能,需要在组件中监听滚动事件,判断滚动条是否到达底部,然后触发加载更多数据的方法。 以下是一个简单的示例代码: ```vue <template> <waterfall :list="list" :options="options" @scroll="handleScroll"></waterfall> </template> <script> import Waterfall from 'vue-waterfall2' export default { components: { Waterfall }, data() { return { list: [], // 列表数据 options: { // 瀑布流配置项 }, currentPage: 1, // 当前页码 isEnd: false, // 是否已经加载完所有数据 } }, mounted() { this.loadMore() }, methods: { loadMore() { if (this.isEnd) return // 发送请求获取数据,这里假设返回一个 Promise 对象 fetchData(this.currentPage).then((data) => { if (data.length) { this.list = this.list.concat(data) this.currentPage++ } else { this.isEnd = true } }) }, handleScroll(position) { // 监听滚动事件 const { maxY } = position if (maxY <= window.innerHeight) { this.loadMore() } }, }, } </script> ``` 在这个示例中,我们定义了一个 `list` 数组来存放列表数据,`options` 对象用来配置瀑布流效果。在 `mounted` 钩子函数中,我们首次加载数据。`loadMore` 方法用来加载更多数据,其中 `fetchData` 方法用来发送网络请求获取数据。在 `handleScroll` 方法中,我们监听了 `waterfall` 组件的滚动事件,当滚动到底部时触发加载更多数据的方法。`isEnd` 变量用来判断是否已经加载完所有数据,避免重复请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值