GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account. Current behavior When navigating forward and then back, the scroll position is lost. If the user decides do navigate back, the page does not scroll to the previous point. What is the expected behavior?
The browser should scroll to the previous position like it happens in any non-single page application. It should be considered a bug that can reduce the level of the user experience. I'm trying to solve this problem with the code below that detects if the current route url is in my previousRoutesUrl list.
If yes, I know that the user is navigating back, so that I won't call window. If not, I know that the page must be scrolled to top.
The problem is: once I call window. If I comment the window. The problem is harder than you may think. It is relatively easy to just scroll top on any route change, but to scroll to previous scroll position is hard. You need to store the scroll position for each route you leave preferably in session storage to make Duplicate tab browser feature work as expected and restore it when the route is active again, but only when it active through navigating back, and not when user just clicked Heroes list again how to determine if it is the first or second case?
Also the route you navigating to can have some dynamically loadable content, and the scroll position should be restored only after all this content is loaded again or else it will be wrong scroll position.
On non-SPA applications the browser takes care of all this cases. Maybe we can somehow use this browser feature to implement this, I don't know. But if it should be implemented fully on JS - it is hard problem I think. I hate to scroll down again and again.
If I meet the problem when using a web app, I would think why don't you just use simple web pages, the scroll problem just destroy all the good feelings. It's not only a problem specific to Angular 2. Angular 1, React and many if not all other frameworks suffer the problem. Google photos don't have the scroll problem.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. When navigating from one route to another then scrolling down a bit and navigate back to the previous route, the page remains at the same scroll position. Navigate from one view to another then scroll down a bit and navigate back to the previous view.
When navigating back to another route the page should be scrolled to position at which it was before going forward. This is the behaviour currently implemented in all browsers when navigating between normal pages not SPA. But I never seen this behaviour in any implementation of angular1 router. It is very annoying when you scroll down some list, click on list item to see details page, scrolling down details, then click browser Back button and not see the list page in exactly the same state as it was before.
The github, for example, implements this correctly: Open issues list and scroll down, then open any issue details, then click Back browser button. You will end up at exactly the same scroll position at which you were been before opening issue details page.
It will be very cool if angular2 router able to do it. It is really a killer feature I never seen in any other routers. Edit There is dedicated issue for this behaviour now Is there a recommended method for scrolling to the top in RC. The suggested workaround in no longer works. In my case the window. If you have a similar issue this is what I did as a workaround:. Any solution?
None of the proposed solutions work. This seems to be working for me-for how long, not sure:. Ariix wouldn't that scroll after every time change detection runs? So still looking. Any update on this? Its annoying when you scroll down some list, click a list item to see details which is on another route you have to scroll up. If the next details page's length is small and the list page is very long and by chance you clicked the last item in the list. It would give an impression that there aren't any contents on details page as the user has to scroll up to view contents.
This seems to remember the scroll position in Chrome and Firefox but not IEbut I have no idea why. SmaugTheGreat Yes, that does work! But how is that even possible that it remembers the last scroll position? Its strange, when using SmaugTheGreat solution i. But when using it window. A mixture of Smaugs solution above and Guilherme Meireles on stackoverflow does the trick for me. Also unsubscribing in ngOnDestroy to avoid leakage. As a quick response, i am posting one of my component.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. My services include:.
By default, the directive listens to the window scroll event and invoked the callback. To trigger the callback when the actual element is scrolledthese settings should be configured:. In this example, the onScroll callback will be invoked when the window is scrolled down:.
Find file. Sign in Sign up. Go back. Launching Xcode If nothing happens, download Xcode and try again. Latest commit Fetching latest commit…. My services include: consulting to companies and startups on how to approach code in their projects and keep it maintainable.
I provide project bootstrapping and development - while afterwards, I integrate it on site and guide the team on it. The event will be triggered this many milliseconds after the user stops scrolling. You signed in with another tab or window.
Angular v6.1 Now Available — TypeScript 2.9, Scroll Positioning, and more
Reload to refresh your session. You signed out in another tab or window.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again.
If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. Virtual Scroll displays a virtual, "infinite" list. This module displays a small subset of records just enough to fill the viewport and uses the same DOM elements as the user scrolls. This method is effective because the number of DOM elements are always constant and tiny irrespective of the size of the list.
Thus virtual scroll can display an infinitely growing list of items in an efficient way. Note - The Events without the "vs" prefix have been deprecated because they might conflict with native DOM events due to their "bubbling" nature.
Using the vs prefix will prevent this bubbling conflict because there are currently no official DOM events prefixed with vs. If you want to use the scrollbar of a parent element, set parentScroll to a native DOM element.
Items must have fixed height and width for this module to work perfectly. You can also modify bufferAmount which causes extra items to be rendered on the edges of the scrolling area. The event vsEnd is fired every time the scrollbar reaches the end of the list. You could use this to dynamically load more items at the end of the scroll. See below. If you want the header to scroll out of view don't add the header angular element ref. Otherwise, the 1st measured item is used for all items.
If your items can change sizes dynamically, you'll need to notify virtual-scroller to re-measure them. There are 3 methods for doing this:. This is what gives the performance benefits compared to keeping all the off-screen items in the DOM.Angular version 6.
This is a minor release that is a drop-in replacement for 6. Router Scroll Position Restoration Developers can now configure the router to remember and restore scroll position as the user navigates around an application. New navigation events will reset the scroll position, and pressing the back button will restore the previous position. Turn on restoration in the router configuration:. It is expected that this will become the default in a future major release.
The previous ViewEncapsulation. Native is now deprecated. ShadowDOM v1 has better cross-browser support than the previous version, and is being built as a shared standard across browsers. Many of these changes apply more to library authors and advanced developers, but you can learn more about the differences between v1 and v0.
This allows developers to more dynamically determine the set of rules to follow when designing Schematics. TypeScript 2. One important change in TypeScript 2.
TypeScript has relaxed these declaration emit visibility rules which means you no longer will see this error and you will no longer have to change your code for such export patterns. This is the last planned minor for 6. Sign in. Angular v6. Stephen Fluin Follow.
Get and set scroll position of an element
Angular Blog The latest news and tips from the Angular team. Angular Blog Follow. The latest news and tips from the Angular team.
See responses More From Medium. More from Angular Blog.
Stephen Fluin in Angular Blog. Blair Metcalf in Angular Blog. Discover Medium. Make Medium yours. Become a member. About Help Legal.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. Animated scrolling functionality for angular written in pure typescript with no additional dependencies. Breaking Change : Starting with v6 this library consists of two modules: ngx-page-scroll-core the service and ngx-page-scroll the directive. Using the PageScrollService scroll method you may trigger scroll animations.
Provide an options object that provides a reference to the document and the scroll target. Additional properties are optional. Note: The scroll method is a shorthand from creating a PageScrollInstance an object encapsulating all information relevant for performing a scroll animation using PageScrollService create and starting it using the PageScrollService start method.
Check the PageScrollConfig interface for possible options and their impact. The default values may be found in the defaultPageScrollConfig object.
For ease of use a directive pageScroll exists, which allows you to quickly add scroll animations to your angular app by adding a property to your existing HTML a-tags. It can also work cross-routes, meaning it will start the scroll animation after the target route has been loaded. It utilizes the ngx-page-scroll-core module for that, thus requires it as a peer dependency.
In your template you may add the pageScroll attribute to elements with an href attribute pointing towards an id on the same page e. The onClick event will be interrupted and the scroll animation starts.
Alternatively you may set the optional pageScrollTarget property to a valid css selector to specify the target element to scroll to. Additional attributes may be set on an DOM element using the pageScroll directive for customization. They take precedence over the default settings specified in PageScrollConfig class.
Thereby it is possible to have all page scroll-animations last e. The following example will check whether the route Home is currently loaded. If this is true, the scroll-animation will be performed with the default properties. If a different route is loaded, a subscription for route changes will be made and the scroll-animation will be performed as soon as the new route is loaded.
Overriding all possible properties.The scrollTop property sets or returns the number of pixels an element's content is scrolled vertically. Tip: Use the scrollLeft property to set or return the number of pixels an element's content is scrolled horizontally. Tip: To add scrollbars to an element, use the CSS overflow property. Tip: The onscroll event occurs when an element's scrollbar is being scrolled.
Toggle between class names on different scroll positions - When the user scrolls down 50 pixels from the top of the page, the class name "test" will be added to an element and removed when scrolled up again. Slide in an element when the user has scrolled down pixels from the top of the page add the slideUp class :. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.
Therefore, we use the documentElement property for these browsers body. Example Toggle between class names on different scroll positions - When the user scrolls down 50 pixels from the top of the page, the class name "test" will be added to an element and removed when scrolled up again. Example Slide in an element when the user has scrolled down pixels from the top of the page add the slideUp class : window. Remove this line to show the triangle before scroll draw triangle.
HOW TO. Your message has been sent to W3Schools. W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
Special notes: If the number is a negative value, the number is set to "0" If the element cannot be scrolled, the number is set to "0" If the number is greater than the maximum allowed scroll amount, the number is set to the maximum number. A Number, representing the number of pixels that the element's content has been scrolled vertically.