uniapp媒体查询不好使
Uniapp使用媒体查询时需要注意以下几点:
- 使用@media标签定义媒体查询时,需要在样式文件中直接定义,不能在组件的style属性中定义。例如:
/* 在样式文件中定义媒体查询 */
@media (min-width: 768px) {
.container {
width: 50%;
}
}
-
在uniapp中,可以使用vw和vh单位来实现响应式布局,而不一定需要使用媒体查询。
-
如果在组件的style属性中使用媒体查询,需要使用uniapp提供的$media方法。例如:
<template>
<view class="container">
<view class="box"></view>
</view>
</template>
<style>
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #ccc;
}
/* 在组件的style属性中使用$media方法定义媒体查询 */
.box {
width: 100px;
height: 100px;
background-color: red;
$media(min-width: 768px) {
width: 200px;
height: 200px;
}
}
</style>
注意:$media方法只能在组件的style属性中使用,不能在样式文件中使用。
原文地址: https://www.cveoy.top/t/topic/bg91 著作权归作者所有。请勿转载和采集!