Search code examples
jqueryhtmlsmooth-scrolling

Stop Smooth Scroll between Sections in a Middle Position


I have the following structured code. I have sticky header as well as few sections in my page. I have few in page hyperlinks here.

As you can see in the snippet there is a link text inside section 1 to section 2 .

I added some jquery from w3school for smooth scrolling.


PROBLEM

When the hyperlink is clicked it scroll to the section 2 and take the section 2's start point to the top of the body. Since I have a sticky header it hides some contents of section 2.

NOW WHAT I WANT IS : When scrolling to the section 2 I want the section to start after the sticky header, instead of starting at the top of the body.

// Add smooth scrolling to all links
$("a").on('click', function(event) {

  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: $(hash).offset().top
    }, 800, function() {

      // Add hash (#) to URL when done scrolling (default click behavior)
      window.location.hash = hash;
    });
  } // End if
});
header {
  background: red;
  width: 100%;
  height: 50px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

#sec-1,
#sec-2 {
  width: 100%;
  height: 100vh;
}

#sec-1 {
  margin-top: 50px;
  background: green;
}

#sec-2 {
  background: blue;
}

#sec-2>p {
  background: #fff;
  margin: auto;
  width: 60%;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>

<section id="sec-1">
  <a href="#sec-2">Scroll to section 2</a>
</section>
<section id="sec-2">
  <p>This is section 2</p>
</section>


Solution

  • Your problem is the last line in the javascript.

    / Add hash (#) to URL when done scrolling (default click behavior) window.location.hash = hash;

    This actually forces your URL to jump to the original hash position. You dont actually add an # but you force the window to jump to your original defined in the javascript variable hash which is in this case section-2.

    // Add smooth scrolling to all links
    $("a").on('click', function(event) {
    
      // Make sure this.hash has a value before overriding default behavior
      if (this.hash !== "") {
        // Prevent default anchor click behavior
        event.preventDefault();
    
        // Store hash
        var hash = this.hash;
    
        // Using jQuery's animate() method to add smooth page scroll
        // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
        $('html, body').animate({
          scrollTop: $(hash).offset().top -70
        }, 800, function() {
    
        });
      } // End if
    });
    

    The scroll works as intended now.

    https://jsfiddle.net/exuj6mro/18/