[前端]判断是否在移动端,以及手机方向

2018-11-01  本文已影响0人  喵吉呀呀

<div id="box"></div>

<div id="box2"></div>

<script type="text/javascript">

        function isMobile() {
                return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;
        }

        function orient() {
                if (window.orientation == undefined) {
                        return 'Not mobile';
                }

                // 竖屏
                if (window.orientation == 0 || window.orientation == 180) {
                        return true;
                }

                // 横屏
                else if (window.orientation == 90 || window.orientation == -90) {
                        return false;
                }
        }

        document.getElementById('box').innerHTML = 'isMobile:' + isMobile()

        isMobile()&&(document.getElementById('box2').innerHTML = 'isMobileVertical:' + orient())
        //另一种判断屏幕方向的写法
        function orient() {
                // landscape(横屏)和portrait(竖屏)
                return window.matchMedia("(orientation: landscape)");
        }
</script>

上一篇下一篇

猜你喜欢

热点阅读