瀑布流 封装组件

class Waterfall  {
	constructor(obj) {
		// 获取内容宽度
		this.el = document.querySelector(obj.el).offsetWidth
		// 获取所有的子元素
		this.oItems = document.getElementsByClassName(obj.elChren)
		// 列数
		this.columns = obj.columns
		// 间距
		this.gap = obj.gap
		this.setImgPos()
	}
		 setImgPos() {
			var item,
			oItemsLen = this.oItems.length,
			arr=[],minIdx=-1
			for (let i = 0; i < oItemsLen; i++) {
				item = this.oItems[i]
				// 均分每个元素的宽度
				item.style.width=((this.el - (this.columns - 1) * this.gap) / this.columns ) + 'px';
				// 第一排的元素
				if(i<this.columns){
					// 获取第一排的元素高度
					arr.push(item.offsetHeight);
					item.style.top = 0;
					item.style.left =i* ((this.el - (this.columns - 1) * this.gap) / this.columns+this.gap) + 'px';
				}else{
					// 获取第一排元素的最小高度
					minIdx = this.getMinIdx(arr)
					item.style.left=this.oItems[minIdx].offsetLeft+'px'
					item.style.top = (arr[minIdx]+this.gap)+'px'
					arr[minIdx]+= item.offsetHeight + this.gap
				}
			}
		}
		
		getMinIdx(arr){
			return [].indexOf.call(arr, Math.min.apply(null, arr))
		}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现瀑布流布局的方法有很多种,这里介绍一种使用Vue2封装瀑布流组件的方法。 首先,我们需要引入一个瀑布流插件。这里推荐使用Masonry.js(https://masonry.desandro.com/)。通过npm安装: ``` npm install masonry-layout ``` 然后在Vue组件中引入并初始化Masonry: ```javascript import Masonry from 'masonry-layout'; export default { name: 'WaterfallFlow', mounted () { this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, // 每列的宽度 gutter: 10, // 列与列之间的间距 fitWidth: true // 是否自适应容器宽度 }); }, methods: { reload () { this.masonry.reloadItems(); this.masonry.layout(); } } } ``` 接下来,我们需要在模板中渲染出瀑布流的布局。这里我们可以使用`v-for`指令来循环渲染出每个瀑布流项,并使用`ref`指令来获取瀑布流容器的DOM对象: ```html <template> <div class="waterfall-flow" ref="waterfallFlow"> <div class="waterfall-flow-item" v-for="(item, index) in items" :key="index"> <!-- 瀑布流项的内容 --> </div> </div> </template> ``` 最后,我们需要在组件中监听窗口大小的变化,并在变化时重新布局瀑布流。这里我们可以使用`window.resize`事件来监听窗口大小的变化,并在事件回调中调用`reload`方法重新布局瀑布流: ```javascript export default { name: 'WaterfallFlow', mounted () { // 初始化瀑布流 this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, gutter: 10, fitWidth: true }); // 监听窗口大小变化 window.addEventListener('resize', this.reload); }, beforeDestroy () { // 销毁瀑布流 this.masonry.destroy(); // 移除窗口大小变化监听 window.removeEventListener('resize', this.reload); }, methods: { reload () { // 重新布局瀑布流 this.masonry.reloadItems(); this.masonry.layout(); } } } ``` 这样就完成了一个简单的Vue2瀑布流组件封装。完整代码如下: ```html <template> <div class="waterfall-flow" ref="waterfallFlow"> <div class="waterfall-flow-item" v-for="(item, index) in items" :key="index"> <!-- 瀑布流项的内容 --> </div> </div> </template> <script> import Masonry from 'masonry-layout'; export default { name: 'WaterfallFlow', props: { items: { type: Array, default: () => [] } }, mounted () { // 初始化瀑布流 this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, gutter: 10, fitWidth: true }); // 监听窗口大小变化 window.addEventListener('resize', this.reload); }, beforeDestroy () { // 销毁瀑布流 this.masonry.destroy(); // 移除窗口大小变化监听 window.removeEventListener('resize', this.reload); }, methods: { reload () { // 重新布局瀑布流 this.masonry.reloadItems(); this.masonry.layout(); } } } </script> <style scoped> .waterfall-flow { width: 100%; } .waterfall-flow-item { width: 100%; margin-bottom: 10px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值