scrollIntoView() doesn't work after opening collapsible - javascript

When I use scrollIntoView() normally to scroll to a selected element on the page, it works great.
But what I want to do, is click a button that opens a collapsible (thus making the page taller), and then scrollIntoView() to the top of that collapsible element.
When I try adding that piece of functionality, upon the first click the scrolling just "jerks" or flickers a bit, and on the second click it scrolls to the middle of the collapsible.
How can I get around this?
I've tried performing scrollIntoView() after a delay of 10 seconds, same bad behavior occurs.
Sorry, can't share the code because it doesn't belong to me.

Related

jQuery scroll to top freezing if called mid-scroll

I have some code using jQuery's scrollTop(0) method which is not working correctly on Chrome (Version 56.0.2924.87).
My code sets a 2 second timeout which does the following:
Scrolls to the top of the screen.
Un-hides a div which has 100% width and height and requests the user to complete some action.
Appends a class to the html tag which sets overflow: hidden.
Once users completes the action it will remove the class and hide the div so they can continue scrolling.
This is working relibaly on Firefox.
On Chrome, if the 2 second timeout executes while the user is not scrolling, then it works as expected. However, if the 2 second timeout is called while the user is in the middle of scrolling, it seems to freeze.
I have created this Plunker containing all the code. The issue does not occur if you test within the in-line demo window, you need to click on the Preview in a Separate Window (blue button top right inside the code demo).
In my demo, this is what happens if the timeout runs while I am not actively scrolling:
And this is what happens if the timeout is executed while I am scrolling down (mid-scroll):
How can I make this work reliably on Chrome even if the user is currently scrolling on the page?
Why you can not see the div when is display. When the event is called in the middle of the scrolling the scroll bar hidden and the div apper at the top of your page, but your viewport is not at the top position. If you set the div CSS position property to fixed it will work, css position. the fixed value of the position property is relative to the viewport.
So the problem is not on your jquery code.
code here
plunker
And check this:
Scrolls to the top of the screen.
Once users complete the action it will remove the class and hide the div so they can continue scrolling.
I think it will be best if they can continue at the position the wear before.
What about if you add stop(). before the .scrollTop(0)?
$('html,body,document').stop().scrollTop(0);
I am not an expert, but may be worth a shot.
Hope it helps.

Chrome exhibiting strange scrolling behavior after appending divs onto the page

I have a webpage that loads comments from a MySQL table, by appending "comment" divs one at a time into a collective "comments" parent div. However, this has caused some problems with scrolling in Google Chrome. I've tested this behavior in Firefox and it works fine there.
If I refresh the page anywhere above where the bottom of the page would be without any comment divs, the refresh works just fine and the scroll bar is where it should be.
However, if I refresh the page after scrolling beyond the first appended comment div, the page sets the vertical scroll position to where it would be if the comment divs weren't appended at all. I can't scroll down with the mouse wheel, as if Chrome thinks the divs were never appended, even though they are clearly there and the scrollbar shows them to be there.
If I scroll up one tick of my scroll wheel from there, the page scrolls "up" to where the scroll bar would be after scrolling up one tick from the actual bottom of the page, with all of the comment divs appended. It's like the scroll bar is in two places at once, the "bottom of the page" if the comments weren't appended and the actual bottom of the page with the comments appended. Very confusing.
Scrolling by clicking and dragging on the scrollbar works just fine. Likewise, scrolling "up" and then scrolling either up or down from there works too. I just can't scroll downward without scrolling "upward" first or starting the page refresh at a point other than one below where the bottom of the page would be without the comments appended.
I'm not entirely sure what's going on or how to fix it, and the only other mention of this I've seen on the internet doesn't have an explanation or a solution. Would anyone be familiar with what's happening, why it's happening, and how to prevent it?

The best way to prevent scrolling while mobile navigation is active?

I'm trying to find the best technique for showing a navigation/menu for a mobile site I'm working on. So far all my solutions have flaws, and I was wondering if anyone could point me in the right direction. (You may have to reduce browser window size to make the examples work as intended)
First off, take a look at this "basic" example: http://audunaas.no/menu/problem.html
You'll notice the content is still scrollable while the navigation is open. This is confusing and annoying.
My first attempt at a fix was to set the body to overflow:hidden and position:fixed whenever the navigation is open. Example here: (Had to remove link because maximum 2 links in post)
This, however, causes the body to snap back to the top and "forget" its scroll position. This means you are taken back to the top of the page whenever you open the navigation. Not good.
Lastly, my best solution so far is one where i put all the page content inside a #scroll-area-div with a fixed position taking up most of the screen, and body scroll set to hidden. Example here: http://audunaas.no/menu/
This solves most of the problems in the page itself, but breaks the "memory" of the back button. This means whenever you click back in your browser you are taken to the top of the page instead of back to where you were before you decided to click a link. (Mobile safari seems to be the only browser that remembers scroll position on div's aswell as body)
I can't really think of any more solutions to this, except for maybe some sort of javascript. Does anyone have some experience with similar cases, or any ideas on how to accomplish this?
The solution must:
Prevent scrolling on page content when navigation is open
Preserve scroll position on page content when navigation is open
Remember scroll position on previous page when back-button is used

JQueryMobile: How to start the page x pixels scrolled down?

I've got a JQueryMobile site, and I want to place a div at the top, that isn't visible on the first page load, but only when the user scrolls up. It's meant to emulate a pull-to-refresh. Basically like what these guys did, using this Javascript. This isn't a pull-to-refresh, but they're doing what I want to, which is 'hiding' a div above the initial view.
Unfortunately, JQueryMobile waits until the site is fully loaded, then automatically scrolls all the way back up to the top. So even though I can scroll the page down immediately, a few seconds later (random time), it'll scroll back up.
Is there any way to either stop the scrolling-back-up of JQueryMobile, or to create this div using HTML/CSS?
You can set display:none on your header so it isn't visible, then show it and scroll at the same time. See http://jsfiddle.net/Lanny/CDhmr/1/. Even if there's a significant delay between your page being rendered and your javascript executing scroll will happen at the same time that you extend the document.
It seems that $.mobile.defaultHomeScroll might help here.
Haven't used JQueryMobile, but noticed this pull request on GitHub:
https://github.com/jquery/jquery-mobile/pull/5751

How to prevent page scrolling by jQuery animate function?

I have a page that scrolls across multiple screens using jQuery's animate function. I am using tab indeces set by jQuery for the form. The problem is that when I give focus to the first element of the form using
$('#myforminput1').focus();
the page automatically scrolls to the element that has focus, even though it is on the third screen. I don't want the #myforminput1 to have focus until after it scrolls to page three. What happens when I add it to the code is that the page jumps to the focused element, and then animates to move the amount to page 3 so it overshoots the form. It might work if I could set the focus after the animation stops.
There is a similar problem with IE7 in that when a select box is selected using tabs, the page scrolls so that the select box is on the left part of a page. It seems like this problem might be related.

Categories

Resources