Is there a way to prevent browsers from caching values like scroll position and zoom level? - javascript

I am designing an interactive web game that takes place entirely in the browser. It uses html5, and everything (including the elements) is part of the game world. Since this is the case, I need some pretty strict control over the positioning of my elements, scroll position, zooming, etc.
One particular level requires that an element be placed off screen (just outside the viewport) so that the user must scroll the page to find it. Unfortunately, after scrolling, the page seems to record the new width of the page including the originally unseen element. When the page is refreshed, the zoom level is adjusted to fit the entire screen with the hidden element into the viewport. This gives away the puzzle and ruins the level.
I know that browsers store information like scroll position so that when a user revisits the page they can pick up right where they left off. This is great for some things, but bad for my purposes. Is there a way to prevent this caching behavior of my browsers? Is there a way to get or set the zoom level of a page using JavaScript?
Currently I am using the code below to reset the scroll position right before the user leaves the page. It works pretty well, but the user can see the page scroll right before leaving.
window.addEventListener("beforeunload",function(event_){
window.scrollTo(0,0);
/* What I would love is if there were a way to do this: */
// window.zoomTo(1.0);
/* But I'm sure that's asking for too much. */
});

I managed to fix my problem by keeping the hidden element out of the html flow all together by setting its css position property to fixed. I simulate page scrolling by changing the elements style.left value with some custom touch event handlers. The page has no need to resize or zoom with the addition of the off screen element because fixed position elements do not effect layout.
This doesn't answer my question about resetting the zoom level, however, and I would still appreciate any insight anyone may have.

Related

Scroll the page up or down after scrolling reaches the top or bottom of a scrollable element in mobile

I know this question sounds confusing, so here's a better explanation:
I'm using a mobile. Say there's a scrollable element somewhere in the body. When I scroll the element and it reaches the bottom, I want instead of getting stuck for keep scrolling the element, it scrolls the page instead. That's also true if I reach the top of the element, it scrolls up the page, not getting stuck.
I tested it on two different mobile phones with the latest version of Chrome. The First one does exactly that. Weirdly enough, the second one only works when it gets to the top but not to the bottom. Is there any way to make it always work irrespective of the environment (mobile phone or browser) we are using?
Edit: What I'm trying to achieve with this
Desktop:
I want a way so that the buttons placed at the bottom of the content are not so distanced from the user's view. If we remove the scrollbar, then the users have to scroll all the way to the end of the content to be able to click the buttons.
The problem with this method is that, on mobile, in some browsers, it blocks the user from scrolling the page, even after reaching the end of the element. So they have to touch the edges of the screen to be able to scroll the page instead of the element.
What I want is, for users having difficulty touching the edges, they can still scroll the element. And when it reaches the bottom of the element, it scrolls the page.
I know this is weird. I know some of the better tricks like using the Read More-Read Less button, but it requires JavaScript I guess? I'm in a situation that's really hard to use JavaScript at the moment (shortly because of how bad the code is organized), and looking for a way if there's a simple trick using pure CSS. Any help or idea is appreciated!

More accurate scrolling

See this page:
http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/scrollbar_themes_demo.html
Try and scroll to 10,20 or 30% position using the mouse-wheel. It is not possible. It seems to jump to 33%, 66% and 100% as if they are anchors. That makes sense on a large page if you want to scroll quickly through the page.
But the page I am using it for I need more control, I need it to be able to easily scroll to specific points. Is that possible to do that where I can use the mousewheel to scroll more specifically to say 8,22,47%. Of course, it might not be possible to scroll to those exact points but it should be easy enough to scroll and slightly adjust with the mouse-wheel to get to that point you want.
Is that basically what smooth scroll is and if so do I just turn it off or do i need a jquery script to able to do this on my website or can you adjust your current script to change the anchors so instead of jumping 10% it only jumps 2% which would likely give more control.
On this particular page there wouldn't be a ton of scrolling needed but you do want to be able to stop it at specific points otherwise one tile is half cut off and then you have to use your mouse on the scrollbar to move it to a specific point where its not cut off.
I did not really know what was the best solution at the time but the problem was that I had a view like Trello with cards and when I used the mouse scroll often it would stop with half the card cut off, the only way to see the full card was to stop the mouse wheel scroll and manual use the scrollbar to pull it to an exact position.
As it turns out there was a perfect solution. http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/complete_examples.html
See the last solution, snap scrolling to images, I used that but snapped to the top of the card so for each mouse scroll it moves to show a new card which is the perfect solution for me and hopefully will solve other users with similar layouts.
The initial link I posted did not have this solution even though it was the same code repo, I stumbled across the solution later but I accept I probably did not explain my question very well initially

