js监听屏幕方向如何第一次默认不监听
大家有时候有需求在屏幕方向改变的时候重新执行某个渲染函数,以获取方向改变后的实际宽高,但是首次加载的执行函数要在其他地方执行,这时候可以加一个flag的状态值,默认为false,当第一次检测到方向改变的时候再改为true。
this.supportOrientation = typeof window.orientation === ‘number‘;
// 检查屏幕方向
checkScreenOrientation() {
if (this.supportOrientation) {
if (window.orientation === 0) {
this.screenOrientation = ‘portrait‘;
}
if (window.orientation === 90 || window.orientation === -90) {
this.screenOrientation = ‘landscape‘;
}
} else {
this.screenOrientation = (window.innerWidth > window.innerHeight) ? ‘landscape‘ : ‘portrait‘;
}
// if (this.orientationFlag) { // 在为true的时候交换屏幕长宽
let temp = this.bodyWidth;
// this.bodyWidth = this.bodyHeight;
// this.bodyHeight = temp;
// } else {
// this.orientationFlag = true;
// }
i f(this.orientationFlag){
imageMof();//重新设置图片高度
}else {
this.orientationFlag = true;
}
}本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!