首页 > 其他分享 >记录--Vue3基于Grid布局简单实现一个瀑布流组件

记录--Vue3基于Grid布局简单实现一个瀑布流组件

时间:2023-11-06 18:57:54浏览次数:30  
标签:const -- props 网格 value Grid Vue3 grid ref

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

前言

在学习Grid布局之时,我发现其是CSS中的一种强大的布局方案,它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,在刷某书和某宝首页时,我们发现其展示方式就是一种瀑布流,是一种流行的网站页面布局,视觉表现为参差不齐的多栏布局,随着页面向下滚动,这种布局会不断加载数据块并附加到当前尾部。采用瀑布流布局的方式可以打破常规网站布局排版,给用户眼前一亮的新鲜感,更好的适应移动端。

因此结合二者,本文将通过grid布局简单实现一个瀑布流组件,该组件已开源上传npm,可以直接安装使用,Git地址在文尾。

实现效果:

实现原理

1、使用grid布局将页面分为无数个小网格,每个网格高度为1px。

.grid-content {
    display: grid;
    grid-auto-rows: minmax(1px, 1px);
    overflow: auto;
}

2、宽度根据需要自定义的列数自动分配。

   'grid-template-columns': `repeat(${props.columns}, 1fr)`,

3、根据每个卡片窗口的高度计算每个卡片需要跨越几个网格(因为每个网格设置高为1px,所以高度就是需要跨越的网格数)

'grid-row-end': `span ${gridItem.value.clientHeight - 1}`

4、监听瀑布流滚动事件,通过判断滚动条距离底部的高度,在滚动到底部一定距离时加载更多的数据,以实现无限滚动。

主要代码实现

gridContent 组件主要代码,循环展示每个条目,根据自定义的列展示不同的列数量,根据触底数据判断获取最新数据。监听传入的数据进行处理,目前只是做了简单处理,后面将通过虚拟列表的形式,动态处理该数据,以增加性能。

<template>
    <div class="grid-content" ref="gridContent" :style="gridStyle" @scroll="getMoreData">
        <grid-item v-for="item in showDataList" :key="item.dataIndex" :data="item">
            <template #slot-scope="slotProps">
                <slot name="slot-scope" :slotProps="slotProps"></slot>
            </template>
        </grid-item>
    </div>
</template>
<script lang="ts" setup>
import GridItem from './gridItem.vue';
import { ref, watch } from 'vue';

const props = defineProps({
    dataList: {
        type: Array,
        default: []
    },
    columns: {
        type: Number,
        default: 2
    },
    width: {
        type: Number,
        default: 300
    },
    height: {
        type: Number,
        default: 400
    },
    bottom:{
        type: Number,
        default: 50
    },
    loading:{
        type: Boolean,
        default: true
    }

})

const emit=defineEmits(['getMoreData']);

const gridStyle = ref({});
const showDataList = ref<any>([])

watch(() => props.dataList, (newValue) => {
    let tempData: any = [];
    newValue.forEach((item: any, index) => {
        tempData.push({ ...item, dataIndex: index })
    })
    showDataList.value = tempData;

    gridStyle.value = {
        'grid-template-columns': `repeat(${props.columns}, 1fr)`,
        width:props.width + 'px',
        height:props.height + 'px'
    }
}, { immediate: true,deep:true })

const isLoading=ref<boolean>(false);
watch(()=>props.loading,(newValue:boolean)=>{
    isLoading.value=newValue;
})

const gridContent=ref<any>(null);
//根据触底数据判断获取最新数据
const getMoreData=()=>{
    const scrollHeight = gridContent.value.scrollHeight || 0;
    const clientHeight = gridContent.value.clientHeight || 0;
    const scrollTop = gridContent.value.scrollTop || 0;
    if(scrollHeight - clientHeight - scrollTop < props.bottom && !isLoading.value){
        isLoading.value=true;
        emit('getMoreData');
    }
}
</script>
grid-item 组件代码,主要通过获取组件高度设置跨越的网格数,通过插槽展示每个卡片。
<template>
    <div class="grid-item" :style="itemStyle">
        <div ref="gridItem">
            <slot name="slot-scope" :data="data"></slot>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
defineProps({
    data: {
        type: Object,
        default: () => { }
    }
})

const gridItem = ref<any>(null);
const itemStyle = ref({})

onMounted(() => {
    itemStyle.value = { 'grid-row-end': `span ${gridItem.value.clientHeight - 1}` }
})

</script>
<style scoped> 
.grid-item {
    grid-row-end: span 100;
}
</style>

使用示例

npm install @fcli/vue-grid-waterfall --save-dev 来安装

在项目中使用
import VueGridWaterfall from '@fcli/vue-grid-waterfall';
const app=createApp(App)
app.use(VueGridWaterfall);

使用示例:

