vue3瀑布流示例,左侧菜单根据窗口滚动条进行固定和取消固定,实现瀑布流demo

本文主要是介绍vue3瀑布流示例,左侧菜单根据窗口滚动条进行固定和取消固定,实现瀑布流demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

瀑布流demo的实现效果:

效果说明:

1.使用vue3实现瀑布流效果;

2.瀑布流横向设置5等分,可根据个人需求调整;

3.左侧菜单可根据右侧滚动条滑动时进行固定和取消固定,实现更优的展示效果;

4.瀑布流中的图片可使用img标签,也可使用背景图片(代码中注释的部分已标名)

实现方式:

一、创建瀑布流子组件

1.新建瀑布流组件的vue文件,如命名为WaterFall.vue

2.构建瀑布流中展示的内容,可根据不同需求进行自定义

<template><div class="list"><divclass="item"v-for="(item, index) in waterList":key="index":style="{width: width + 'px',height: item.height + 'px',left: item.left + 'px',top: item.top + 'px',// 'background-image': 'url(' + item.image + ')', // 假设 item 对象中有 image 属性// 'background-size': 'cover', // 如果需要的话,设置背景图片大小// 'background-position': 'center', // 如果需要的话,设置背景图片位置// 'background-repeat': 'no-repeat', // 禁止背景图片重复}"><img :src="item.image" :alt="item.text" /><p class="text-box">{{ item.text }}</p></div></div>
</template>

3.构建瀑布流展示的相关方法

这里需要说明的是:为增加该瀑布流的可复用性,瀑布流数据通过父组件的list进行传递,boxWidth是指盛放瀑布流的盒子宽度,也是通过父组件进行传递,也可个人自己定义。父组件在下面也会给出详细代码。

这里将每个瀑布流里面的盒子设置的间距为右边20,下边20,第一行的数据进行了特殊处理,可根据个人需求调整。

<script setup>
import { ref, reactive, onMounted } from 'vue';
const props = defineProps({list: {type: Array,default: () => {return [];},},boxWidth: {type: String,default: () => {return '';},},
});const width = 226; // 图片宽度
const gap = 20; // 图片上下间距
const rightGap = 20; // 图片右间距
const waterList = ref([]); // 瀑布流数组
const heightList = reactive([]); // 列高度数组// 屏幕宽度需要在 mounted 之后拿到
onBeforeUpdate(() => {// 计算列数console.log('组件里获取的宽度', props.boxWidth);const column = Math.floor(props.boxWidth / width); // 根据盒子的宽度确定列数console.log('计算的列数', column);// 核心内容就是维护每个图片的 left、topfor (let i = 0; i < props.list.length; i++) {// 先铺上第一行(i < column 则表示是第一行)if (i < column) {props.list[i].top = 0;props.list[i].left = i * (width + (i > 0 ? rightGap : 0));// 塞进瀑布流waterList.value?.push(props.list[i]);// 高度数据更新heightList[i] = props.list[i].height;}// 后面的就要一张张塞进去,每次找出最低的列往里塞else {// 最低的高度,先默认为第一列高度let current = heightList[0];// 最低的列,先默认为第一个let col = 0;// 循环每一列进行比较heightList.forEach((h, i) => {if (h < current) {current = h;col = i;}});console.log('最低的列', col, '高度为', current);// 由此计算出该图片的 left、topprops.list[i].left = col * (width + rightGap);props.list[i].top = current + gap;// 塞进瀑布流waterList.value.push(props.list[i]);// 更新列高度数组heightList[col] = current + gap + props.list[i].height;}}console.log('waterList', waterList.value);console.log('heightList', heightList);
});
</script>

4.设置瀑布流的相关样式

可根据个人需求进行调整,这里展示的是上方效果图的样式

