Difficulty with HTML Bootstrap 3 including active class in UL LI elements

Struggling to implement scroll-spy for adding an active class to the ul li elements on a one-page scroll with a sticky nav. For some reason, I can't seem to get it to work within my project. Below is the menu structure:

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<nav class="navbar navbar-default navbar-fixed-top my-nav" role="navigation">
    <div class="container_full height">
        <div class="navbar-header page-scroll height">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="logo-holder">
            <img class="" src="img/logo-p.png"/>
            <span>
                <a class="page-scroll" href="#home-page">xxxxxx</a>
                <i>xxxxxxx</i>
            </span>
            </div> 
        </div>  
        <div class="collapse navbar-collapse navbar-ex1-collapse height">
            <ul class="nav navbar-nav height">
                <li class="hidden">
                    <a class="page-scroll" href="#page-top"></a>
                </li>
                <li>
                    <a class="page-scroll" href="#home-page">item1</a>
                </li>
                <li>
                    <a class="page-scroll" href="#item2">item2</a>
                </li>                   
                <li>
                    <a class="page-scroll" href="#item3">item3</a>
                </li>

                <li>
                    <a class="page-scroll" href="#item4">item4</a>
                </li>
            </ul>
            </div>
        </div>
    </div>
</nav>
<div class="container_full" id="site">
<section class="parallax">
<!-- HOME PAGE DISPLAY -->  
    <div id="home-page" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            //many divs css parallax--

The javascript file for the nav bar is as follows:

 $('.parallax').scroll(function() {

if ($('.parallax').scrollTop() > 100) { 
    $(".navbar-fixed-top").addClass("top-nav-collapse");
    $(".my-nav").addClass("nav-resize");



} else {

    $(".navbar-fixed-top").removeClass("top-nav-collapse");
    $(".my-nav").removeClass("nav-resize");
    $(".moveLogo").removeClass("moveLogo-resize");
}
});


$(function() {
$('a.page-scroll').bind('click', function(event) {
    var $anchor = $(this);
    var position =  $($anchor.attr('href')).offset().top + $('.parallax').scrollTop() ;

    $('html,body,.parallax').stop().animate({
        scrollTop: position
    }, 1500, 'easeInOutExpo');

    event.preventDefault();
   });
});

When using the above method without the parallax divs, it works fine. However, currently, the li class remains active on the last item when scrolling down the page.

https://i.sstatic.net/N4Cx7.jpg

In the CSS, green signifies scrollable content within the parent container "Blue", while the green divs are targeting specific IDs.

 .blue{
    height:100%;
    width:100%;
    overflow:hidden;

 }

 .green // Has many divs

I've attempted to target the green > divs but haven't had any success so far.

Answer №1

Your initial code lacked the necessary components for scroll spy functionality, so I took the liberty to include a set of div elements that are 1000px in height with varying background colors matching the IDs in your navigation menu. Upon testing, scroll spy is now operational. It's possible that you were either missing sufficient content to trigger scrolling or had incorrectly implemented the IDs.

Furthermore, it should be noted that I have integrated the most recent versions of Bootstrap and jQuery...

Feel free to check out the updated fiddle

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
        <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
        <!--[if lt IE 9]>
            <script src="//oss.maxcdn.com/libs/html5shiv/r29/html5.min.js"></script>
            <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>


<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<nav class="navbar navbar-default navbar-fixed-top my-nav" role="navigation">
    <div class="container_full height">
        <div class="navbar-header page-scroll height">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="logo-holder">

            <span>
                <a class="page-scroll" href="#home-page">xxxxxx</a>
                <i>xxxxxxx</i>
            </span>
            </div> 
        </div>  
        <div class="collapse navbar-collapse navbar-ex1-collapse height">
            <ul class="nav navbar-nav height">
                <li class="hidden">
                    <a class="page-scroll" href="#page-top"></a>
                </li>
                <li>
                    <a class="page-scroll" href="#home-page">item1</a>
                </li>
                <li>
                    <a class="page-scroll" href="#item2">item2</a>
                </li>                   
                <li>
                    <a class="page-scroll" href="#item3">item3</a>
                </li>

                <li>
                    <a class="page-scroll" href="#item4">item4</a>
                </li>
            </ul>
            </div>
        </div>
    </div>
</nav>
<div class="container_full" id="site">
<section class="parallax">
<!-- HOME PAGE DISPLAY -->  
    <div id="home-page" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:pink"></div>
        </div>
    </div>

    <div id="item2" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:red"></div>
        </div>
    </div>

    <div id="item3" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:green"></div>
        </div>
    </div>


        <script src="http://code.jquery.com/jquery.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>

</script>
    </body>
</html>

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

Creating a visually appealing website layout using HTML and CSS to align DIV

I'm having trouble with the CSS aspect of my portfolio website design. I'm trying to create a layout like this: Here's what I have so far: body { margin: 0; padding: 0; } .menu { height: 100vh; width: 380px; background-color: # ...

