104 lines
3.6 KiB
Vue
104 lines
3.6 KiB
Vue
<script>
|
||
// rem 基准:以 1920×1280 设计稿为准,100px = 1rem,按宽高取小(contain)缩放
|
||
const DESIGN_WIDTH = 1920
|
||
const DESIGN_HEIGHT = 1280
|
||
const BASE_FONT_SIZE = 100
|
||
// 整体缩放系数:<1 整体缩小,>1 整体放大(布局偏大就调小这个值)
|
||
const REM_SCALE = 0.675
|
||
|
||
function logScreenInfo () {
|
||
try {
|
||
const info = uni.getSystemInfoSync()
|
||
console.log('[screen] uni =', JSON.stringify({
|
||
screenWidth: info.screenWidth,
|
||
screenHeight: info.screenHeight,
|
||
windowWidth: info.windowWidth,
|
||
windowHeight: info.windowHeight,
|
||
pixelRatio: info.pixelRatio,
|
||
platform: info.platform,
|
||
statusBarHeight: info.statusBarHeight,
|
||
safeArea: info.safeArea
|
||
}))
|
||
} catch (e) {
|
||
console.log('[screen] uni 失败 =', JSON.stringify(e))
|
||
}
|
||
|
||
// #ifdef APP-PLUS
|
||
try {
|
||
console.log('[screen] plus.screen =', JSON.stringify({
|
||
resolutionWidth: plus.screen.resolutionWidth,
|
||
resolutionHeight: plus.screen.resolutionHeight,
|
||
scale: plus.screen.scale,
|
||
dpiX: plus.screen.dpiX,
|
||
dpiY: plus.screen.dpiY
|
||
}))
|
||
} catch (e) {
|
||
console.log('[screen] plus.screen 失败 =', JSON.stringify(e))
|
||
}
|
||
// #endif
|
||
}
|
||
|
||
function setRemBase () {
|
||
// #ifdef H5
|
||
if (typeof document !== 'undefined' && document.documentElement) {
|
||
const docEl = document.documentElement
|
||
const width = docEl.clientWidth || window.innerWidth || DESIGN_WIDTH
|
||
const height = docEl.clientHeight || window.innerHeight || DESIGN_HEIGHT
|
||
const scale = Math.min(width / DESIGN_WIDTH, height / DESIGN_HEIGHT)
|
||
docEl.style.fontSize = (scale * BASE_FONT_SIZE * REM_SCALE) + 'px'
|
||
console.log('[rem] html fontSize =', docEl.style.fontSize, 'width =', width, 'height =', height, 'scale =', scale)
|
||
console.log('[screen-h5] screenWidth =', window.screen.width, 'screenHeight =', window.screen.height,
|
||
'availWidth =', window.screen.availWidth, 'availHeight =', window.screen.availHeight,
|
||
'devicePixelRatio =', window.devicePixelRatio)
|
||
console.log('[viewport-h5] clientWidth =', width, 'clientHeight =', height,
|
||
'innerWidth =', window.innerWidth, 'innerHeight =', window.innerHeight)
|
||
}
|
||
// #endif
|
||
|
||
logScreenInfo()
|
||
}
|
||
|
||
export default {
|
||
onLaunch: function () {
|
||
console.log('App Launch')
|
||
setRemBase()
|
||
// #ifdef H5
|
||
window.addEventListener('resize', setRemBase)
|
||
// #endif
|
||
},
|
||
onShow: function () {
|
||
console.log('App Show')
|
||
setRemBase()
|
||
},
|
||
onHide: function () {
|
||
console.log('App Hide')
|
||
},
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
/* rem 基准:设计稿 1920×1280,REM_SCALE=0.675
|
||
fontSize = min(w/1920, h/1280) × 67.5px = min(3.515625vw, 5.2734375vh)
|
||
纯 CSS 实现,首帧即正确,不依赖 renderjs 时序(修复真机退出登录后白屏) */
|
||
html {
|
||
font-size: min(3.515625vw, 5.2734375vh);
|
||
}
|
||
|
||
/* H5/Electron 下 picker 下拉弹窗宽度与触发下拉框一致(高特异性覆盖库默认 300px),阴影调淡 */
|
||
uni-picker .uni-picker-container .uni-picker-custom,
|
||
.uni-picker-container .uni-picker-custom.uni-picker-custom {
|
||
width: 6.656rem !important;
|
||
box-shadow: 0 0 0.4rem 0.1rem rgba(0, 0, 0, 0.1) !important;
|
||
}
|
||
|
||
/* H5/Electron 下 picker 下拉选项居左显示(覆盖库默认居中 text-align:center) */
|
||
uni-picker .uni-picker-container.uni-selector-select .uni-picker-select .uni-picker-item,
|
||
.uni-picker-container.uni-selector-select .uni-picker-select .uni-picker-item,
|
||
uni-picker .uni-picker-container .uni-picker-view-content .uni-picker-item,
|
||
.uni-picker-container .uni-picker-view-content .uni-picker-item {
|
||
text-align: left;
|
||
padding-left: 0.512rem;
|
||
}
|
||
</style>
|