方法一: 媒体查询
<style lang='scss'>
@media only screen and (min-device-width: 375px) and (max-device-width: 812px) {
.serve-category {
width: 320px;
.category-name {
width: calc(310px / 4);
}
}
}
@media only screen and (min-device-width: 768px) and (max-device-width: 2000px) {
.serve-category {
width: 440px;
.category-name {
width: calc(430px / 4);
}
}
}
</style>
方法二: 通过 uni.onWindowResize 监听窗口尺寸变化
const resizeMixin = {
data() {
return {
windowResizeCallback: "",
resizeWidth: 0,
resizeHeight: 0,
isScreen: true,
};
},
created() {
const systemInfo = uni.getSystemInfoSync();
this.resizeWidth = systemInfo.windowWidth;
this.resizeHeight = systemInfo.windowHeight;
this.handelScreen();
this.windowResizeCallback = (res) => {
this.resizeWidth = res.size.windowWidth;
this.resizeHeight = res.size.windowHeight;
this.handelScreen();
};
uni.onWindowResize(this.windowResizeCallback);
},
methods: {
handelScreen() {
this.isScreen = this.resizeWidth > this.resizeHeight ? true : false;
},
},
beforeDestroy() {
uni.offWindowResize(this.windowResizeCallback);
},
};
export default resizeMixin;
<script>
import resizeMixin from "@/mixin/index.js";
export default {
mixins: [resizeMixin],
data() {
return {
popupWidth: "",
};
},
watch: {
isScreen: {
deep: true,
immediate: true,
handler(val) {
this.popupWidth = val ? "716px" : "550px";
},
},
},
};
</script>