3

Any value I set to data-offset for scrollspy does not work at all. Here are the relevant html, css, and js code:

HTML

<nav class="navbar navbar-fixed-top navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><strong>YOUNG'S PORTFOLIO</strong></a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right enlargeItem">
        <li><a href="#aboutBox">ABOUT</a></li>
        <li><a href="#portfolioBox">PORTFOLIO</a></li>
        <li><a href="#contactBox">CONTACT</a></li>
      </ul>
    </div>
  </div>
</nav>

CSS

body {
      background-color: gray;
      padding-top: 110px;
      data-spy='scroll';
      data-target='.navbar';
      data-offset='';
      position: relative;
    }

JS

$('body').scrollspy();

When I scroll to target area where a tag is defined (#aboutBox, #portfolioBox, #contactBox), it does highlight the appropriate list item.

However, it is off a bit, so I wanted to change the offset by changing the data-offset but changing the data-offset does not do anything.

Here is the link to the codepen if someone wants to take a look at the whole thing

Danii
  • 99
  • 1
  • 6

3 Answers3

4

Argh. Couple things wrong with my code.

  1. I was trying to set attributes using CSS! WOW rookie mistake. I need some sleep.
  2. I was using navbar-fixed-top class. So I had to manually offset by 150px.

I applied this to my javascript:

$("body").attr({
    "data-spy": "scroll",
    "data-target": ".navbar"
  }).scrollspy({
    offset: 150
  });
Danii
  • 99
  • 1
  • 6
0

Come across this thread as i am looking to find out if you can have a negative value on data-offset.

In response to this post You don't have to add any extra JS or CSS for scrollspy to work it's all bundled with bootstrap, you would just add the attributes to the body tag. ref: https://www.w3schools.com/bootstrap4/bootstrap_scrollspy.asp

<body data-spy="scroll" data-target=".navbar" data-offset="150">
0

Try this code :

$(document).ready(function () {
    offset = 10; // get extra padding
    nav_top = $("#tsnav"); // get the top navbar hieght 
    side_nav = $("#cve_info") // target nav
    nav_height = nav_top.outerHeight() + offset; // actual height from top where content go and fixed 
    
    side_nav.find("a").on('click', function () {
        let $el = $(this);
            id = $el.attr("href");

        $('html, body').scrollTop( $(id).offset().top - nav_height);
        return false;
    })
});
chrslg
  • 9,023
  • 5
  • 17
  • 31
  • Please, avoid "code only" answer, especially for old questions, since they don't really help people who may encounter similar problem, how hey could adapt it. If possible, it would be nice to take some times to explain why and how this answer works. – chrslg Nov 29 '22 at 00:33