Need a smooth scroll in given code for a single page website, using tag it is working well but the effect is not smooth.
$('ul.nav-main').find('a').click(function() {
var $href = $(this).attr('href');
var $anchor = $('#' + $href).offset();
window.scrollTo($anchor.left, $anchor.top);
return false;
});
ul {
list-style: none;
margin-bottom: 100px;
}
li {
padding: 5px 15px;
}
div {
height: 500px;
margin-bottom: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><a href="#div1">Div 1</a></li>
<li><a href="#div2">Div 2</a></li>
<li><a href="#div3">Div 3</a></li>
</ul>
<div id="div1" style="background: red;">
Div 1
</div>
<div id="div2" style="background: green;">
Div 2
</div>
<div id="div3" style="background: blue;">
Div 3
</div>