Display a navigation bar upon scrolling in Angular

I am looking to create a dynamic navbar that appears when scrolling down on a page that includes an embedded Power BI report. Here is the HTML structure:

<div class="divNav " (scroll)="onWindowScroll($event)">
...
</div>
<div class="report">
  <iframe  [src]="this.linkReport" frameborder="0" allowFullScreen="true"></iframe>
</div>

And here is the CSS styling:

other class ...
    .scroll-down
    {
      visibility: visible;
    }

Finally, in the TypeScript file, we have the following event listener setup:

@HostListener('window:scroll', ['$event'])
  onWindowScroll(e) {
    let menu = document.querySelector(".divNav");
    let scrollDown = "scroll-down";
    window.addEventListener("scroll", () => {
      const currentScroll = window.pageYOffset;
      if (currentScroll > 100) {
        menu.classList.add(scrollDown);
        return;
      }
    })

}

Answer №1

To me, it seems like HostListener serves as the event handler in this case. Are you repeatedly attaching an EventListener every time a scroll event occurs? You might want to consider the following approach:

@HostListener('window:scroll', ['$event'])
  onWindowScroll($event) {
    const currentScroll = window.pageYOffset;
      if (currentScroll > 100) {
        let menu = document.querySelector(".divNav");
        menu.classList.add("scroll-down");
      }
    })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am currently encountering challenges while trying to operate my smartadmin theme on angular 2 platform

I recently acquired the SmartAdmin Theme for my project development needs. It worked seamlessly with AngularJS, but I encountered numerous errors when attempting to integrate it with Angular2. I have attached screenshots showing the issues for your referen ...

Ways to assign unpredictable values (such as ids, dates, or random numbers) to a Domain Entity or Aggregate Root when it has been injected as dependencies

I am currently developing a frontend repository that follows an innovative hexagonal architecture approach with domain-driven design principles, and it utilizes Redux Toolkit. The development process involves Test-Driven Development (TDD) where I employ c ...

Requiring Additional d3 Plugins in d3 v4 Extension: A guide

I am currently working on developing a d3 v4 plugin by following the guidelines provided at . My main objective is to be able to npm install the plugin and seamlessly use it within an Angular 2/4 component. The repository for my project can be found here: ...

What is the best way to position a logo and navbar in alignment?

I'm still learning the ropes here. I'm having trouble getting the navbar to align with the logo because margin-top/bottom doesn't seem to be working for some reason. <HTML> <HEAD> <link href="style.css" rel="stylesheet"&g ...

Implementing scrolling functionality either horizontally or vertically to a specific element on a website beyond the typical emergency services

Is there a way to explain in detail the CONCEPTUAL process used to achieve this? I have observed that it loads "grid.html" which is tiled and can appear to be "infinite" if clicked continuously (try clicking on a corner image repeatedly). However, I am un ...

Ways to prevent Deno Workers from using cached source code

Good day, I am currently working on building a custom javascript code execution platform using Deno Workers. Additionally, I have implemented an Oak web server to manage requests for script modifications and their compilation and execution. An issue arise ...

What do you do when schema.parseAsync cannot be found?

Currently facing an issue with zod validation in my Node.js environment, specifically encountering the error: TypeError: schema.parseAsync is not a function Despite attempting various solutions like re-importing and troubleshooting, I am unable to resol ...

Issue arises when combining inline-block and overflow-wrap for div elements

I am facing an issue with two divs that look good on the same line when utilizing display: inline-block. However, if one of the containers contains an excessively long word that I attempt to wrap using overflow-wrap and word-break, it causes the container ...

Adjust the background image color with CSS styling

I have a collection of icons that I would like to incorporate into my application. These icons primarily consist of glyphicons with a few others mixed in. I want the HTML structure to mimic that of a glyphicon, specifically: <a href="#"> <spa ...

Discover the seamless transformation of a class definition into a Vue 3 component definition utilizing the dazzling 'tc39' decorators

The proposed API in the tc39/proposal-decorators repository is significantly different from the previous decorators API. Although TypeScript 5 doesn't fully support the new API yet, it's only a matter of time before the old API becomes deprecated ...

Is it possible to bind an http post response to a dropdown list in Angular?

My goal is to connect my post response with a dropdown list, but the text displayed in the drop-down box shows "[object Object]". My request returns two results - `ArticleID` and `Title`. I want to display `Title` in the dropdown and save the corresponding ...

What is the best way to retrieve information from multiple pages of my REST API?

Recently, I integrated a search engine into my application and now I need to gather all the data available for a particular resource, such as all the posts made by a user. The response I receive from my API looks something like this: { "count": 2, ...

Issue with triggering angular function multiple times in certain conditions

Issue: Experiencing difficulties with Angular directive as it is being called multiple times, resulting in incorrect transaction outcomes and multiple entries on the Console screen. Desired Outcome: Ensure that the function executes only once. Sample cod ...

The issue with Framer Motion's scrollYProgress not updating

Currently, I am working on a project that involves Framer Motion and Next.js. In my implementation, I am utilizing the Framer motion useScroll hook to keep track of the user's scroll activity on the page. However, it seems like the scroll tracking fun ...

Eliminate unnecessary spacing from the sticky container

Trying to implement a sticky menu in an angular 14 project using angular material 14 has been quite challenging for me. Initially, I attempted to use the 'fixed' position, but encountered issues where the menu would consistently return to the to ...

How to stop cursor from changing on link click in Google Chrome

Have you ever noticed that when you click on a link in Chrome (but not Safari or Firefox), the cursor changes from pointer to arrow? Is there a way to prevent this behavior so that the pointer remains even after clicking, while still hovering over the link ...

Sending the router outlet parameter to the parent element

Main component: <main-component> <sub-menu [menuItems]="routerlist"></submenu> <router-outlet></router-outlet> </main-component> Definition of router-outlet in TypeScript: To populate the menu list, I have initial ...

Unexpected behavior in Internet Explorer with ChartJS version 2.6.0

When using ChartJS v2.6 to create a pieChart within a Bootstrap Grid, everything appears correctly in Chrome and Firefox, but not in Internet Explorer. In IE, the chart is unintentionally stretched on larger screens. Bootstrap should handle the sizing, but ...

The v-bind class feature is failing to update the CSS on the object's properties

I'm attempting to dynamically modify the CSS of certain buttons based on object properties within a list. Below is the data I am rendering out, and despite following the documentation and ensuring my setup is correct, I seem to be overlooking somethin ...

WordPress Ignoring PHP Generated Elements When Processing Divs

Whenever I attempt to utilize the get_date function, it seems to disregard all div elements and instead places itself right after the header, at the beginning of the <div class="entry-content">. <div class="entry-content"> May 16, ...