iPad doesn't trigger resize event going from vertical to horizontal?
Solution 1
If I understood you correctly, you want to do something when the user tilts the iPad. Here you go:
window.onorientationchange = function(){
var orientation = window.orientation;
// Look at the value of window.orientation:
if (orientation === 0){
// iPad is in Portrait mode.
}
else if (orientation === 90){
// iPad is in Landscape mode. The screen is turned to the left.
}
else if (orientation === -90){
// iPad is in Landscape mode. The screen is turned to the right.
}
}
Solution 2
I think what you want would be to use the iPad Orientation CSS, which looks like this:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css" />
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" />
Also, the orientationchange
event fires when the orientation is changed, according to iPad web development tips.
Together, that should give you tools enough to deal with the change.
Solution 3
This includes all orientations.
Here are two options:
window.onorientationchange = function() {
var orientation = window.orientation;
// Look at the value of window.orientation:
if (orientation === 0) {
// iPad is in Portrait mode.
} else if (orientation === 90) {
// iPad is in Landscape mode. The screen is turned to the left.
} else if (orientation === -90) {
// iPad is in Landscape mode. The screen is turned to the right.
} else if (orientation === 180) {
// Upside down portrait.
}
}
or
// Checks to see if the platform is strictly equal to iPad:
if(navigator.platform === 'iPad') {
window.onorientationchange = function() {
var orientation = window.orientation;
// Look at the value of window.orientation:
if (orientation === 0) {
// iPad is in Portrait mode.
} else if (orientation === 90) {
// iPad is in Landscape mode. The screen is turned to the left.
} else if (orientation === -90) {
// iPad is in Landscape mode. The screen is turned to the right.
} else if (orientation === 180) {
// Upside down portrait.
}
}
}
Solution 4
You want this fix for the orientation bug on iphone and ipad. read about it here: http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
github newest version here: https://gist.github.com/901295 use the second version on the page.
Solution 5
The only thing I could find from apple:
Safari on iPad and Safari on iPhone do not have resizable windows. In Safari on iPhone and iPad, the window size is set to the size of the screen (minus Safari user interface controls), and cannot be changed by the user. To move around a webpage, the user changes the zoom level and position of the viewport as they double tap or pinch to zoom in or out, or by touching and dragging to pan the page. As a user changes the zoom level and position of the viewport they are doing so within a viewable content area of fixed size (that is, the window). This means that webpage elements that have their position "fixed" to the viewport can end up outside the viewable content area, offscreen.
I understand the "works on the iPhone" part...but maybe it doesn't anymore? This could be a change in OS/mobile Safari since the latest public iPhone OS release shipped (the above documentation is from March 2010).
I'm going to re-tag this question adding iPhone to it, maybe one of the guys with the developer 4.0 OS release can test this? If it is the case it's been removed, this should be a bug filed/fixed before it goes live...I'm not sure on how the procedures are on this with Apple are though.
dclowd9901
Updated on July 09, 2022Comments
-
dclowd9901 almost 2 years
Has anyone noticed this behavior? I'm trying to write a script that will trigger upon a resize. It works fine on normal browsers, works fine on iPhone, but on iPad, will only trigger going from horizontal to vertical viewport, not vice versa.
Here's the code:
$(window).resize( function() { var agent=navigator.userAgent.toLowerCase(); var is_iphone = ((agent.indexOf('iphone') != -1)); var is_ipad = ((agent.indexOf('ipad') != -1)); if(is_iphone || is_ipad){ location.reload(true); } else { /* Do stuff. */ }; });
-
dclowd9901 about 14 yearsThanks for looking it up and adding the tag. To compound my confusion, I'm no Javascript zen master, so I wasn't even sure if I was putting the code together correctly.
-
Nick Craver about 14 years@dclowd9901 - The code looks alright, just an extra
;
after theif
there :) Just curious, why do you need to reload when the page resizes? -
dclowd9901 about 14 yearsI have functionality in a slideshow plugin that I wrote that gives it fullscreen viewing with a parameter. Users brought it to my attention that lack of accommodating window resizes was an oversight. I tried to write a bandaid before I release a new tested version, but came across the issue about which I posted.
-
dclowd9901 about 14 yearsI can't test this yet, but this is probably exactly what I'm looking for. Great general resource for general iPad development too. Thanks in advance artlung! I'm looking forward to trying this out!
-
artlung about 14 yearsCool! Hope it's helpful. I have a friend who just got one and am eager to play with it.
-
dclowd9901 about 14 yearsThis doesn't seem to be working. The safari browser doesn't seem to recognize the
orientationchange
property. -
artlung about 14 yearsI don't have an iPad or iPhone to test with. Is that where you're testing?
-
dclowd9901 about 14 yearsI should mention the browser built in debug outright says "window.orientationchange" is not a function.
-
kennytm about 14 years@dclowd:
window.
**on
**orientationchange
. -
dclowd9901 about 14 years@KennyTM: Looks like we have a winner. Put it up as an answer, and collect your prize ;)
-
dclowd9901 about 14 yearsI chose your answer because it was most concisely the correct answer. As a point of notation, though, the solution I went with incorporated none of the
if
checking. It simply ran code (namely a page reload) when the orientation was changed. -
Vincent about 14 years@dclowd9901 Yes, I thought so, I just wanted to provide some extra information :)
-
user3167101 almost 13 yearsI think it'd be better to check for
window.orientation
then explicitly check for the device in most situations. -
Alexandre Khoury about 12 yearsYou forgot when
orientation === 180
. It's when the iPad is turned upside-down. -
Gabbax0r about 7 years7 years later and this answer still rescues me. thanks a lot.
-
Vincent about 7 years@Gabbax0r Wow, that's crazy. I wrote this when I was still a teenager! Glad I could help.
-
Gabbax0r about 7 yearsthats nice. dont know why ipad still doesnt trigger .resize() from vertical to horizontal.
-
GreySage almost 7 yearsOf note is that the onorientationchange event is fired BEFORE the dimensions of the screen are actually changed, so if you print out height/width, you will get the numbers for the orientation you just left.