当前位置: 首页 > news >正文

图片上加语音 网站开发ui培训学费

图片上加语音 网站开发,ui培训学费,前端培训出来工资多少,近期重大新闻事件瀑布流 - Vue3基于Grid布局简单实现一个瀑布流组件 前言 在学习Grid布局之时,我发现其是CSS中的一种强大的布局方案,它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,在刷某书和某宝首页时&…

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

前言

在学习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>
属性属性名称类型可选值
dataList瀑布流列表数据Array[]
columns展示的列数number2
width瀑布流宽度number0
height瀑布流高度number0
bottom滚动到底部触发加载数据的距离number50
loading是否加在载数据boolean加载数据完数据设为false
#slot-scope插槽objectslotProps.data

最后

本组件为初步实现,后续还会结合虚拟滚动,减少dom渲染,当数据量过大时提升性能,欢迎star关注。

Git地址:https://gitee.com/fcli/vue-grid-waterfall.git

http://www.yayakq.cn/news/380340/

相关文章:

  • 网站 dns 解析 手机访问设置秦皇岛平台公司
  • 网站导航条专门做页面跳转报名工具小程序官网
  • 自己做网站别人怎么看见如何做 行业社交类网站
  • 泉州手机网站建设价格安徽省建设总站网站
  • 淘宝客网站需要多大空间南宁市住房和城乡建设局网站
  • 网站做外链手机系统优化工具
  • 官方网站建设哪儿有为知笔记导入wordpress
  • 资阳做网站公司关键词排名优化
  • 下载了模板如何做网站中国平面设计网站
  • 万户网站协作管理系统做网站知名的学习网站
  • 网站建站系统程序社区网站建设费用
  • 网站备案申请流程有没有专门做蛋糕的网站
  • 网站建设越来越注重用户体验现在网站前台用什么做
  • 设计公司网站首页显示微信怎样建网站
  • 怎么做一淘宝客网站吗请别人做网站
  • 有什么网站做统计图的wordpress 手动安装
  • php企业门户网站模板成都住建局官网登陆
  • 做网站犯法新洲建设局网站
  • 推广产品网站建设爱网站在线观看视频
  • 信誉好的昆明网站建设asp wordpress
  • 杭州网站开发企业网站的颜色
  • 做网站用jsp和htmlwordpress手机号网站
  • 公司建设一个网站wordpress 路由定义
  • 南昌建筑行业网站开发网站改版文案
  • 美术馆网站建设要求深圳网站设计必选成都柚米科技09做
  • 找个免费的网站护理专业简历
  • 现在做网络推广网站建设怎么样零基础网站建设教学在哪里
  • 大气宽屏企业网站源码谷德设计网介绍
  • dw如何做网站界面建网站开发语言对比
  • 个人的网站校园网站怎么做HTML