Controlling image movement using scroll bar of browser

I want to guide an image through a path. The scroll down of browser should bring the image down the path, and the scroll up of browser should make the image to trace back its path.
Here is an example of what I would like to acheive:
I want to guide that bug down the path as the user scrolls down through the article on the page and make it retrace its path when the user scrolls up (The bug's head will always be in the direction of motion).
How can I achieve this using jQuery and javascript?
You'll need to calculate the path you intend to use, and then bind a function to the scroll event that moves the image based on the distance scrolled, something like :
$(window).on('scroll', function(e) {
var S = $(this).scrollTop(), // scrolled distance
T = 10 + (S/24), // value for Top
L = 10 + Math.abs(Math.sin(S/400)*50); // value for Left
$("img").css({top: T+'%', left: L+'%'}); //set CSS
});​
FIDDLE
I don't believe you can directly capture the mouses scroll with javascript or jquery alone. As that works with whats part of the browser, and inside the "window". You can however capture a scroll event. Based on the windows height/width. It takes a little calculation via javascript to build a reliable/stable equation that will work on all browsers in all resolutions. I'm not going to go into details here more so due to the fact that your question is rather vague and doesn't supply a problem to be solved more than it sounds like a demand for an answer.
But the essence of what you may want to do is, get the width/height of the window/body. And through some trickery of smoke and mirrors or in this case HTML and CSS and properly laying out a bunch of layers on top of one another create a page thats a mile long, with a hidden scroll bar, that you have 2 layers on top of one acting as your "path" and the other your image.. Of which when scrolled from point a to point b is tracked via the scrolling event of the page being a mile long under it all. And then use that couples with the width/height found to make adjustments so it doesnt run off screen at any given point (less thats what you want).

scrollTo() flickering

I have a dynamic page where clicking a link triggers some javascript that sets some page elements to display:none, and thus changes the height of the page. The typical browser behavior for this is that the scroll position from the top of the page is retained. I want to retain the scroll position from the bottom instead, because the link is near the bottom of the page and I would like the user's cursor to remain over the link after it is clicked.
The code I have to do this is:
var scrollBottom=getDocHeight()-getScrollTop();
//do stuff to change height
window.scrollTo(0,getDocHeight()-scrollBottom);
(using cross-browser functions I found to obtain document height and scroll position).
This works fine in chrome and internet explorer, but in firefox, there is a small delay between the page height changing and the scroll position changing. As a result there is a flicker as the page quickly realigns itself, which is bothering me a great deal.
Can anyone suggest a fix or a more natural way to remember the scroll position from the bottom of the page instead of the top?
Thank you.

I need some pointers on how to implement inertia

Ok, so I've created a little plugin that takes a bunch of elements and creates a sort of never ending list. I'll try to explain...
I have a div, and it's got about 20 elements tags in it. When the user scrolls up, the top element moves out of view and is moved to the bottom of the list. And vice-versa so that when the user scrolls down, the bottom element is moved to the top of the list.
This is specifically for Mobile Safari (iPad, iPhone) web content
What I would like to do is implement inertia so the scrolling slows to a halt in response to how fast or slow the user is scrolling when their finger leaves the screen. Just like the inertia commonly found in the iPhone / iPad UI.
The problem is, every time an element moves to the top or the bottom of the list, the scollTop value for the parent div is adjusted to make it look like all the elements are staying in the same place. Which means the scrollTop value is never more than the top elements total height. So there's no value I can think of that I can keep on manipulating to give the illusion of inertia.
I'm stumped. Does anyone have any suggestions?
iScroll implements scrolling with inertia, but I'm not sure how it would react to you adding and removing elements mid-scroll. Might be worth looking into though.

Categories

Resources