jQuery scrollTop() Scroll to Section 2 down below

Description

jQuery scrollTop() Scroll to Section 2 down below

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from   w  ww .  j ava2s  .c o m
<script>
$(document).ready(function(){
  $("a").on('click', function(event) {
    if (this.hash !== "") {
      event.preventDefault();
      let hash = this.hash;
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
        window.location.hash = hash;
      });
    }
  });
});
</script>
 <style>
body, html, .main {
  height: 100%;
}

section {
  min-height: 100%;
}
</style>
</head>
<body>

<a href="#section2">
Click Me to Smooth Scroll to Section 2 Below</a>

<div class="main">
  <section></section>
</div>

<div class="main" id="section2">
  <section style="background-color:blue"></section>
</div>

</body>
</html>



PreviousNext

Related