Vue3页面自适应,表格滚动高度

文章介绍了如何在Vue3中使用响应高度计算,特别是在表格滚动时,避免使用calc函数的复杂性。同时提供了微信小程序中获取元素到顶部距离的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

适用场景:在网页表格中我们需要获取页面剩余高度来为表格做滚动的时候就需要使用响应高度,可以使用原生calc来计算,但是calc有个缺陷就是,有可能要去计算多个盒子高度,使用下面的代码就可以直接获取当前元素到顶部的距离,然后减去总高度即可,是相当的方便 。

TS端代码:

import { ref , onMounted } from "vue";

/*
*
* Vue3计算剩余高度
*
*/
export default function () {

    //在Init的时候先行调用,然后在监听窗口的变化,保证是最新的宽高度
    onMounted(()=>{
        setWindowResize();
        window.addEventListener('resize',setWindowResize)
    });

    //测算基点
    let basePoint = ref();

    //元素测试盒子
    let elementToTopHight = ref(0);

    //窗口的高度
    let windowHeight = ref(0);


    const setWindowResize = function () {
        elementToTopHight.value = basePoint.value.getBoundingClientRect().top;
        windowHeight.value = window.innerHeight
    }

    return { basePoint , elementToTopHight , windowHeight };
}

页面端代码:

<script setup lang="ts">
  import useCommon from '@/common/useCommon';

  const  { basePoint , windowHeight , elementToTopHight } = useCommon();


</script>

<template>
   <div id="app">
     <div  style="height: 30px;background-color: rosybrown">{{ elementToTopHight }}</div>
     <div ref="basePoint"></div>
     <div :style="`height:calc( ${ windowHeight } - ${ elementToTopHight }px);background-color: tan`"></div>
   </div>
</template>

<style>
  html, body, #app {
    height: 100vh;
    width: 100vw;
    margin: 0;
    padding: 0;
    background-color: rebeccapurple;
  }
</style>

 微信小程序(Uniapp):

//获取屏幕的总高度
export function getScreenHeight() : number {
	let platform = uni.getSystemInfoSync().uniPlatform;
	if(platform == 'mp-weixin') {
		//微信小程序需要减去顶部的statusBarHeight
		return uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight || 0;
	}else {
		return uni.getSystemInfoSync().windowHeight;
	}
	
}


//微信小程序只能在页面内调用,不能导入调用,导入调用无效
export function getElementToTopHight( element : string ) : number {
	//初始高度
	let initHight = 0;
	uni.createSelectorQuery().select( element ).boundingClientRect( ( res : any ) => {
		initHight = res.top;
	}).exec();
	return initHight;
}


//获取页面剩余高度
import { getCurrentInstance } from "vue";


export async function getPageResidueDIST(nodeKey: string) : Promise<number> {
	const query = uni.createSelectorQuery().in(getCurrentInstance());
	let systemInfo = await uni.getSystemInfo();
	let nodeToTop : number = await new Promise((ok, _) => {
		query
			.select(nodeKey)
			.boundingClientRect((data: any) => {
				ok(data.top);
			})
			.exec();
	});
	return systemInfo.windowHeight - nodeToTop;
}

运行效果图:

### 使用 Vite、Vue3TypeScript 实现响应式设计与自适应布局 为了实现基于 Vite、Vue3TypeScript 的响应式设计和自适应布局,可以采用 CSS 媒体查询以及 Vue 组件化的优势来构建灵活的界面。下面是一个简单的例子展示如何设置这样的项目并创建一个能够根据不同屏幕尺寸调整自身的组件。 #### 安装依赖项 首先,在命令行工具中初始化一个新的 Vite 项目,并安装必要的包: ```bash npm init vite@latest my-vue-app --template vue-ts cd my-vue-app npm install ``` 接着按照之前的描述引入 `DataV Vue3`[^1]。 #### 创建响应式的 Vue 组件 定义一个名为 `ResponsiveComponent.vue` 的单文件组件如下所示: ```vue <template> <div :class="[&#39;responsive-container&#39;, currentSizeClass]"> <!-- 这里放置具体的 UI 元素 --> Content goes here... </div> </template> <script lang="ts"> import { defineComponent, ref, onMounted, onUnmounted } from &#39;vue&#39;; export default defineComponent({ name: &#39;ResponsiveComponent&#39;, setup() { let windowWidth = ref(window.innerWidth); const breakpoints = { small: 600, medium: 900, large: 1200, }; function updateWindowWidth() { windowWidth.value = window.innerWidth; } // 计算当前窗口大小对应的类名 const currentSizeClass = computed(() => { if (windowWidth.value >= breakpoints.large) return &#39;large&#39;; else if (windowWidth.value >= breakpoints.medium) return &#39;medium&#39;; else if (windowWidth.value >= breakpoints.small) return &#39;small&#39;; else return &#39;&#39;; }); onMounted(() => { window.addEventListener(&#39;resize&#39;, updateWindowWidth); }); onUnmounted(() => { window.removeEventListener(&#39;resize&#39;, updateWindowWidth); }); return { currentSizeClass, } }, }); </script> <style scoped> .responsive-container { width: 100%; } /* 不同断点下的样式 */ @media only screen and (max-width: 599px){ .responsive-container.small{ background-color: lightblue; /* 小型设备背景颜色 */ } } @media only screen and (min-width: 600px) and (max-width: 899px){ .responsive-container.medium{ background-color: lightgreen; /* 中型设备背景颜色 */ } } @media only screen and (min-width: 900px){ .responsive-container.large{ background-color: lightsalmon; /* 大型及以上设备背景颜色 */ } } </style> ``` 此代码片段展示了如何监听浏览器视窗宽度的变化,并据此改变应用中的某些属性或行为;通过媒体查询实现了不同分辨率下不同的视觉效果。 #### 注册全局组件 最后一步是在项目的入口文件 `main.ts` 中注册该组件以便在整个应用程序范围内使用它: ```typescript // main.ts import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; import ResponsiveComponent from &#39;@/components/ResponsiveComponent.vue&#39; const app = createApp(App) app.component(&#39;ResponsiveComponent&#39;, ResponsiveComponent) app.mount(&#39;#app&#39;) ``` 这样就完成了一个基本的支持多种屏幕尺寸的应用程序框架搭建工作。可以根据实际需求进一步扩展和完善各个部分的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值