Issue with Spyscroll functionality

Having some trouble getting Spyscroll to work. Can anyone help me identify the issue? I've been at it all day...

I've attempted both the javascript and html+css setups, but neither seem to be functioning correctly. Manually adding the "active" class to the link works for styling, but the navigation is not highlighting as I scroll.

Check out the Fiddle here

Here's the HTML snippet:

<body data-spy="scroll" data-target=".navbar" data-offset="50">
    <nav class="navbar navbar-default navbar-fixed-top">
        <ul class="nav">
            <li><a href="#1">ABOUT</a></li>
            <li><a href="#2">PROCESS</a></li>
            <li><a href="#3">SERVICES</a></li>
            <li><a href="#4">TEAM</a></li>
        </ul>
    </nav>

    <section class="page" id="1">
    </section>
    <section style="background-color:red" class="page" id="2">
    </section>
    <section class="page" id="3">
    </section>
    <section style="background-color:red" class="page" id="4">
    </section>
</body>

And here's the CSS snippet:

.nav {
  margin: 0 auto;
  list-style: none;
  text-align: center;
}
.nav li {
  display: inline-block;
  margin: 0 10px;
}
.nav li a {
  color: #000;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.2s ease;
}
.nav li a:hover {
  color: #00dcbe;
}

.nav li a.active {color:blue;}

