LoginSignup
0
0

More than 5 years have passed since last update.

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

Posted at

使わなくなったので供養

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()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0