The addition of one-page navigation in Angular 7 seems to be experiencing some glitches

I recently added a new menu option to my Angular 7 project, but it's not functioning correctly. I used Bootstrap 4 and followed this example from here. Can anyone help me figure out the correct way to implement this?

Check out the sample on StackBlitz.

Thank you.

This is the code snippet:

home.component.html

<section id="about">
   <div class="container">
     <div class="row">
       <div class="col-lg-8 mx-auto">
         <h2>About this page</h2>
         <p class="lead">This template showcases various features like:</p>
         <ul>
           <li>Smooth scrolling navigation links</li>
           <li>Responsive design for a one-page website</li>
           <li>Bootstrap scrollspy functionality</li>
           <li>Minimal custom CSS for personalized designs</li>
         </ul>
       </div>
     </div>
   </div>
 </section>

 // More sections follow...

header.component.html

<header id="header">
    // Header content goes here...
</header>

full-component.html

<app-header></app-header>
<router-outlet></router-outlet>

Answer №1

Implement a function called scrollToElement() within the app.component.ts file and then invoke this function from the HTML code.

scrollToElement(element): void {
    element.scrollIntoView({behavior: "smooth", inline: "nearest"});
}

For instance,

<a class="nav-link js-scroll-trigger"  (click)="scrollToElement(about)">About</a>

In this example, about refers to a template reference of the targeted element for scrolling.

<section id="about" #about>

Refer to the updated Stackblitz for more details.

Answer №2

If you're looking to implement a basic bootstrap Scrollspy, here's a code snippet I just put together that might be helpful for you. Cheers!

function scrollSpy(v, e) {
    e.preventDefault();
    $('html, body').animate({
        scrollTop: $(v.hash).offset().top
    }, 500, function(){
        window.location.hash = v.hash;
    });
}

(function() {
    'use strict';
    window.addEventListener('load', function() {
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
        form.addEventListener('submit', function(event) {
            if (form.checkValidity() === false) {
                event.preventDefault();
                event.stopPropagation();
            }
            form.classList.add('was-validated');
        }, false);
        });
    }, false);
})();
section {
  height: 250px;
}

