How To Prevent Scrolling With Navigation in Angular Component?
Last Updated :
24 Jul, 2024
When navigating between routes in an Angular application, the browser's scroll position is often reset to the top of the page. This can be frustrating for users who want to maintain their scroll position between route changes. In this article, we'll explore how to prevent scrolling with navigation in Angular components.
Steps To Prevent Scrolling with Navigation
Step 1: Install Angular CLI
If you haven't installed Angular CLI yet, install it using the following command
npm install -g @angular/cli
Step 2: Create a New Angular Project
ng new prevent-scroll-app
cd prevent-scroll-app
Step 3: Generate Components
ng generate component home
ng generate component about
Dependencies
"dependencies": {
"@angular/animations": "^18.0.0",
"@angular/common": "^18.0.0",
"@angular/compiler": "^18.0.0",
"@angular/core": "^18.0.0",
"@angular/forms": "^18.0.0",
"@angular/platform-browser": "^18.0.0",
"@angular/platform-browser-dynamic": "^18.0.0",
"@angular/platform-server": "^18.0.0",
"@angular/router": "^18.0.0",
"@angular/ssr": "^18.0.6",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Project structure
Project structureExample:Below is a complete example that demonstrates how to prevent scrolling during navigation using Angular Router.
HTML
<!-- src/app/app.component.html-->
<h1>Angular17 Scroll preventing App</h1>
<nav style="
position: fixed;
bottom: 10px;
left: 50%;
background-color: red;
color: white;
font-size: 20px;
width: 100%;
">
<ul>
<li>
<a routerLink="/" routerLinkActive="active" ariaCurrentWhenActive="page">
Home Component
</a>
</li>
<li>
<a routerLink="about" routerLinkActive="active" ariaCurrentWhenActive="page">
AboutComponent
</a>
</li>
</ul>
</nav>
<!-- The routed views render in the <router-outlet>-->
<router-outlet> </router-outlet>
HTML
<!-- src/app/home/home.component.html-->
<h1>Home Component</h1>
<p>
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
AngularJS is a popular open-source framework that simplifies web development
by creating interactive single-page applications (SPAs). Unlike traditional
websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page. AngularJS makes this possible
by transforming static HTML into dynamic content that adapts to user
interactions. Features like data binding and dependency injection streamline
development, saving you time and effort. With regular updates and a large
community, AngularJS ensures your web applications stay modern and efficient.
</p>
HTML
<!-- src/app/about/about.component.html-->
<h1>About Component</h1>
<p style="background-color: brown; color: white">
AngularJS is a popular open-source framework that simplifies web development by creating interactive single-page
applications (SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user
experience by updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient. AngularJS is a
popular open-source framework that simplifies web development by creating interactive single-page applications
(SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by
updating content on the same page.
AngularJS makes this possible by transforming static HTML into dynamic content that adapts to user interactions.
Features like data binding and dependency injection streamline development, saving you time and effort. With regular
updates and a large community, AngularJS ensures your web applications stay modern and efficient.
</p>
JavaScript
//src/app/app.route.ts
import { Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];
JavaScript
//src/app/app.config.ts
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)]
};
JavaScript
//src/app/app.component.ts
import { Component, Inject, PLATFORM_ID } from '@angular/core';
import {
RouterOutlet,
RouterLink,
RouterLinkActive,
Event,
NavigationStart,
NavigationEnd,
Router,
} from '@angular/router';
import { CommonModule, isPlatformBrowser } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, RouterLink, RouterLinkActive],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
private scrollPositions: { [url: string]: number } = {};
constructor(
private router: Router,
@Inject(PLATFORM_ID) private platformId: Object
) {
if (isPlatformBrowser(this.platformId)) {
this.router.events.subscribe((event: Event) => {
if (event instanceof NavigationStart) {
this.scrollPositions[this.router.url] = window.scrollY; // Save scroll position
}
if (event instanceof NavigationEnd) {
const storedScrollPosition =
this.scrollPositions[event.urlAfterRedirects];
if (storedScrollPosition !== undefined) {
setTimeout(() => window.scrollTo(0, storedScrollPosition), 0); // Restore scroll position
}
}
});
}
}
}
To start the application run the following command.
ng serve --open
Output