JSFiddle - React, Tailwind, and code Playground
HTML
<nav id="nav">
<ul>
<li><a href="#page2" onclick="scrollToPage2_method2();return false">JS animate solution
</a> </li>
</ul>
</nav>
<article id="page1" class="wrap">PAGE1</article>
<article id="page2" class="wrap">PAGE2
<div id="top">
<a onclick="scrollToTop_method2();return false">
<p>JS animate</p>
<p>top</p>
</a>
</div>
</article>
CSS
* {
padding: 0;
margin: 0;
}
nav ul {
list-style: none;
float: left;
}
nav ul li {
display: inline-block;
margin: 0 20px;
}
#nav {
position: fixed;
width: 100%;
height: 10px;
background: #48D1CC;
color: #fff;
padding: 30px;
text-align: center;
z-index: 2;
}
a {
text-decoration: none;
display: inline-block;
color: #fff;
}
#page1 {
width: 100%;
height: 700px;
background: #3A5FCD;
color: #fff;
line-height: 700px;
font-size: 100px;
text-align: center;
}
#page2 {
width: 100%;
height: 700px;
background: #EE6A50;
color: #fff;
line-height: 700px;
font-size: 100px;
text-align: center;
}
#top {
float: right;
position: relative;
top: 550px;
font-size: 20px;
z-index: 1;
width: 100px;
height: 100px;
background: #333;
color: #fff;
line-height: 50px;
text-align: center;
margin: 10px;
}
JavaScript
function scrollToPage2_method2() {
var $page2 = $("#page2");
var offset = $page2.offset();
console.log(offset.top);
$('html,body').animate({
scrollTop: offset.top
}, 1000);
};
function scrollToTop_method2() {
$('html,body').animate({
scrollTop: 0
}, 1000);
}