The banner <div> appears in Chrome but is not visible in IE9

I'm facing an issue with a banner div under my navigation. It seems to show up in Google Chrome but doesn't appear in IE9. Below is the HTML and CSS code: HTML: <div class="content"> <div class="slide-banner"></div> ...

How can I change the displayed value of a 'select' element programmatically in Internet Explorer?

My interactive graphic has a drop-down menu implemented with a <select> element. Everything functions correctly, except when using Internet Explorer. The issue arises when attempting to programmatically change the selected value of the <select> ...

Adjusting jQuery Button Width with input type of "button"

Currently, I am working with jQuery Mobile to develop a straightforward Golf Score Card App. To calculate the total at the end of a form submission, I've implemented the following code. <input id="total" type="text" name="total" value="" readonly= ...

fragment of the visual display

Are you aware that by utilizing the canvas tag in HTML5, it is possible to load a small portion of a large image with minimal load times? This can be advantageous when creating a game with just a few tiles but of considerable size, as it reduces the numb ...

Is a page reload triggered by clicking a webelement?

My Latest Project Lately, I've been working on developing a utility method to enhance my Selenium automation testing. This method aims to locate and wait for web elements efficiently, supporting various locators and incorporating timeouts for better ...

Tips for locating the highest number in JavaScript

I'm having trouble with my code where the first number, even if it's the largest, is not displaying as such. Instead, it shows the second largest number. Even when following suggestions, I encountered an issue where if the numbers are entered as ...

Generating small image previews in JavaScript without distorting proportions

I am currently working on a client-side Drag and Drop file upload script as a bookmarklet. To prepare for the upload process, I am utilizing the File API to convert the images into base64 format and showcase them as thumbnails. These are examples of how m ...

Sticky positioning is not maintaining its position at the top

I've noticed a strange behavior where, when I scroll down, the yellow box goes on top of the blue box. I have set both boxes to have a position sticky so that they should stay in place and not overlap. However, I want only the orange box to be scrolla ...

The script is failing to execute in a modal dialog box

Can anyone help troubleshoot why this script isn't functioning properly in a modal window (PrettyPhoto)? I've attempted jQuery(document.body).ready(function(){ instead of $(function() { but I'm encountering the same issue: the script wor ...

Can a single SVG file be referenced and reused multiple times in a project?

Instead of repeating these code blocks: <svg class="icon-user"> <use href="LONGFILENAME.svg#icon-user"> </use> </svg> <svg class="icon-user2"> <use href="LONGFILENAME.svg#icon-user2"> </use> </ ...

Submit a form to the same page without reloading and toggle the visibility of a div after submission

Is there a way to submit a form without refreshing the page and hiding the current div while showing a hidden one? I attempted to use the following code, but it ends up submitting the form and reloading the page. <form method="post" name="searchfrm" a ...

Can I display multiple jQuery Fancybox notifications on a single webpage?

Can multiple Fancybox alerts be displayed on a single page? For example, can a user trigger one alert and then trigger another within that alert without closing the first? For an example, you can click the zoom icon on the products and then click the &apo ...

Installing Slick Carousel is a breeze with the help of NPM and Gulp - here's how

I've been struggling to find clear instructions for installing slick-carousel, besides the npm install slick-carousel command on the npm website. As someone not very experienced with plugin installation this way, I could really use some guidance. Her ...

Hovering over an element will change its background color along with adjusting the background

I encountered a situation with the following code: CSS: <style> div.testy { border:1px solid black; } div.testy:hover { background-color:red; } </style> HTML: <div class="testy" style="height:100px; back ...

Adjust the div height to 100% in Bootstrap to center text, even with the presence of a navbar

I'm currently using Bootstrap 5 and facing an issue with getting text centered vertically on the page. I attempted to make the container of the div take up 100% of the page by using h-100, but it resulted in overflow due to the navbar occupying some s ...

Do static and relative elements without any additional properties always appear in the identical position when rendered?

While working with elements, I have come to understand that when an element has a position of relative, it serves as a fixed point for elements inside it with absolute positioning. Additionally, I have learned that with relative positioning, I have the abi ...

Tips for creating an input box that only accepts floating point numbers:

I have a custom component - a text box that I am using in two different places. In one location, it accepts integers and in another, floats. For the integer validation (where dataType=2), I have successfully implemented it. However, for the float validat ...

How can I place this ribbon on top of an image in an HTML email to ensure it displays correctly on email clients such as Outlook?

As I delve into my research, it's becoming apparent that achieving this result might not be feasible across all email clients. However, I'm curious to know if there have been any recent developments in the email world that would allow me to repli ...

The use of pattern fill in fabric.js is causing a decrease in rendering speed

I recently attempted to create a clip mask effect on a large image by using the picture as a pattern and setting it to the svg paths that support the desired shape. You can view the slow-loading jsfiddle example here: http://jsfiddle.net/minzojian/xwurbw ...