Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a website force a device rotation lock?

I'm currently working on a website that is relatively equal for all devices; desktop & mobile. I'm working with % as I think that is the best option.

It's based on portrait mode. If you change the device to landscape, the whole website looks like a fat midget.

So I'm wondering: Is there a possibility to lock a website, displaying it in portrait all the time?

And by that, I mean: Device rotation locked. Not that when going to landscape, the website returns back to portrait, while in landscape. (which I already saw some code on StackOverflow.)

Check my site at: http://prototyping.iscs.nl/mobiel.html 

for reference :)

Thanks in advance

like image 593
Sander Schaeffer Avatar asked Nov 23 '12 12:11

Sander Schaeffer


People also ask

Why is my screen rotation locked?

If the screen is locked in Portrait or Landscape mode and you need to change it, tap the icon (either Portrait or Landscape) so it activates Auto rotate.

Why do some apps not allow screen rotation?

Cause of Android Screen Not Rotating Auto rotate option is turned off or not working. The screen you're using isn't set to auto-rotate. Recent apps are interfering with auto-rotate. You're touching the screen when you rotate.

Is there a way to force apps to rotate?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”


2 Answers

In an update to an old ('12) question, I think this can help a lot of people!

I haven't figured out a true way of locking the device rotation, but came up with a perfect alternative, which I've seen a few people do too.

Option A. One simple alert

By use of a simple jQuery script, you can detect the orientation of your device.

if(window.innerHeight > window.innerWidth){
  alert("Please use Landscape!");
}

Well, a simple alert is easy, but the notification can be quite nicer!

Option B. One nice image notification

(update as of 04-2018: (as I just saw my post again, I thought of something easier..) use media queries. Pretty much the same as below, but instead of using Javascript, use css, hide the element by default and show it when the orientation is landscape → @media (orientation: landscape) {...})

Simply add an fixed element to your page that is shown when the orientation has changed.

HTML

<div class="turnDeviceNotification"></div>

CSS

.turnDeviceNotification {
  position:fixed;
  top: 0;
  left:0;
  height:100%;
  width:100%;
  display: none;
}

You can update this element with text, or simply connect it to a background-image by

.turnDeviceNotification {
  background-image:url('../images/turnDevice.jpg');
  background-size:cover;
}

Simply add a nice background to your images folder, such as the one below.

Noticed the object has an display: none ? That's because else it'd be shown even in portrait mode. Now, all you need to do is to use the script below, so the object is shown only in landscape mode.

jQuery(window).bind('orientationchange', function(e) {
 switch ( window.orientation ) {
  case 0:
    $('.turnDeviceNotification').css('display', 'none');
    // The device is in portrait mode now
  break;

  case 180:
    $('.turnDeviceNotification').css('display', 'none');
    // The device is in portrait mode now
  break;

  case 90:
    // The device is in landscape now
    $('.turnDeviceNotification').css('display', 'block');
  break;

  case -90:
    // The device is in landscape now
    $('.turnDeviceNotification').css('display', 'block');
  break;
 }
});

This will show the notification only when the device orientation has changed to landscape. Sample image notification

like image 57
Sander Schaeffer Avatar answered Sep 21 '22 16:09

Sander Schaeffer


@media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) {
  html {
    transform: rotate(-90deg);
    transform-origin: left top;
    width: 100vh;
    overflow-x: hidden;
    position: absolute;
    top: 100%;
    left: 0;
  }
}

Source : https://css-tricks.com/snippets/css/orientation-lock/

like image 36
user13715931 Avatar answered Sep 17 '22 16:09

user13715931