Detect change in orientation using javascript

后端 未结 8 1228
鱼传尺愫
鱼传尺愫 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 05:14

    You can use mediaMatch to evaluate CSS media queries, e.g.

    window
        .matchMedia('(orientation: portrait)')
        .addListener(function (m) {
            if (m.matches) {
                // portrait
            } else {
                // landscape
            }
        });
    

    CSS media query fires before the orientationchange. If you are looking to capture the end of the event (when the rotation has been completed), see mobile viewport height after orientation change.

    0 讨论(0)
  • 2020-11-27 05:17

    You can use the orientationchange event like so:

    window.addEventListener('orientationchange', function(){
         /* update layout per new orientation */
    });
    
    0 讨论(0)
提交回复
热议问题