移动端开发-禁止横屏

2021-12-17  本文已影响0人  我才不要你管呐

对于移动端的页面,很多时候是不希望横屏显示的,有可能横屏显示页面显示不全或者影响美观,也或者其他的方面。但是,横屏这个功能一般在手机或手机浏览网页的app上设置,作为网页是没有权限去操作这一设置的。
我们不能控制用户手机是否能横评,换种思路,手机横屏时页面提示禁止横屏

通过 orientationchange 事件判断是否横屏
orientationchange 为html5的新特性,是在用户水平或者垂直翻转设备(即方向发生变化)时触发的事件
效果如下


image.png

代码如下

(function rotate(){
   var orientation=window.orientation;
   var pd = null;
   function createPd(){
       if(document.getElementById('preventTran') === null){
            var imgData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAABaCAYAAADkUTU1AAAI9ElEQVR4Xu1cfXBcVRU/5+Z1N8GEj2AhFQvUIigfBetYaRVbBhADU2wHVoYk3bx3k8kMcSyFPxzUf8IfOjrqIHYUXbL3vW6mKXbtINapg1ColLEUnYIj9QPGOE0VdUjjlE3tdnffO87J7GY26yZ9H5tNst37X5tzzu/87rl777v3nnMR5rhFo9HLhBDrhRC3AMBqAFgBABfmYU8CwAgAHAGAVwDgJaXUO+Vc6u7uXhkOh0/GYrGxIC5jEOVZdLG3t7fdcZyHiOgORHSL4xDRfiHEE/F4fB8AEGNIKdcS0fMA8IxpmluC+OzWEdcY0Wh0jaZp2wFgjWulMoJE9CoRbRVCEHcCIp4PAOOpVOqSZDJp+7VdMcIbNmzQVqxYMYCIXwEA4dehEj2O+GlEfF/h/xFxfTwef9mv/YoQ7u/vb06n00kA+FypIxweAHgdAJ4DgF9nMpmj4+Pj77Jca2vr0nA4fC0ArAeAO4lotYvh/22l1JfnjXAkEmluaWn5JQB8ukx09hLRgGVZb7hxUNf1m4QQjxHRxlmI/0kpxZ3kqwWNMEopfwIAkRL0fwNAn1Lq51696ujouKKxsfEwAFw6k246nV45PDzMs7vnFoiwlPIRAPhuCeqbjuPcYVnWv7x609nZ+cFwOMzL0xVn0d2qlOKJ0XPzTZjXxYaGhqM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RSJaW67eiSfBTCazeefOne+56bjZZAIRzhtmG8Q7mba2tu8AwBcrWKTFnfX4yMjIowcOHMgFJcv6lSA8zQ8p5a0AwJPZqiAOEtEb/AigZVkHg9gp1a04YQaIRCINzc3N9yHil4honYeIF4b/9/Pf374np5k6aU4IF4NJKT8EAO355E5+NelyACjcBvJ7WKMAwLusV3K53L5EIsH/nrP2PzAJNfmP9znfAAAAAElFTkSuQmCC';
            pd = document.createElement('div');
            pd.setAttribute('id','preventTran');
            pd.style.position = 'fixed';
            pd.style.left = '0';
            pd.style.top = '0';
            pd.style.width = '100%';
            pd.style.height = '100%';
            pd.style.overflow = 'hidden';
            pd.style.backgroundColor = '#2e2e2e';
            pd.style.textAlign = 'center';
            pd.style.zIndex = '99999';
            document.getElementsByTagName('body')[0].appendChild(pd);
            var img = document.createElement('img');
            img.src = imgData;
            pd.appendChild(img);
            img.style.margin = '60px auto 30px'
            var br = document.createElement('br');
            var p = document.createElement('p');
            p.style.width = '100%';
            p.style.height = 'auto';
            p.style.fontSize = '22px';
            p.style.color = '#626262';
            p.style.lineHeight = '34px';
            p.style.textAlign = 'center';
            p.innerHTML = '为了您的良好体验';
            p.appendChild(br);
            p.innerHTML += '请将手机/平板竖屏操作';
            pd.appendChild(p);
        }
   }
   if(orientation==90||orientation==-90){
        if(pd == null && document.getElementById('preventTran') === null) createPd();
        document.getElementById('preventTran').style.display = 'block';
   }
   window.onorientationchange=function(){
      if(pd == null && document.getElementById('preventTran') == null) createPd();
      document.getElementById('preventTran').style.display='none';
      rotate();
   };
})();
上一篇下一篇

猜你喜欢

热点阅读