Created
February 25, 2020 01:27
-
-
Save xuanye/d95df22333f6c3fe6d4130e247cbf7de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 判断横竖屏 | |
var utils = { | |
debounce: function(func,delay){ | |
var timer = null; | |
return function(){ | |
var context = this, | |
args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function(){ | |
func.apply(context,args); | |
},delay); | |
} | |
} | |
} | |
var detectRes = document.getElementById('J_detectRes'); | |
var detectData = document.getElementById('J_detectData'); | |
function detectOrient() { | |
var storage = localStorage; // 不一定要使用localStorage,其他存储数据的手段都可以 | |
var data = storage.getItem('J-recordOrientX'); | |
var cw = document.documentElement.clientWidth; | |
var _Width = 0, | |
_Height = 0; | |
if(!data) { | |
sw = window.screen.width; | |
sh = window.screen.height; | |
// 2.在某些机型(如华为P9)下出现 srceen.width/height 值交换,所以进行大小值比较判断 | |
_Width = sw < sh ? sw : sh; | |
_Height = sw >= sh ? sw : sh; | |
storage.setItem('J-recordOrientX',_Width + ',' + _Height); | |
}else { | |
var str = data.split(','); | |
_Width = str[0]; | |
_Height = str[1]; | |
} | |
if(cw == _Width) { | |
// 竖屏 | |
return; | |
} | |
if(cw == _Height){ | |
// 横屏 | |
return; | |
} | |
} | |
// 3.函数去抖处理 | |
window.onresize = utils.debounce(detectOrient,300); | |
detectOrient(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://aotu.io/notes/2017/01/31/detect-orientation/index.html