<style lang="scss" scoped>
.list {position: relative;height: 100%;width: 100%;.item {position: absolute;.text-box {font-weight: 500;font-size: 18px;color: #000000;}img {width: 100%;height: 90%;object-fit: cover;}}
}
</style>

二、创建使用瀑布流的父组件

1.新建父组件,如命名为index.vue

2.创建父组件的展示内容,分为左侧的菜单和右侧的瀑布流内容

isMenuBarFixed为控制左侧菜单根据右侧滚动条进行滑动的属性,menu-bar里面的内容为左侧菜单的内容,works-box为右侧瀑布流的内容,中间的<div style="width: 160px" v-if="isMenuBarFixed"></div>为当左侧菜单脱离文档流的占位内容,关于脱离文档流,想要学习的同学可参考这篇博客:CSS标准文档流和脱离文档流_css脱离文档流-CSDN博客

<template><div class="max-content"><div class="main-content"><div class="works-publicity"><nav class="menu-bar" ref="menuBar" :class="{ fixed: isMenuBarFixed }"><ul style="padding: 0"><liv-for="(item, index) in menuItems":key="index"class="menu-item"@click="setActiveItem(index)":class="{ active: activeIndex === index }"><div class="menu-item-content"><divclass="menu-item-border":class="{ blue: activeIndex === index }"></div><img:src="item.isActive ? item.activeImage : item.image"class="menu-icon"alt=""/><div class="menu-text" :class="{ blue: activeIndex === index }">{{ item.text }}</div></div></li></ul></nav><div style="width: 160px" v-if="isMenuBarFixed"></div><div class="works-box" ref="myDiv"><water-fall :list="list" :boxWidth="boxWidth" class="water-fall" /></div></div></div></div>
</template>

3.父组件的相关方法

控制左侧瀑布流菜单根据右侧滚动条固定的相关关键代码可参考本篇博客的缩略版使用vue3实现右侧瀑布流滑动时左侧菜单的固定与取消固定-CSDN博客

<script setup>
import { cloneDeep } from 'lodash-es';
const config = useRuntimeConfig();const myDiv = ref(null);
const menuBar = ref(null);
const isMenuBarFixed = ref(false);
const boxWidth = ref(''); // 展示瀑布流的盒子的宽度
const menuItems = ref([{text: '第一个',image: '/images/works-publicity/industrial-design.png',activeImage: '/images/works-publicity/industrial-design-active.png',isActive: true,},{text: '第二个',image: '/images/works-publicity/handicrafts.png',activeImage: '/images/works-publicity/handicrafts-active.png',isActive: false,},{text: '第三个',image: '/images/works-publicity/painting.png',activeImage: '/images/works-publicity/painting-active.png',isActive: false,},{text: '第四个',image: '/images/works-publicity/photograph.png',activeImage: '/images/works-publicity/photograph-active.png',isActive: false,},{text: '第五个',image: '/images/works-publicity/geography.png',activeImage: '/images/works-publicity/geography-active.png',isActive: false,},{text: '第六个',image: '/images/works-publicity/tradition.png',activeImage: '/images/works-publicity/tradition-active.png',isActive: false,},
]);
const list = [{height: 450,background: 'red',image: '/images/works-publicity/test1.png',text: '平面作品+李宇轩',},{height: 600,background: 'pink',text: '美术作品+沈佳宜',image: '/images/works-publicity/test2.png',},{height: 450,background: 'blue',image: '/images/works-publicity/test3.png',text: '平面作品+李宇轩',},{height: 350,background: 'green',image: '/images/works-publicity/test4.png',text: '平面作品+李宇轩',},{height: 500,background: 'gray',image: '/images/works-publicity/test5.png',text: '平面作品+李宇轩',},{height: 400,background: '#CC00FF',image: '/images/works-publicity/test6.png',text: '平面作品+李宇轩',},{height: 300,background: 'pink',image: '/images/works-publicity/test7.png',text: '平面作品+李宇轩',},{height: 600,background: '#996666',image: '/images/works-publicity/test8.png',text: '平面作品+李宇轩',},{height: 400,background: 'gray',image: '/images/works-publicity/test9.png',text: '平面作品+李宇轩',},{height: 400,background: '#CC00FF',image: '/images/works-publicity/test10.png',text: '平面作品+李宇轩',},{height: 500,background: 'gray',image: '/images/works-publicity/test11.png',text: '平面作品+李宇轩',},{height: 300,background: '#996666',image: '/images/works-publicity/test12.png',text: '平面作品+李宇轩',},{height: 600,background: 'gray',image: '/images/works-publicity/test13.png',text: '平面作品+李宇轩',},{height: 400,background: '#CC00FF',image: '/images/works-publicity/test1.png',text: '平面作品+李宇轩',},{height: 500,background: 'gray',image: '/images/works-publicity/test2.png',text: '平面作品+李宇轩',},{height: 300,background: '#996666',image: '/images/works-publicity/test3.png',text: '平面作品+李宇轩',},{height: 300,background: 'gray',image: '/images/works-publicity/test4.png',text: '平面作品+李宇轩',},{height: 400,background: '#CC00FF',image: '/images/works-publicity/test5.png',text: '平面作品+李宇轩',},{height: 500,background: 'gray',image: '/images/works-publicity/test6.png',text: '平面作品+李宇轩',},{height: 400,background: '#996666',image: '/images/works-publicity/test7.png',text: '平面作品+李宇轩',},{height: 350,background: 'gray',image: '/images/works-publicity/test8.png',text: '平面作品+李宇轩',},{height: 450,background: '#CC00FF',image: '/images/works-publicity/test9.png',text: '平面作品+李宇轩',},{height: 430,background: 'gray',image: '/images/works-publicity/test10.png',text: '平面作品+李宇轩',},{height: 600,background: '#996666',image: '/images/works-publicity/test11.png',text: '平面作品+李宇轩',},
];
const activeIndex = ref(0);
// 点击左侧菜单栏切换内容
const setActiveItem = (num) => {menuItems.value.forEach((item, idx) => {item.isActive = num === idx;});activeIndex.value = num;
};
// 监听滚动事件
const handleScroll = () => {const scrollTopThreshold = 428;const scrollTop = document.body.scrollTop; // 获取滚动位置if (scrollTop >= scrollTopThreshold) {isMenuBarFixed.value = true;} else {isMenuBarFixed.value = false;}
};onMounted(async () => {await nextTick(); // 等待 DOM 更新boxWidth.value = myDiv.value.offsetWidth; // 确保 myDiv 已经被定义并且 DOM 已经渲染document.body.addEventListener('scroll', handleScroll); // 添加滚动事件监听器
});
// 组件卸载前
onUnmounted(() => {document.body.removeEventListener('scroll', handleScroll()); // 移除滚动事件监听器
});
</script>

4.父组件内容的相关样式

<style lang="scss" scoped>
.works-publicity {display: flex;width: 100%;padding: 20px 0;box-sizing: border-box;.menu-bar,.fixed {position: relative; /* 或者 static,取决于你的布局 */top: 0; /* 确保它在页面顶部开始 */transition: top 0.3s ease; /* 可选的过渡效果 */display: flex;justify-content: space-around; /* 根据需要调整菜单项间距 */list-style: none;padding: 0;.menu-item {display: flex;position: relative;align-items: center; /* 垂直居中图片和文字 */justify-content: center;cursor: pointer; /* 鼠标悬停时改变样式(可选) */width: 160px;height: 136px;text-align: center;border-left: 1px solid #d3dde9;}.menu-icon {width: 32px; /* 根据图标大小调整 */height: 32px; /* 根据图标大小调整 */margin-bottom: 20px; /* 图片和文字之间的间距 */}.menu-item-content {display: flex;align-items: center;flex-direction: column;}.menu-item-border {position: absolute;left: 0; /* 左边框位置 */top: 0; /* 可以根据需要调整 */bottom: 0; /* 可以根据需要调整 */width: 3px; /* 边框宽度 */background-color: transparent; /* 默认透明 */}.menu-item-border.blue {background-color: var(--el-color-primary); /* 激活时变为蓝色 */}.menu-text.blue {color: var(--el-color-primary); /* 激活时文字变为蓝色 */}}.fixed {/* 当固定时 */position: fixed;top: 0;width: 160px; /* 或者你需要的宽度 */z-index: 100; /* 确保它在其他内容之上 *//* 其他样式,可能需要调整以适应固定定位 */}.works-box {display: flex;flex-direction: column;width: 100%;.water-fall {margin-top: 30px;}}
}
</style>

这样,关于瀑布流的展示及滚动条滑动时,左侧菜单的灵活固定就完成啦!

这篇关于vue3瀑布流示例,左侧菜单根据窗口滚动条进行固定和取消固定,实现瀑布流demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1004284

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本