/    Sign up×
Community /Pin to ProfileBookmark

Can I use javasript to maintain page position when orientation changes?

I’ve searched for answers to this enough to note that this is more complicated than I’d have thought. But I do notice that social media pages on “facebook” can get quite long, and yet will maintain the position you are “at” when you change the viewing orientation on a smartphone browser. Pages I’m trying to develop definitely do NOT. Some mobile devices seem to take care of this for you, but is there a “canned” drop in universal fix for this?

I’d thought maybe I could write code to be triggered by an “onscroll” event, somehow saving the current page position (maybe to a cookie?), and then maybe restore to that position when the window.onorientationchange event fires. BUT, I don’t even know if that can be done, and I know scroll “bars” don’t even show up on smartphone pages. So is there even an “onscroll” even in that case? Ideas?

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 08.2019 — I don't even know if such an event exists[/quote]Not exactly but there is a resize event. You can get width and height of the viewport and then get the orientation easily.
somehow saving the current page position (maybe to a cookie?)[/quote]As the window is not closed and opened again, it should be sufficient to simply store the position in a variable.

However I expect another problem: You can easily store the scroll position and set it again after the orientation has changed. But the required scroll position will not be the same as the dimensions of the elements will change when the width has been changed.
Copy linkTweet thisAlerts:
@PeterPan_321authorJan 08.2019 — @Sempervivum#1599714

Right. A simple global variable will do because the page isn't being re-loaded. But what you said is what I'm finding too. Easy enough to add both an "onScroll()" event to the body tag , and also add an event listener for the orientation change (window.addEventListener('orientationchange', screenOrientationChg);, So the onScroll function just gets and saves the document.body.scrollTop to a global variable. Then when my screenOrientationChg() function gets triggered, I thought I'd simply set document.body.scrollTop to that last saved variable. I initially put in enough alert()s to make sure the events were firing and know that the offset variable was updating, so I know its "functional"

Well I can tell it does do "something", in the sense of moving the screen to a new position when the phone is turned. But at least on my I-phone it does NOT move the screen to the desired point. Again I know its doable, because facebook seems to do it perfectly. But I think I'd need to translate the vertical and horizontal page points to each other. And even if i came up with a formula that worked on my particular phone, it would likely fail on others with different screen aspect ratios.
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...