js 检测移动端横屏以及竖屏切换

// 屏幕切换刷新
window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', screenFlipp, false);
//页面加载时检查
$(function () {
    screenFlipp();
});

function screenFlipp() {
    if (window.orientation == 0 || window.orientation == 180) {
        var status = window.localStorage.getItem('screenStatus')
        if (status == 'Vertical') {
            window.location.reload();
        }
        //alert('竖屏')
        window.localStorage.setItem('screenStatus', 'Horizontal');
        orientation = 'portrait';
        return false;
    } else if (window.orientation == 90 || window.orientation == -90) {
        var status = window.localStorage.getItem('screenStatus')
        if (status == 'Horizontal') {
            window.location.reload();
        }
        // alert('横屏')
        window.localStorage.setItem('screenStatus', 'Vertical');
        orientation = 'landscape';
        return false;
    }
}

非原创,也是在网上down出来的例子,引入到项目中发现不好使,然后自己又做了一遍改动,适用于AngularJS

你可能感兴趣的:(常用js方法)