方向更改时的事件侦听器不会触发

时间:2019-05-18 11:20:46

标签: orientation addeventlistener event-listener

我正在尝试使用事件侦听器记录我当前的方向状态-但由于某种原因,它不会触发。

if(window.orientation){
    adjustHeader(window.orientation);
    window.addEventListener("orientationchange", e => {
        adjustHeader(window.orientation)
    });
}else if(screen.orientation){
    adjustHeader(screen.orientation);
    window.addEventListener("orientationchange", e => {
        adjustHeader(screen.orientation)
    });
}



function adjustHeader(deviceOrientation){
    if (Math.abs(deviceOrientation) === 90) {
        // Landscape
        alert("LANDSCAPE");
    } else {
        // Portrait
        alert("PORTRAIT");
    }  
}

0 个答案:

没有答案