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
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read