Detect change in orientation using javascript

后端 未结 8 1347
鱼传尺愫
鱼传尺愫 2020-11-27 04:37

Is it possible to detect change in orientation of the browser on the iPad or Galaxy Tab using javascript? I think it\'s possible using css media queries.

8条回答
  •  青春惊慌失措
    2020-11-27 04:56

    An easy to use snippet :

    function doOnOrientationChange()
    {
        switch(window.orientation)
        { 
            case -90:
            case 90:
              // alert('landscape');
              $('#portrait').css({display:'none'});
              $('#landscape').css({display:'block'});
    
              break;
            default:
              // alert('portrait');
              $('#portrait').css({display:'block'});
              $('#landscape').css({display:'none'});
              break;
        }
    }
    
    window.addEventListener('orientationchange', doOnOrientationChange);
    
    // First launch
    doOnOrientationChange();
    

提交回复
热议问题