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>
相关推荐
小鹿的周先生6 分钟前
第四章-SpringAI-函数调用&ToolCalling
开发语言·人工智能·python
江畔柳前堤7 分钟前
具身智能全景深度指南(2026年9月版):从“会聊天的AI“到“能干活的机器“
大数据·javascript·图像处理·人工智能·分布式·智慧城市·原型模式
️学习的小王11 分钟前
Windows Claude Code 接入 Playwright‑MCP,调用本机Edge浏览器(避坑完整教程)
前端·windows·edge
liuyicenysabel16 分钟前
Grafana + Prometheus 分级告警配置设计(P0/P1/P2)
javascript·grafana·prometheus
律宏阔17 分钟前
CloakBrowser 开发踩坑笔记
前端·浏览器
神仙别闹20 分钟前
基于C++ MFC 实现的智慧公交系统
开发语言·c++·mfc
Made in Haven71223 分钟前
HTML课程笔记补2
前端·笔记·html
秋田君25 分钟前
Qt_webSocket协议编程实战
开发语言·qt·websocket
Cry丶33 分钟前
Vue 3 业务管理页面实战:组件拆分、父子通信与弹窗复用
前端·javascript·vue.js·父子通信·组件拆分·弹窗复用
ttwuai35 分钟前
Go 后台附件迁到对象存储后,path、cdnUrl 和 tenant_id 怎么一起验?
开发语言·后端·golang