Reputation: 31
I'm using the screen orientation plugin. It works well but after I lock the screen orientation the screen size remains with the previous orientation size. So I have to reload the app to get the right screen size after lock the orientation. Any idea why this behavior? I need to get the right size after the lock. Here a code sample:
console.log(window.screen.width) // 450
screen.orientation.lock('landscape').then(() => {
console.log(window.screen.width) // 450. Should be 640
});
BTW: I didn't find a issue tracker of this plugin
Upvotes: 0
Views: 533
Reputation: 2629
As a workaround, you could try listening for a proper orientation change event. The following snippet listens for a change event, and then removes the event listener after it fires once:
window.addEventListener('orientationchange', function() {
// After orientationchange, add a one-time resize event
var afterOrientationChange = function() {
// YOUR POST-ORIENTATION CODE HERE
// Remove the resize event listener after it has executed
window.removeEventListener('resize', afterOrientationChange);
};
window.addEventListener('resize', afterOrientationChange);
});
Add this snippet before you lock the orientation.
Edit: Here is why I suggest using a resize event in conjunction with the orientation change event: https://stackoverflow.com/a/49383279/508098
I do not know why that plugin does not work as expected -- I did not look.
Whilst this would be better to discuss with the developers of that project, as you pointed out, there is no issue tracker for that plugin's project.
Try this and see if you get the right numbers.
Upvotes: 2