Search code examples
javascripthtmlcssanchorscrollmagic

Anchor tags work going down the page, but not going up


I'm having a strange issue with my project where my anchor tags work going down the page, but doesn't seem to work going back up the page. I have managed to re-create this issue with as little code possible.

I've added a debug to the code example to give as much information as possible. The plug we use is the ScrollMagic feature & I have attempted to try and fix this in the JS, but to no avail. Any ideas on why the anchors do not scroll upward?

/*============================================================
    -----SCROLLMAGIC SETUP-----
============================================================*/
var ctrl = new ScrollMagic.Controller({
  globalSceneOptions: {
    triggerHook: 'onLeave'
  }
});

$("section").each(function() {
  var name = $(this).attr('id');
  new ScrollMagic.Scene({
    triggerElement: this
  })
    .setPin(this)
    .addIndicators({
    colorStart: "rgba(255,255,255,0.5)",
    colorEnd: "rgba(255,255,255,0.5)",
    colorTrigger : "rgba(255,255,255,1)",
    name:name
  })
    .loglevel(3)
    .addTo(ctrl);
});

var wh = window.innerHeight;
new ScrollMagic.Scene({
  offset: wh*3
});

/*============================================================
    -----SCROLLMAGIC ANCHORS/SCROLLTO-----
============================================================*/
$('header a').on('click',function() {
  var targetSection = $(this).attr('href').substring(1);
  var targetPerc = (targetSection-1) / ($('nav a').length-1);
  var targetPos = scene.scrollOffset() + (scene.duration()*targetPerc);
  controller.scrollTo(targetPos);
});

/*============================================================
    -----BOUNCEIN ANIMATIONS ON VIEW-----
============================================================*/
$(function() {
  AOS.init();
});
/* Make the body 100% of the browser viewport height */
html, body, .hero { height: 110%; margin: 0; }

/* Make each section 100% of the browser viewport height */
section { height: 100%; position: relative; }

/* Header to follow through the page */
header { position: fixed; left: 5%; top: 40%; z-index: 99; font-size: 18px; }

/* Background colors for each section */
section#one   { background: url('https://via.placeholder.com/1920x1080') center / cover; }
section#two   { background: linear-gradient(90deg, #F4F6F5 50%, #FFFFFF 50%);            }
section#three { background: url('https://via.placeholder.com/1920x1080') center / cover; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/ScrollMagic.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/aos@3.0.0-beta.5/dist/aos.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/aos@2.3.0/dist/aos.css" rel="stylesheet"/>
<!-- Header -->
<header id="navigation">
  <p><a href="#one">who we are</a></p>
  <p><a href="#two">what we do</a></p>
  <p><a href="#three">get in touch</a></p>
</header>

<!-- Section One -->
<section id="one">
  <div class="container text-center" data-aos="fade-up">
    <h1>One</h1>
  </div>
</section>

<!-- Section Two -->
<section id="two">
  <div class="container text-center" data-aos="fade-up" data-aos-offset="0">
    <h2>Two</h2>
  </div>
</section>

<!-- Section Three -->
<section id="three">
  <div class="container text-center" data-aos="fade-up" data-aos-offset="0">
    <h1>Three</h1>
  </div>
</section>


Solution

  • The code seems to work well , if the script is added as the last piece of the head element.

    http://jsfiddle.net/1f7e3akn/10/

    Also works if you add it as an external script, same place

    <script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.3/ScrollMagic.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://unpkg.com/aos@3.0.0-beta.5/dist/aos.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://unpkg.com/aos@2.3.0/dist/aos.css" rel="stylesheet"/>
    <script src="<your_script>"></script>