-1

I'm working on an angular5 app. In that app, I need to go down to the current page based on data. ScrollToTop is working fine, I want to go down to the current page. Is there any way like scrollTop in angular5.

Joe Warner
  • 3,335
  • 1
  • 14
  • 41
  • 4
    Possible duplicate of [Scroll Automatically to the Bottom of the Page](https://stackoverflow.com/questions/11715646/scroll-automatically-to-the-bottom-of-the-page) – lin Mar 12 '18 at 13:12
  • Did you try the javascript `document.querySelector('#ComponentId').scrollIntoView();` ? – ibenjelloun Mar 12 '18 at 13:13
  • Not an answer but just take not of a function called focus, try and make use of that function on the last generated data. – Wale Mar 12 '18 at 13:15

2 Answers2

0

You can scroll to the bottom of the page with this line:

window.scrollTo(0,document.body.scrollHeight);

If you want to scroll a specific html element, try this one:

window.scrollTo(0,document.querySelector(".scrollingContainer").scrollHeight);

See also this demo, it can help you.

Laiso
  • 2,630
  • 3
  • 16
  • 32
-1

You can make it using animate and scrollTop from jQuery as following:

HTML :

<a href="#contact">Contact</a>
<!-- set the H2 deep down in the page -->
<h2 id="contact">Contact</h2>

jQuery :

$('a[href^="#"]').on('click', function(){
    var the_id = $(this).attr("href");
    if (the_id === '#') {
        return;
    }
    $('html, body').animate({
        scrollTop:$(the_id).offset().top
    }, 'slow');
    return false;
});
kevinniel
  • 1,088
  • 1
  • 6
  • 14