code

Materializecss

Materialize css

html

<link
  rel="stylesheet"
  href="//cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/css/materialize.min.css"
/>
<script src="//cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script>
<div class="row">
  <div class="col s12 m9 l10">
    <div id="introduction" class="section scrollspy">
      <p>
        order cruelty disagree censorship vegetable polish exercise precede
        parking seminar cell dependence landowner inject practical air hand put
        inspiration refuse unity press stop apple art interrupt fibre leaflet
        improvement concept addicted lace attraction disappear call trouble
        discover critical rice partner familiar needle extort uniform insurance
        mosaic stress peel shape medieval rain quantity vigorous field
        microphone violation bundle bride station researcher civilian pedestrian
        chart sugar pace satisfaction pastel plastic boom confine promote
        competence practice siege glimpse boot live pride hypothesis parameter
        folklore traffic ambiguity question pig ecstasy insist brag stubborn
        loot proud hate direct overlook authority garage fill catch write
        liberal bulb brain investment admiration rub arm brother worker horn
        computer virus prestige promotion threshold trace picture pension tap
        head introduction steel notorious hit attic ticket sea decisive visible
        pure failure queen error thought visit rule ruin scale rider loan revive
        recommendation skip timber performer refer integration smile user
        catalogue driver slide progress identity cry style afford train unfair
        suggest charter bet help wording crisis touch coal coast sip hard
        crosswalk respectable penny terrace look range trust cabinet watch
        deviation fare sustain term grief white series single harmful cunning
        soap kitchen session angle dragon biology merchant rush challenge peak
        cause butterfly chew distribute grind wolf adviser frown ash frequency
        object narrow roof institution ordinary conventional advantage forget
        hero disturbance remedy dinner denial guerrilla surround junior jelly
        supplementary law meet tie behave provoke second visual jam random
        system indoor fit available export bowel blank stretch hospital west
        button conclusion toll trainer polite efflux motivation abolish pumpkin
        thoughtful dance maximum calculation gasp domestic shine favour fossil
        punish bad mix save expectation top dictate death statement observation
        funny mood integrated pin connection charm salad precedent deteriorate
        poetry acquaintance positive human body helpless acid assumption host
        calorie clock disappointment perform spring meal bulletin bang rape
        option heaven clarify revenge trench invasion explosion structure preach
        unlike beginning confuse sword trial pony avenue follow master world
        aloof compete appetite rumor settle painter abandon fiction payment
        insistence constellation paper grip standard literacy tasty text north
        elapse discovery chase node building number radio display decorative
        favor projection linear fraction arrogant lifestyle resign support craft
        disclose outfit fountain wound hook promise museum growth blackmail
        railroad virus principle buffet tycoon witch offense width network
        timetable pity wall cake stem tense false salesperson chip guest thread
        battle reporter coalition comment youth fate freighter switch retailer
        monstrous cherry guide scandal warning award injection control pick
        texture
      </p>
    </div>

    <div id="structure" class="section scrollspy">
      <p>
        order cruelty disagree censorship vegetable polish exercise precede
        parking seminar cell dependence landowner inject practical air hand put
        inspiration refuse unity press stop apple art interrupt fibre leaflet
        improvement concept addicted lace attraction disappear call trouble
        discover critical rice partner familiar needle extort uniform insurance
        mosaic stress peel shape medieval rain quantity vigorous field
        microphone violation bundle bride station researcher civilian pedestrian
        chart sugar pace satisfaction pastel plastic boom confine promote
        competence practice siege glimpse boot live pride hypothesis parameter
        folklore traffic ambiguity question pig ecstasy insist brag stubborn
        loot proud hate direct overlook authority garage fill catch write
        liberal bulb brain investment admiration rub arm brother worker horn
        computer virus prestige promotion threshold trace picture pension tap
        head introduction steel notorious hit attic ticket sea decisive visible
        pure failure queen error thought visit rule ruin scale rider loan revive
        recommendation skip timber performer refer integration smile user
        catalogue driver slide progress identity cry style afford train unfair
        suggest charter bet help wording crisis touch coal coast sip hard
        crosswalk respectable penny terrace look range trust cabinet watch
        deviation fare sustain term grief white series single harmful cunning
        soap kitchen session angle dragon biology merchant rush challenge peak
        cause butterfly chew distribute grind wolf adviser frown ash frequency
        object narrow roof institution ordinary conventional advantage forget
        hero disturbance remedy dinner denial guerrilla surround junior jelly
        supplementary law meet tie behave provoke second visual jam random
        system indoor fit available export bowel blank stretch hospital west
        button conclusion toll trainer polite efflux motivation abolish pumpkin
        thoughtful dance maximum calculation gasp domestic shine favour fossil
        punish bad mix save expectation top dictate death statement observation
        funny mood integrated pin connection charm salad precedent deteriorate
        poetry acquaintance positive human body helpless acid assumption host
        calorie clock disappointment perform spring meal bulletin bang rape
        option heaven clarify revenge trench invasion explosion structure preach
        unlike beginning confuse sword trial pony avenue follow master world
        aloof compete appetite rumor settle painter abandon fiction payment
        insistence constellation paper grip standard literacy tasty text north
        elapse discovery chase node building number radio display decorative
        favor projection linear fraction arrogant lifestyle resign support craft
        disclose outfit fountain wound hook promise museum growth blackmail
        railroad virus principle buffet tycoon witch offense width network
        timetable pity wall cake stem tense false salesperson chip guest thread
        battle reporter coalition comment youth fate freighter switch retailer
        monstrous cherry guide scandal warning award injection control pick
        texture
      </p>
    </div>

    <div id="initialization" class="section scrollspy">
      <p>
        order cruelty disagree censorship vegetable polish exercise precede
        parking seminar cell dependence landowner inject practical air hand put
        inspiration refuse unity press stop apple art interrupt fibre leaflet
        improvement concept addicted lace attraction disappear call trouble
        discover critical rice partner familiar needle extort uniform insurance
        mosaic stress peel shape medieval rain quantity vigorous field
        microphone violation bundle bride station researcher civilian pedestrian
        chart sugar pace satisfaction pastel plastic boom confine promote
        competence practice siege glimpse boot live pride hypothesis parameter
        folklore traffic ambiguity question pig ecstasy insist brag stubborn
        loot proud hate direct overlook authority garage fill catch write
        liberal bulb brain investment admiration rub arm brother worker horn
        computer virus prestige promotion threshold trace picture pension tap
        head introduction steel notorious hit attic ticket sea decisive visible
        pure failure queen error thought visit rule ruin scale rider loan revive
        recommendation skip timber performer refer integration smile user
        catalogue driver slide progress identity cry style afford train unfair
        suggest charter bet help wording crisis touch coal coast sip hard
        crosswalk respectable penny terrace look range trust cabinet watch
        deviation fare sustain term grief white series single harmful cunning
        soap kitchen session angle dragon biology merchant rush challenge peak
        cause butterfly chew distribute grind wolf adviser frown ash frequency
        object narrow roof institution ordinary conventional advantage forget
        hero disturbance remedy dinner denial guerrilla surround junior jelly
        supplementary law meet tie behave provoke second visual jam random
        system indoor fit available export bowel blank stretch hospital west
        button conclusion toll trainer polite efflux motivation abolish pumpkin
        thoughtful dance maximum calculation gasp domestic shine favour fossil
        punish bad mix save expectation top dictate death statement observation
        funny mood integrated pin connection charm salad precedent deteriorate
        poetry acquaintance positive human body helpless acid assumption host
        calorie clock disappointment perform spring meal bulletin bang rape
        option heaven clarify revenge trench invasion explosion structure preach
        unlike beginning confuse sword trial pony avenue follow master world
        aloof compete appetite rumor settle painter abandon fiction payment
        insistence constellation paper grip standard literacy tasty text north
        elapse discovery chase node building number radio display decorative
        favor projection linear fraction arrogant lifestyle resign support craft
        disclose outfit fountain wound hook promise museum growth blackmail
        railroad virus principle buffet tycoon witch offense width network
        timetable pity wall cake stem tense false salesperson chip guest thread
        battle reporter coalition comment youth fate freighter switch retailer
        monstrous cherry guide scandal warning award injection control pick
        texture
      </p>
    </div>
  </div>
  <div id="scrollspy-nav" class="col hide-on-small-only m3 l2">
    <ul class="section table-of-contents">
      <li><a href="#introduction">Introduction</a></li>
      <li><a href="#structure">Structure</a></li>
      <li><a href="#initialization">Intialization</a></li>
    </ul>
  </div>
</div>

css

#scrollspy-nav {
  position: sticky;
  top: 0;
}

#scrollspy-nav .active {
  color: red;
}

javascript

$(function () {
  // $('.scrollspy').scrollSpy();
  // {
  //   scrollOffset: 1,
  //   getActiveElement: function(id) {
  //     console.log('getActiveElement id:',id);
  //     return 'a[href="#' + id + '"]';
  //   }
  // });

  $(".scrollspy").scrollSpy();

  $(".scrollspy").on("scrollSpy:enter", function () {
    console.log("scrollSpy:enter! $(this):", $(this));
    // $('.navbar-fixed').find('a[href="#'+$(this).attr('id')+'"]').parent().addClass('active');
  });
  $("scrollspy").on("scrollSpy:exit", function () {
    console.log("scrollSpy:exit! $(this):", $(this));
    // $('.navbar-fixed').find('a[href="#'+$(this).attr('id')+'"]').parent().removeClass('active');
  });

  // $('.scrollspy').waypoint(function(direction) {
  //   console.log('WAYPOINT!! direction:',direction);
  //   jQuery(this).addClass('on').next();
  // });
});

this demo preserves the compiled output and may depend on external libraries or services that are no longer available.

original jsfiddle post