Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best method of re-rendering a web page on orientation change?

I have a fluid CSS layout which is rendering badly on an iphone when I change the orientation. (It looks fine when it is refreshed).

I am using the code below to refresh the page on orientation change, which works fine - it just feels a little wrong doing so. Is there any way of achieving this without having to reload the entire page? It is a mobile site, I don't really want to force the user to load the page twice.

var supportsOrientationChange = "onorientationchange" in window,     orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";  window.addEventListener(orientationEvent, function() {     window.location.reload() }, false);    

Edit:

The two main issues when testing on an iphone are:

I have a which is 100% width, with a right aligned background image. When I change the orientation from portrait to landscape the body width remains as how it rendered on portrait mode and vice versa. It is more of an issue from landscape to portrait as the page is too wide and it seems to render the images twice.

like image 982
theorise Avatar asked Oct 27 '11 16:10

theorise


People also ask

How do I change the orientation of a Web page?

Change part of a document to landscape Select the content that you want on a landscape page. Go to Layout, and open the Page Setup dialog box. Select Landscape, and in the Apply to box, choose Selected text.

Which method is used to screen orientation?

When the screen orientation of an Android device is changed, the current activity being displayed is destroyed and re-created automatically to redraw its content in the new orientation. In other words, the onCreate() method of the activity is fired whenever there is a change in screen orientation.

What is CSS orientation?

The orientation CSS media feature can be used to test the orientation of the viewport (or the page box, for paged media). Note: This feature does not correspond to device orientation.

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

Assuming your CSS is already happily rendering on your various size mobile device screens, you need to define the viewport in the <head> of your template.

Example, this sets the page width to be the device's screen width and an initial zoom of 100%. Initial zoom is applied at page load, but not when the orientation is changed.

<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

By adding a maximum-scale=1.0 parameter to the viewport you will force the iPad/iPhone to maintain the zoom level and re-layout the page on orientation change. The disadvantage of this is that it will disable zooming. However, the advantage is that you can make layout adjustments with media queries to present the content in a suitable fashion for the current orientation. You can read more about viewport here: Choosing a ViewPort

Now onto media queries. You should put media queries at the bottom of your CSS file and in the order of smallest width to largest width for screen widths. For example, taken from the Html5BoilerPlate CSS example:

@media only screen and (min-width: 480px) {   /* Style adjustments for viewports 480px and over go here */  }  @media only screen and (min-width: 768px) {   /* Style adjustments for viewports 768px and over go here */  } 

So all your normal styles are above this and applied first, then if the screen is 480px or wider the next block of styles are applied, then if the screen is 768px or wider the last block of styles are applied.

By combining the fixed zoom level to 1.0 and the media-queries, you can make your site responsively resize to the screen size and orientation without javascript. Obviously you need to make sure the site is then well designed so users don't need zooming. If your site is optimized for mobile this shouldn't be a problem.

Please note: other non-safari mobile browsers may re-layout the page without setting the maximum-scale on the viewport. But this behavior is inconsistent and most developers seem to cater to apple devices even if the implementation is worse than other devices. Some other devices would maintain the zoom level and recenter the viewport when the orientation changes. But all devices are ok to fix the zoom level to 1.0.

like image 144
BenSwayne Avatar answered Sep 23 '22 06:09

BenSwayne


2015 update

All the other answers are incorrect or outdated. Here's what works:

  window.addEventListener('orientationchange', function () {      var originalBodyStyle = getComputedStyle(document.body).getPropertyValue('display');      document.body.style.display='none';      setTimeout(function () {        document.body.style.display = originalBodyStyle;      }, 10);    });

The code listens to the orientationchange event and forces a re-flow of the body element by hiding it and showing it 10 milliseconds later. It does not depend on any <meta> tags or media queries.

Other answers suggested using media queries, but you already use them, since you said "It looks fine when it is refreshed".

Some other answers suggest using location.reload(). This is very inefficient, because it will reload the entire page, including images etc. Especially on mobile, you don't want to do that.

Yet other answers suggested adding <meta name="viewport" content="width=device-width, initial-scale=1.0"> or variations thereof. As of Safari 7, this no longer works. Here's a demo. To make sure you see how it doesn't work, start with the iPad in landscape mode, load the page, then rotate. Notice the page doesn't expand to full height, despite using flexbox all the way.

Compare that to this page, where we use the hide/show body technique in production.

like image 24
Dan Dascalescu Avatar answered Sep 24 '22 06:09

Dan Dascalescu