> 由于设备的长宽尺寸不同,和用户查看模式(横向/竖向)的切换,页面需要进行相应的调整。
> orientationchange 事件是在用户水平或者垂直翻转设备(即方向发生变化)时触发的事件。
> window.orientation可以检测当前设备处于何种查看模式。
> window.orientation具有三个返回值:
(1)0:表示竖屏模式(portrait)
(2)-90:设备横向旋转到右侧的横屏模式(landscape)
(3)90:设备横向旋转到左侧的横屏模式(landscape)
(4)180:设备竖屏模式,但是倒竖着(portrait),当前暂不支持
const evt = 'orientationchange' in window ? 'orientationchange' : 'resize';
const resizeFontSize = () => {
const docEl = document.documentElement;
const { clientWidth } = docEl;
if (!clientWidth) {
console.log('没有获取到屏幕宽高,监听resize')
return;
}
let fs =100*(clientWidth / 1920);
if(fs>100) {
fs=100;
}
docEl.style.fontSize = `${fs}px`
}
const init = () => {
resizeFontSize();
if (window.addEventListener) {
window.addEventListener(evt,resizeFontSize,false)
}
};
export default { init, resizeFontSize}