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

vue3 双容器自动扩展布局 根据 内容的多少 动态定义宽度

 需求:

左右两个列表 挨着排列,当左边内容超出滚动条时,换列显示,右边的列表随之移动

效果图:

1.左边数据:10,右边数据:5

2.左边数据:30,右边数据:5  此时:左边数据分两列显示,右边跟着移动

 

 完整代码:


<template><div class="layout-padding"><div class="layout-padding-auto layout-padding-view" style="overflow: auto"><div class="container1"><div class="left" ref="leftRef"><div v-for="n in leftItems" :key="n" class="item">Left {{ n }}</div><button @click="clickBtn('left')">+</button></div><div class="right" ref="rightRef"><div v-for="n in rightItems" :key="n" class="item">Right {{ n }}</div><button @click="clickBtn('right')">+</button></div></div></div></div>
</template><script setup>
import {ref, onMounted, onUnmounted} from 'vue'const leftItems = ref(180)
const rightItems = ref(50)
const leftRef = ref()
const rightRef = ref()const clickBtn = (type) => {if (type == 'left') {leftItems.value++getData(leftRef.value)} else if (type == 'right') {rightItems.value++getData(rightRef.value)}
}
const getData = (ref) => {if (!ref) returnconst items = ref.querySelectorAll('.item')if (!items.length) returnconst itemHeight = items[0].offsetHeight + 10const containerHeight = ref.offsetHeightconst columns = Math.ceil((items.length + 1) * itemHeight / containerHeight)ref.style.width = `${columns * 200}px`ref.style['min-width'] = `${columns * 200}px`
}
onMounted(async () => {getData(leftRef.value)getData(rightRef.value)
});const updateSize = () => {getData(leftRef.value)getData(rightRef.value)
}onMounted(() => {window.addEventListener('resize', updateSize)
})onUnmounted(() => {window.removeEventListener('resize', updateSize)
})
</script><style scoped>
.container1 {display: flex;height: 100%;overflow: auto;
}.left, .right {display: flex;flex-direction: column;flex-wrap: wrap;align-content: flex-start;gap: 10px;padding: 10px;
}.item {padding: 10px;background: #eee;width: 180px;min-width: 180px;
}button {margin-top: 10px;width: 180px;
}
</style>

http://www.xdnf.cn/news/13832.html

相关文章:

  • 【Pytorch】(1)Pytorch环境安装-②安装Pytorch
  • 激光雷达 + 视觉相机:高精度位姿测量方案详解
  • QCustomPlot 中实现拖动区域放大‌与恢复
  • Python打卡第53天
  • 【SAS求解多元回归方程】REG多元回归分析-多元二次回归
  • 论文略读:AutoG: Towards automatic graph construction from tabular data
  • 拍摄的照片模糊,有什么方法可以一键变清晰吗?
  • 头文件管理
  • 1.线性表的顺序存储-顺序表
  • FastAPI基础入门(四)
  • 【推荐算法课程二】推荐算法介绍-深度学习算法
  • cocos android打包 错误总结
  • QCoreApplication QApplication
  • 【2025】Ubuntu22.04深度学习环境搭建记录
  • Java常用类库大全(学习笔记)持续更新中
  • JS手写代码篇---手写sleep
  • fbx导入blender结构错乱,但在threejs中加载正常
  • 【Python打卡Day36】信贷项目的神经网络训练@浙大疏锦行
  • HALCON第三讲->特征提取
  • HTML常用标签(上)
  • 位运算基础
  • 强化微调技术与GRPO算法(2): 优势、应用场景与选择指南
  • python程序设计(2)
  • AI Agent的记忆体系与架构设计
  • QEMU源码全解析 —— 块设备虚拟化(27)
  • vue下的xlsx文件导出和导入的写法
  • 重要的城市(图论 最短路)
  • ESP32-CAM识别解析QR二维码输出数据
  • D3.js研发分区柱状图
  • 电子垃圾之涂鸦控制板