.page {height:600px;border:1px solid #000}

Answer №1

To enable the functionality of Spyscroll, ensure that you include the .navbar-nav class in your ul element as shown below:

    .nav {
  margin: 0 auto;
  list-style: none;
  text-align: center;
}
.nav li {
  display: inline-block;
  margin: 0 10px;
}
.nav li a {
  color: #000;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.2s ease;
}
.nav li a:hover {
  color: #00dcbe;
}

.nav li a.active {color:blue;}

.page {height:600px;border:1px solid #000}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  <body data-spy="scroll" data-target=".navbar" data-offset="50">

    <!--==========MENU============-->

    
<nav class="navbar navbar-default navbar-fixed-top">

    

      
        <ul class="nav navbar-nav">
          <li><a href="#1">Section 1</a></li>
          <li><a href="#2">Section 2</a></li>
          <li><a href="#3">Section 3</a></li>
          
        </ul>

 

</nav>   

    <!--==========CONTENT============-->

  <section class="page" id="1">
  </section>
   <section style="background-color:red" class="page" id="2">
  </section>
   <section class="page" id="3">
  </section>
   <section style="background-color:red" class="page" id="4">
  </section>

  </body>

Answer №2

Your fiddle has a couple of simple issues that need to be addressed. Firstly, ensure that you load jquery before bootstraps's javascript by placing jquery at the top of the external resources list. Secondly, don't forget to include navbar-nav in your ul element like this:

<ul class="nav navbar-nav">

After making these changes, everything should work smoothly.

You can find an updated version of your fiddle here.

If you want your active nav link to appear in blue, make sure to target the li.active instead of the a tag specifically, and be more precise due to the additional styles applied by the navbar-nav class in bootstrap. Here is an example CSS code to achieve this effect:

.navbar-default .navbar-nav li {
  display: inline-block;
  margin: 0 10px;
}
.navbar-default .navbar-nav li a {
  color: #000;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.2s ease;
}
.navbar-default .navbar-nav li a:hover {
  color: #00dcbe;
}
.navbar-default .navbar-nav li.active a {
  color:blue;
}

Answer №3

Kindly review this for Easy spyscroll -

  <div class="navigation-block">
    <ul>
      <li>
        <a href="#red" class="active">Red</a>
      </li>
      <li>
        <a href="#green">Green</a>
      </li>
      <li>
        <a href="#blue">Blue</a>
      </li>
    </ul>
  </div>
  <div class="content-block">
    <div class="box-block red" id="red"></div>
    <div class="box-block green" id="green"></div>
    <div class="box-block blue" id="blue"></div>
  </div>



    body{
      margin:0;
    }
    html,body,.content-block,.box-block{
      height:100%;
      width:100%;
      display:block;
    }
    .red{
    background:#CF0404;
    }
    .green{
     background:#75890c;
    }
    .blue{
     background:#207ce5;
    }
    *{
      box-sizing:border-box;
    }
    .navigation-block{
      position:fixed;
      top:0;
      left:0;
      width:100%;
      background:#000;
    }
    .navigation-block ul{
      margin:0;
      padding:0;
      width:100%;
      list-style:none;
    }
    .navigation-block ul li{
      width:auto;
      float:left;
      margin-right:10px;
    }
    .navigation-block ul li a{
      width:auto;
      float:left;
      color:#fff;
      font-size:18px;
      font-weight:600;
      text-decoration:none;
      font-family:arial;
      padding:10px;
    }

    .navigation-block ul li a.active{
      color:yellow;
    }

Javascript

  window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;

  $('.navigation-block a').on("click",function (e) {
    e.preventDefault();
      var target = this.hash;
      var $target = $(target);

      $("html, body").stop().animate({
          "scrollTop": $target.offset().top - 40
      }, 600, 'swing', function () {
          // window.location.hash = target;
          history.replaceState(undefined, undefined, target)
      });
      $('.banner-navigation').find('li a').removeClass('active');
      $(this).parents('li a').addClass('active');

  });


  // scroll fixed

    function getSections($links) {
      return $(
        $links
          .map((i, el) => $(el).attr('href'))
          .toArray()
          .filter(href => href.charAt(0) === '#')
          .join(','),
      );
    }

    function activateLink($sections, $links) {
      const yPosition = $window.scrollTop();

      for (let i = $sections.length - 1; i >= 0; i -= 1) {
        const $section = $sections.eq(i);

        if (yPosition >= $section.offset().top - 40) {
          return $links
            .removeClass('active')
            .filter(`[href="#${$section.attr('id')}"]`)
            .addClass('active');
        }
      }
    }

    function onScrollHandler() {
      activateLink($sections, $links);
    }

    // Variables
    const $window = $(window);
    const $links = $('.navigation-block li a');
    const $sections = getSections($links);
    const $root = $('html, body');
    const $hashLinks = $('a[href^="#"]:not([href="#"])');

    // Events
    $window.on('scroll', onScrollHandler);

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

Server elements fail to refresh following the use of JQuery AjaxUpload

I have been using JQuery AjaxUpload to upload an image and then attempt to update the image control with the freshly uploaded image. However, my code is not functioning as expected and I am unable to set ViewState. I would like to figure out why this is ha ...

How come when I applied height:100% and position: absolute to .test2, it ended up being 200px instead of the expected 204px

Upon setting the height of a container element to 200px, I encountered an issue when trying to set the height of another element within it to 100%. The height didn't render as expected, as height:100% takes the height of the parent element, which was ...

Customized CSS scrollbar within a specific div

Is it possible to apply CSS to customize the scroll bar of a specific div without affecting the entire page layout? ...

The jQuery Animate feature springs to life with just a single click

I'm attempting to integrate jQuery's animate feature with a bookmark ID on my website. When I first click on the link, it jumps to the bookmark ID without any animation, but if I click the same link again, the animation works as expected. var Ti ...

How to retrieve video duration in Angular 2 before uploading the file

Is there a way to retrieve the video duration before uploading it in Angular2? I am looking for a solution without using jQuery. Here is the code snippet I have so far: <input type="file" class="form-control" ng2FileSelect [uploader]="uploader" accept= ...

When the form is submitted, the text input vanishes and then the page is refreshed using AJAX technology

Can anyone help me troubleshoot why my page is reloading even though I used AJAX, and how to prevent it from clearing my input text after clicking the submit button? I tried using the show method to resolve this issue but it didn't work. <form met ...

Apply a style to the div element that contains a TextInput component

As a beginner in the world of React and Material UI, I am currently working with Material UI version "1.0.0-beta.17", React 15.6.2, styled-components 2.0.0, and styled-components-breakpoint 1.0.1. Within a div element, I have two TextInput fields. const ...

troubles with compatibility between bootstrap.css and IE11

I am currently developing a web application using AngularJS and bootstrap.css. While everything appears fine on Chrome, I am facing some formatting issues on both Firefox and IE11. HEAD <head> <meta charset="utf-8"> <meta http-equi ...

Can someone help me troubleshoot why my ASP.NET web service call is not working properly?

I am trying to use jQuery to call a web service method, but I keep encountering the following error: Uncaught SyntaxError: Unexpected token < This error seems to be related to the URL of my service method. Here is the code I am using: $("#btnDialog") ...

Prioritize the first child in Flexbox by truncating its text before the second

I am working with a flex-box container named .search-bar, which contains two divs with text, labeled as .title. My goal is to make the second child take up any extra space available and only start ellipsizing when there is no more space left for the first ...

Ways to prevent ERR_INSUFFICIENT_RESOURCES when making AJAX requests

It's been a while since I dabbled in JavaScript, so please be patient with me. I'm currently developing an application that generates reports on student data using PHP as the backend. Periodically, we need to refresh the database used for these ...

A continuous jQuery method for updating select option values

I'm currently working on a form and using jQuery to make some alterations. The form consists of multiple select elements, and I want to change the value of the first option in each of them. However, as the form allows for dynamic addition of new selec ...

Pagination CSS may fail to function properly in Chrome after an AJAX call is returned

Having an issue with CSS not getting applied correctly after making an AJAX call with pagination. The problem seems to be specific to Chrome, as it works fine in Firefox. When inspecting the element, the CSS is present but not being applied properly until ...

Error 404 encountered while attempting to access dist/js/login in Node.JS bootstrap

I currently have a web application running on my local machine. Within an HTML file, I am referencing a script src as /node_modules/bootstrap/dist/js/bootstrap.js. I have configured a route in my Routes.js file to handle this request and serve the appropri ...

Latest FF 35 showing alert for blank field in HTML5 email input box

My form includes an email input field with a default value. When the user focuses on the field, it clears out if the value matches the default one. Upon blurring the element, the default value is restored if the field remains empty. In Firefox 35, clickin ...

I am consistently finding a class in my program, but when I try to locate the same class in the HTML, it seems to be non-existent

Here's the issue: when I manually search for a class = "warn," it only finds it if the seller does not have the card. This is because the text indicating that the card is unavailable has that class. However, the code below always returns TRUE even if ...

Can the bottom border on an input textfield be removed specifically under the new character?

This is the new visual design I received: https://i.stack.imgur.com/kiQGe.png The label CLG is represented as a label, with the line being an input type=tel. Disregard the purple overlay... The designer has requested that I remove the border when a user ...

JS: Discovering an element's screen position consistently across various computers

I am currently working with a matrix of size 13 x 20, where each cell contains a number ranging from 0 to 300, representing different tiles to build a map. In addition, there is a "hero" character - a movable image. Pressing an arrow key will move the her ...

Enhancing the appearance of CSS Tables

I need help with applying a gradient line below each row of data that I am fetching from my database and displaying in a <table>. I've been able to achieve the design using an <hr> element, as shown here: http://jsfiddle.net/ghrw3k8e/. Ho ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...