I'm trying to set the size of a background image to match the screen size upon window resize. The problem is that width and height don't alternate their values when I change the mobile orientation. When I test it in the dev tools of a desktop browser it works, however when testing in several mobile browsers, although the orientation does get changed, the measures don't.
This is the basic js:
$(function() {
function resizeBackground() {
$('#background-image').height(screen.height);
}
resizeBackground();
$(window).resize(resizeBackground);
});
Unfortunately due to a weird vh bug on iOS I'm forced to use JS. The issue here is that the background image jumps when the browser address bar of some browsers, specially Chrome and Firefox, gets hidden. It's detailed here: stackoverflow.com/questions/24944925/.
Summarizing my comments I want to describe why your solution doesn't work:
window.screen.height and window.screen.width get you the device's actual height and width and these values don't change on page resize or orientation change.
For the viewport sizes (you actually need viewport) the appropriate methods (variables) are window.outerWidth and window.outerHeight (in some cases window.innerWidth and window.innerHeight will work also).
For getting actual document size (html document), use document.documentElement.clientWidth and document.documentElement.clientHeight.
For detecting orientation change I would suggest orientationchange event instead of onresize event, because onresize also fires when keyboard is shown for example.
Those mobile browsers should support the more specific orientationchange event. Perhaps they're only firing that one and not resize. Try handling orientationchange too.
window.addEventListener('orientationchange', resizeBackground);
For Mobile Safari you can check window.orientation and if it is 90/-90, choose smaller of width/height for your "height". When orientation is 0, then choose higher value. I have now observed that when opening Mobile Safari directly in landscape, the values are swapped and do not swap on orientation change. As window.orientation is deprecated, it should only be used for Mobile Safari. FOr the rest, we use window.screen.orientation.angle.
Related
How can I make Google Chrome Dev Tools width the same as window.innerWidth
PS: window.screen.width solved my problem
I had the same problem ..... I had a js function that styles and arranges elements .... it has 2event listeners on load and on resize..... on Desktop Mode Evrything works fine ok both load and resize ..... But When i swith to mobile type from dev tools all the elements that use innerWidth as a variable get disturbed ..... this mainly happened because i didn't set the width of an image to 100% of its cotainer wich allowed it to be at it's original size and overflow the innerWidth .... try to find elements that overflows the width of your viewport before js is loaded..... if you set a div with a width that is higher than your device width the innerWidth property gets disturbed ..... but only on mobile mode devtools the desktop mode works fine and im wondering if the problem is related to chrome devtools or the mobiles would really get the wrong innerwidth value ... after checking the element that causes the problem you can adjust its width directly from html css or set up its width with js before you start using the innerwidth property ... thus problem has confuded me and made me wanna launch automated tests because its unexpected and since i use the innerWidth a lot .... PS:Screenwidth will solve your problem but it will always get the screen size not the viewport size ... for mobiles and tablets thats going to work fine since they are always full screen but for desktop thats not going to work when you resize the browser window ... and maybe one day tabets and large mobile will allow users to resize their screens
You should make a generic function.
function paginate(array, page_size, page_number) {
return array.slice((page_number - 1) * page_size, page_number * page_size);
}
called this function whenever your screen size changes. First, try to get the data accordingly then fix the UI with CSS.
I recently purchased an ASUS c100p Chromebook and noticed that when the device is flipped all the way back into "tablet" mode that sizable windows are maximized by default. If they have fixed window bounds they are effectively treated as modal (centered, immovable, and peripheral areas are dimmed) when in the foreground, unless they are set to have the alwaysOnTop attribute in which case they can be moved freely. I want to be able to detect this change so I can have an app with fixed window bounds perform some sort of action (maximize, close, set to alwaysOnTop, etc.). Is there a some way of detecting this transition in Javascript?
I've tried the following events with no success:
chrome.app.window.onFullscreened - Nothing fires on transition.
chrome.app.window.onMaximized - Fires on resizable windows, but not windows with fixed bounds.
chrome.system.display.onDisplayChanged - Fires if device is rotated from landscape to portrait while transitioning, but not if going straight to landscape tablet mode.
You could detect to see if the inner width and height of the window is the same as the fixed bounds with this code:
if(window.innerWidth != 400 || window.innerHeight != 400) {
// some code, eg window.close()
}
Replace the 400s with the width and height the window is being opened with.
In the other Android browsers (Chrome and the default-one) pinch-zoom fires window.resize
- Not so in Firefox : (
So how do I detect it (surely it can't be impossible) ?
Solution: I inserted a (hidden) div with width:100%.
And a function that regularly monitors this div's width.
- When the width in pixels changes, zoom has changed
(or orientation or screen size.. whatever): call myResize().
Demo: http://krydster.dk - Crossword puzzle game (danish).
I'm developing a Javascript/JQuery plugin for Responsive Web Design. It has a function that monitors the viewport for changes, specifically resize and orientation. When a change is detected, a corresponding callback function is called.
However, I just noticed that on Android (specifically using the stock browser on a Google Galaxy Nexus), if the user tries to use the soft keyboard, it resizes the viewport, thus firing the callback function. This is behaviour I would like to eliminate.
Is there a way to - via Javascript - disable this behaviour or detect for it so I can make changes to the code base to accommodate it?!
The solutions I've seen so far have to do mainly with Android App Development and I'm not sure they apply in my case.
Thanks.
Ok, well after some fiddling around I've found out a solution to my problem.
So what happens when the soft keyboard is shown/hidden?! In my test, the viewport width remains constant. However, the viewport height changes size [((current - previous)/previous)*100] when the soft keyboard is shown by 43% (in portrait) and by 58%(in landscape); and when the soft keyboard is hidden by 73%(in portrait) and 139%(in landscape) respectively.
So what I did was disable the callback function when the following conditions are all true:
The device is mobile
The percentage change in viewport width is less than 1%
The percentage change in viewport height is greater than 35%
Since mobile device browsers do not have resize handles like on the desktop, I do not believe there will arise a situation where a user will mimic the above conditions in a natural way.
You can see a sample of the code here: https://github.com/obihill/restive.js/blob/f051fe6611e0d977e1c24c721e5ad5cb61b72c1c/src/restive.js#L4419. Unfortunately, it's part of a bigger codeset, but you should be able to glean the basic idea based on the conditionals.
Cheers.
I had a similar problem. And my solution was to use the orientation change event instead of the resize event which triggers when you least expect it on android :P
$(window).bind( 'orientationchange', function(e){ // Type pretty code here });
source: http://www.andreasnorman.com/dealing-androids-constant-browser-resizing/
I can share you with my pretty code. I was setting trigger on resize event and counting height relative to before resize event.
originalHeight * 100 / currentHeight give you precent wich you can change height container
You can see a sample of the code here
https://jsfiddle.net/ocg9Lus7/
UPDATE 19.11.2018
I recomend you change value from dynamic (100%, vh etc.) to static value after onload window. If you need more dynamic container you can reculclate sizes by bynding function to resize event (originalHeight * 100 / currentHeight)
You can see a sample of the code here: https://jsfiddle.net/gbmo6uLp/
I have a variable height div fixed div that can change height on orientation change, so ideally I wanted to change the CSS by javascript. However on IE10 Metro using the Surface I can't find any JS event that deals with this? iOS and Chrome both handle it fine.
I have tried onorientationchange and onresize to no avail...
IE 10 doesn’t support Device Orientation Events[0], unfortunately. They do have a prototype[1], but this wont help for browsers that have already been released.
Depending on what you want to do, you may be able to use the orientation Media feature[2] to set a different CSS height value when in portrait or landscape. This is supported by IE[3]
[0] http://caniuse.com/#feat=deviceorientation
[1] http://blogs.msdn.com/b/ie/archive/2012/08/30/exploring-device-orientation-and-motion.aspx
[2] http://www.w3.org/TR/css3-mediaqueries/#orientation
[3] http://msdn.microsoft.com/en-us/library/ie/hh772710(v=vs.85).aspx