网页手机端如何初始化就横屏

可以通过旋转的角度来判断

1.  window.onresize 只会在手机开启自动旋转后才会触发。

2. 通过 window.orientation 来判断是否旋转

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="./js/zepto.min.js"></script>

    <title>Document</title>

</head>

<body>

    <style>

        .test {

            position: absolute;

            left: 0;

            top: 0;

            background: black;

            color: #fff;

        }

        html,

        body {

            width: 100%;

            height: 100%;

            margin: 0;

            padding: 0;

        }

    </style>

    <script>

        const width = document.documentElement.clientWidth;

        const height = document.documentElement.clientHeight;

        let index = 0

        console.log(width)

        console.log(height)

        $(function () {

            if (width < height) {

                $('.test').css({

                    width: height + 'px',

                    height: width + 'px',

                    top: (height - width) / 2 + 'px',

                    left: 0 - (height - width) / 2 + 'px',

                    transform: 'rotate(90deg)',

                })

            } else { // 页面一开始就是横屏的情况(自动旋转)

                $('.test').css({

                    width: width + 'px',

                    height: height + 'px',

                    top: 0,

                    left: 0,

                    transform: 'rotate(0deg)',

                })

            }

        })


        function setWidth() {

            index++

            const width = document.documentElement.clientWidth;

            const height = document.documentElement.clientHeight;

            console.log(width)

            console.log(height)

            if (window.orientation === 0 || window.orientation === 180) { // 竖屏

                $('.test').css({

                    width: height + 'px',

                    height: width + 'px',

                    top: (height - width) / 2 + 'px',

                    left: 0 - (height - width) / 2 + 'px',

                    transform: 'rotate(90deg)',

                })

              $('#vsztms', window.parent.document).css({ // 这个是因为我的页面使用
                iframe嵌入进来的,横屏的时候没有自适应,
                所有我这边更改了父级iframe的宽高
                    'height': width,
                    'width': height
                });

            } else {

                if (index === 1) {

                    return

                }

                $('.test').css({

                    width: width + 'px',

                    height: height + 'px',

                    top: 0,

                    left: 0,

                    transform: 'rotate(0deg)',

                })

               $('#vsztms', window.parent.document).css({

                   'height': width,

                   'width': height

                });

            }

        }

        $(function () {

            setWidth()

            window.onresize = setWidth

        })

    </script>

    <div class="test" id="test">

        安河桥下

    </div>

</body>



</html>

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值