Vue3 根据窗口的大小动态调整列表的长度

在当今的Web开发中,页面自适应不同设备的屏幕尺寸是一个至关重要的需求。Vue.js,作为一个流行的前端框架,提供了强大的响应式系统,使得开发者可以轻松地实现这一功能。本文将深入探讨如何使用Vue.js来实时监听浏览器窗口尺寸的变化,并根据这些变化动态调整页面布局。

~script:

javascript 复制代码
const windowWidth = ref(0);
const windowHeight = ref(0);
const tableOuter = ref();
  
const updateWindowSize = () => {
  windowWidth.value = window.innerWidth;
  windowHeight.value = window.innerHeight;
  page.pageSize =
    windowWidth.value > 1200
      ? Math.floor(
          (windowHeight.value -
            (isMobile ? 0 : tableOuter?.value.getBoundingClientRect().top) -
            110) /
            33
        )
      : 10;
	  
  await getList();//获取你的列表
};

onMounted(async () => {
  updateWindowSize();
  window.addEventListener("resize", updateWindowSize);//如果需要实时监听窗口的变化调整列表,就加上
  await getList();//获取你的列表
});

//如果需要实时监听窗口的变化调整列表,就加上如下代码
onActivated(async () => {
  await getList();
  window.addEventListener("resize", updateWindowSize);
});

onDeactivated(() => {
  window.removeEventListener("resize", updateWindowSize);
});

~template

javascript 复制代码
<template>
	<div ref="tableOuter">
		<el-table 
		:data="list"
		:height="autoHeight"
		size="small">
		</el-table>
	</div>
<template>
相关推荐
chushiyunen8 分钟前
mockito笔记
java·开发语言·笔记
悟天特斯9 分钟前
AI驱动的楼宇节能:从粗放管控到精准降碳的实践路径
开发语言·人工智能·python·物联网
名字还没想好☜10 分钟前
Python 用 tempfile 安全创建临时文件:NamedTemporaryFile、TemporaryDirectory 与别自己拼 /tmp 的坑
开发语言·后端·python·安全·编程语言
lvshuocool13 分钟前
golang 多版本管理工具 -- g
开发语言·后端·golang
matlab代码18 分钟前
基于matlab数字图像处理的指纹识别系统 点线特征(GUI界面)【源码69期】
开发语言·matlab
linx29524 分钟前
单元三 · 那 C 的底层知识怎么办
c语言·开发语言·数据结构·c++·嵌入式硬件
Cho1yon36 分钟前
【AI Agent 第十五期: AI Agent 从 0 到 1 系统学习大纲】
javascript·人工智能·学习
计算机魔术师39 分钟前
Beren Millidge、John Schulman、Charlie O'Neill 对谈递归自我改进离我们还有多远
前端
MetaLite44 分钟前
Java 时间处理的两个坑:单位误判,半秒算成一秒
java·开发语言·前端