<template>
  <div class="content">
    <vue-grid-waterfall :data-list="dataList" :columns="3" @getMoreData="getMoreData" :loading="isLoading">
      <template #slot-scope="{ slotProps }">
        <div class="item" :style="{ height: slotProps.data.height, background: slotProps.data.color }">{{ slotProps.data.color
        }}</div>
      </template>
    </vue-grid-waterfall>
  </div>
</template>

<script setup lang="ts">
import vueGridWaterfall from './plugin/index.vue';
import { ref, onMounted } from 'vue'
component: {
  vueGridWaterfall
}

const dataList = ref<any>([]);
//获取随机颜色
const getRandomColor = () => {
  const getColor: any = (color: any) => {
    return (color += '0123456789abcdef'[Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : getColor(color);
  };
  return '#' + getColor('')
}

const getMoreData = () => {
  isLoading.value = true;
  getData()
}
const isLoading = ref(true);

//获取数据
const getData = () => {
  for (let i = 0; i < 100; i++) {
    dataList.value.push({ height: 50 + Math.random() * 50 + 'px', color: getRandomColor() })
  }
  setTimeout(()=>{
      isLoading.value = false;
  })
}

onMounted(() => {
  getData()
})
</script>

 

本文转载于:

https://juejin.cn/post/7280747572695973948

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 

标签:const,--,props,网格,value,Grid,Vue3,grid,ref
From: https://www.cnblogs.com/smileZAZ/p/17813431.html

相关文章

  • Pset_BuildingStoreyCommon
    Pset_BuildingStoreyCommonBuildingStoreyCommon:IfcBuildingStore所有实例定义的公共属性。请注意,几个建筑属性是在IfcBuildingStorey实例中直接处理的,建筑层数(或短名称)由IfcBuildngStore.name处理,建筑层名称(或长名称)由IFcBuildingStore.LongName处理,描述(或注释)由IccBuildingSt......
  • LeetCode222.完全二叉树的节点个数
    题目描述给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h层,则该层包含1~2h个节点。示例提交的代......
  • Jwt生成token详解
     publicStringcreateTokenByBao(StringuserId){Datedate=newDate();Stringtoken=Jwts.builder()//创建一个JWT构建器,用于创建和配置JWT。.setSubject(userId)//主题--生成token所需内容设置.setIssuedAt(date)//签发时间......
  • BUUCTF_Crypto_WriteUp | [AFCTF2018]Morse
    题目-..../.----/-..../-..../-..../...--/--.../....-/-..../-..../--.../-.../...--/.----/--.../...--/..---/--.../--.../....-/...../..-./--.../...--/...--/-----/...../..-./...--/...--/...--/....-/...--/...../--.../----./--.../-..分析一眼摩斯密码,在线解密得到字......
  • 第二次
    这个作业属于哪个课程<23软件技术基础>这个作业的目标<实现一个命令行文本计数统计程序>姓名-学号<耿赫阳><2021330301014>一、作业描述实现一个命令行文本计数统计程序。能正确统计导入的纯英文txt文本中的字符数,单词数,句子数。具体命令行界面要求举例:命......
  • qt 登录
    Qt设计精美的登录注册界面(包含SQLite数据库应用)_c++_桃豆豆-腾讯云开发者社区(csdn.net)......
  • 高等代数期中考寄录
    前言:复习了一周末的数学分析,高等代数碰都没碰。。。凉了。Day11.05这张卷子好多一眼丁真题啊,除了T3计算题有点复杂,不过答案应该没错吧(flag)。很快就还剩T10T11了。T10感觉好复杂啊,是不是要用行列式拆分?但是n项,拆个der。又想到升阶法,还是不行。捣鼓了半天(其实什么都没有研究出来),气......
  • Python如何写注释?
    python如何写给函数写注释:样板:defread_baseline(self,sheet='internal',category='ipe_flip',):"""功能:-从baseline的Excel文件中读取数据。参数:-sheet:要读取的sheet名称,默认为internal-catego......
  • CTFshow Reverse BJDCTF2020 Easy wp
    这个没有入口,只能一个个看,发现qes这个函数有一堆数,函数很小,逆向也容易,找到然后用BJD包裹#intqes()a=[0x7FFA7E31,0x224FC,0x884A4239,0x22A84,0x84FF235,0x3FF87,0x88424233,0x23185,0x7E4243F1,0x231FC]foriinrange(5):v6=(a[i*2]&0xffffffff)|((a[i*2+1]......
  • [机器学习复习笔记] Linear Regression 线性回归(最小二乘法求解析解)
    LinearRegression1.一元线性回归定义一个一次函数如下:\[y=\theta_0+\theta_1x\]其中\(\theta\)被称为函数的参数。显然在坐标图上,这个函数的图像是一条直线,这也是线性回归中的线性含义所在。只有一个\(x\)来预测\(y\),就是用一条直线来拟合数据,即一元线......