nav ul {
  display: flex;
  list-style: none;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<nav id="rd-nav" class="scrollspy-nav">
        <ul>
            <li>
                <a class="nav-link" href="#rd-home" onclick="scrollSpy(this, event)">Home</a>
            </li>
            <li>
                <a class="nav-link" href="#rd-how-we-work" onclick="scrollSpy(this, event)">How we work</a>
            </li>
            <li>
                <a class="nav-link" href="#rd-what-we-do" onclick="scrollSpy(this, event)">What we do</a>
            </li>
            <li>
                <a class="nav-link" href="#rd-portfolio" onclick="scrollSpy(this, event)">Portfolio</a>
            </li>
            <li>
                <a class="nav-link" href="#rd-contact" onclick="scrollSpy(this, event)">Contact</a>
            </li>
        </ul>
    </nav>

    <article class="container">
      <section id="rd-home">Home | Section</section>
      <section id="rd-how-we-work">How we work | Section</section>
      <section id="rd-what-we-do">What we do | Section</section>
      <section id="rd-portfolio">Portfolio | Section</section>
      <section id="rd-contact">Contact | Section</section>
    </article>

    <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №3

I found success by placing the header tag and code directly into the index.html file. It definitely made a difference for me.

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

App crashing when attempting to display an undefined value in an Ionic/Angular 2 application

Uncertain whether the issue lies with ionic or angular specifically. The use of {{ }} is unfamiliar, making it difficult to find a solution. Simple HTML layout: <ion-content padding> <ion-card> <ion-card-content> <ion-row ...

Aframe's a-assets feature experiencing issues when loading dynamic data through Angular 2

Since there is no fixed number of assets that need to be loaded from the server, I am utilizing Angular 2 templates to dynamically create assets. Below is a snippet of sample code: <a-assets> <div *ngFor="let scene of floorData.scen ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

Exploring the functionality of Kendo Grid within an Angular application

Currently, I am facing some challenges while trying to execute my Karma tests using the kendo grid within a fresh Angular project. The specifications for this specific component are outlined below: import { async, ComponentFixture, TestBed } from '@a ...

Creating a personalized design for Bootstrap Badge

Looking to customize the shape of bootstrap badges from the default ellipse to a rectangle with rounded corners. Any tips on the best approach to accomplish this? ...

Struggling with CSS due to the INCLUDE Function

After downloading a Template + CSS File for the website I am working on, everything was going smoothly until I decided to break down the template into separate files for easier modification and editing in the future. However, once I cut out the head sectio ...

Fan Animation in CSS

I have three unique images that I would like to animate in a fan-like manner consecutively. I prefer not to merge the images in Photoshop, as I want them to be displayed one after the other. Here is the code snippet (dummy images are used): .bannerimg ...

How to eliminate arrow icons from Bootstrap Carousel

Welcome to my blog at www.lowcoupling.com. I am trying to hide the left and right arrows on the bootstrap carousel, but my code doesn't seem to be working. Here is what I have tried: .glyphicon-chevron-right{ display:none; } (I also tried the ...

Steps for altering the primary color in PrimeNG__Changing the primary color

What is the most effective way to modify the default primary color for primeNG (saga-blue theme)? Altering --primary-color does not have the desired effect, as elements in node_modules/..../theme.css are styled using the main color hex rather than '-- ...

Setting up a Form submit button in Angular/TypeScript that waits for a service call to finish before submission

I have been working on setting up a form submission process where a field within the form is connected to its own service in the application. My goal is to have the submit button trigger the service call for that specific field, wait for it to complete suc ...

Compact NiceScroll Scroller within a narrower div compared to the width of the scrolled div

I am currently working on a table layout where I have fixed two columns on the left, similar to what is shown in this example. To enhance the scrolling experience, I am using the nicescroll plugin. However, I have encountered an issue where the plugin be ...

The CSS Image Gallery refuses to be centered

.imageContainer{ width: 100%; margin: 0 auto; position: relative; text-align: center; } .imageContainer img{ width: 250px; height: 250px; float: left; } .imageContainer img:hover{ opacity: 0.60; } I am having trouble gett ...

Blending an HTML jQuery toggle

Is there a way to make the current headline fade out while simultaneously fading in a new one when switching to the next div using a jQuery .html switch? Check out the codepen example: https://codepen.io/balke/pen/JpNNve $(window).scroll(function() { ...

What is the best way to generate a PHP loop that auto-populates content in a Bootstrap grid layout?

Currently, I am in the process of building a website for a school project. I need to figure out how to make the following PHP code loop automatically and display in a Bootstrap col-md-4 grid until there is no more content. I am feeling overwhelmed and un ...

Problem with the Auto-fill Feature in PrimeNG Module

Check out my code on Gist:   https://gist.github.com/rickymuvel/8ddc4d14d90877329447ddde9c0aa835 The issue I'm facing involves the Autocomplete module in PrimeNG. It seems that the specific path in the ubigeo.service.ts file is not being called. Her ...

Managing traffic in Google Kubernetes Engine (GKE)

I am encountering an issue with our website deployment on GKE, which consists of 10 pods. When deploying a new version, we use MAXsurge=1 and MAXunavailable=0. Upon trying to access the website during a new deployment, I sometimes only see the header in t ...

Can Angular components be used to replace a segment of a string?

Looking to integrate a tag system in Angular similar to Instagram or Twitter. Unsure of the correct approach for this task. Consider a string like: Hello #xyz how are you doing?. I aim to replace #xyz with <tag-component [input]="xyz">&l ...

The minimum and maximum validation functions are triggered when I am not utilizing array controls, but they do not seem to work when I use array controls

Take a look at the stack blitz example where min and max validation is triggered: https://stackblitz.com/edit/angular-mat-form-field-icrmfw However, in the following stack blitz with an array of the same controls, the validation does not seem to be worki ...

What is the best way to choose the initial and final <td> that appears within a <tr>?

I am working with an html table that has the following structure: <table> <tbody> <tr> <input type="hidden" name="a" value="x"> <input type="hidden" name="b" value="y"> <td>First Ce ...

What is the best way to ensure grid element takes up 100% of the available height within a flex column

I am working with this HTML structure: https://i.stack.imgur.com/fy5Yz.png My goal is to have the .SceneWithHistory-Container take up 100% of the available height in its parent element. Below is my CSS code: .Module-Container margin-top: 120px; displ ...