Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I trigger a CSS event in mobile safari upon iphone orientation change?

I'm trying to figure out how to change an embedded web page when the user rotates their phone from portrait to landscape mode - basically to load a view that is better suited to the wider screen format. Is this possible to do without calling a new page from the server, i.e. to drive it from within CSS/Javascript itself?

Thanks!

like image 801
tbacos Avatar asked Feb 22 '10 18:02

tbacos


People also ask

How do I change the orientation of Safari on my Iphone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.

How do I lock orientation in Safari?

Tap the "Screen Rotation Lock" button, which resembles a circular arrow, at the far left of the bar. A padlock appears along with the words "Portrait Orientation Locked." To disable the screen orientation lock, tap the "Screen Rotation Lock" button again.

How do I get orientation in Javascript?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });


2 Answers

You can use the window.orientation property. Its value is the degree that the current mode of view is used. Here is a brief example:

function updateOrientation()
{   
    var orientation=window.orientation;
    switch(orientation)
    {

        case 0:

                break;  

        case 90:
                break;

        case -90:   

                break;
    }

}

You can use this function on this event:

window.onorientationchange=updateOrientation;

Hope that helps!

like image 103
anthares Avatar answered Sep 30 '22 04:09

anthares


Best not to set the onorientationchange property directly. Instead use the following:

window.addEventListener('orientationchange', function (evt) {
    switch(window.orientation) {
        case 0: // portrait
        case 180: // portrait
        case 90: // landscape
        case -90: // landscape
    }
}, false);
like image 26
Andrew Hedges Avatar answered Sep 30 '22 04:09

Andrew Hedges