在做弹框的时候,如果是从top、bottom出来,会自动填满宽度,但是从center出来,就只是内容大小。怎么样给父级设置宽度也没用。如果设置固定值,就不能做到自适应。尝试过用vue的方法,但是不成功,用微信小程序的方法会在编译的时候报错 ,虽然运行的时候没问题。
走了很多弯路,才发现其实uniapp有这个接口,隐藏得比较深。其实也有些惯性思维的原因,不应该跨过框架本身的接口去找其他方法。
获取系统信息:
screenWidth 屏幕宽度
screenHeight 屏幕高度
windowWidth 可使用窗口宽度
windowHeight 可使用窗口高度
windowTop 可使用窗口的顶部位置 App、H5
windowBottom 可使用窗口的底部位置 App、H5
statusBarHeight 状态栏的高
uni.getSystemInfo({
success: function (res){
console.log(res.model);
console.log(res.pixelRatio);
console.log(res.windowWidth);
console.log(res.windowHeight);
console.log(res.language);
console.log(res.version);
console.log(res.platform);
}
});
示例
设置弹框宽度为屏幕的80%
export default {
data () {
return {
setWidth: 0
}
mounted () {
this.$refs.setPlan.open()
// 注意,这里要用个变量存this,不然进到getSystemInfo后this指向会变化,找不到data变量
var _this = this
uni.getSystemInfo({
success: function (res){
_this.setWidth = res.windowWidth * 0.8
}
})
},
注意:计算表达式不能用 80%(会报错),要用0.8
错:30080%
对: 300
0.8
获取元素的宽度、高度、定位
可以获得如下信息:
bottom:
dataset,如ref
proto:
height:
id
left:
right:
top:
width:
// uniapp的方法
uni.getSystemInfo({
success: function (res){ // res - 各种参数
let obj = uni.createSelectorQuery().select('.类名')
obj.boundingClientRect(function (data){ // data - 各种参数
console.log(data)
}).exec()
}
})
走了很多弯路,才发现其实uniapp有这个接口,隐藏得比较深。其实也有些惯性思维的原因,不应该跨过框架本身的接口去找其他方法。
获取系统信息:
screenWidth 屏幕宽度
screenHeight 屏幕高度
windowWidth 可使用窗口宽度
windowHeight 可使用窗口高度
windowTop 可使用窗口的顶部位置 App、H5
windowBottom 可使用窗口的底部位置 App、H5
statusBarHeight 状态栏的高
uni.getSystemInfo({
success: function (res){
console.log(res.model);
console.log(res.pixelRatio);
console.log(res.windowWidth);
console.log(res.windowHeight);
console.log(res.language);
console.log(res.version);
console.log(res.platform);
}
});
示例
设置弹框宽度为屏幕的80%
export default {
data () {
return {
setWidth: 0
}
mounted () {
this.$refs.setPlan.open()
// 注意,这里要用个变量存this,不然进到getSystemInfo后this指向会变化,找不到data变量
var _this = this
uni.getSystemInfo({
success: function (res){
_this.setWidth = res.windowWidth * 0.8
}
})
},
注意:计算表达式不能用 80%(会报错),要用0.8
错:30080%
对: 300
0.8
获取元素的宽度、高度、定位
可以获得如下信息:
bottom:
dataset,如ref
proto:
height:
id
left:
right:
top:
width:
// uniapp的方法
uni.getSystemInfo({
success: function (res){ // res - 各种参数
let obj = uni.createSelectorQuery().select('.类名')
obj.boundingClientRect(function (data){ // data - 各种参数
console.log(data)
}).exec()
}
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2011-09-18 asp.net 用伪静态---修改webconfig配置文件 .
2011-09-18 asp.net : 网站中,全站统一设置title,keywords,description的实现方案