JavaScript

スマホで画面の縦と横を判定する

使わなくなったので供養

const ua = navigator.userAgent.toLowerCase()

// iPhone
const isiPhone = (ua.indexOf('iphone') > -1)
// iPad
const isiPad = (ua.indexOf('ipad') > -1)
// Android
const isAndroid = (ua.indexOf('android') > -1) && (ua.indexOf('mobile') > -1)
// Android Tablet
const isAndroidTablet = (ua.indexOf('android') > -1) && (ua.indexOf('mobile') == -1)

const directionCheck = () => {
const direction = Math.abs(window.orientation)
if(direction == 90) {
alert('横向き')
} else {
alert('縦向き')
}
}

// iOS
if(isiPhone || isiPad) {
window.onorientationchange = directionCheck
}
// Android
if(isAndroid || isAndroidTablet) {
window.onresize = directionCheck
}
directionCheck()