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;
}
}本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!