We saw how to create a smooth scrolling using CSS in this post: How To Create A Smooth Scrolling Effect With CSS?
But scroll-behavior property is not supported by all browsers so we can use JavaScript or jQuery to achieve cross-browser smooth scrolling solution.
Solution using ID
$(document).ready(function () {
$("#elementID").on("click", function (event) {
event.preventDefault();
var element = $(this);
$("html, body").animate(
{
scrollTop: element.offset().top,
},
800
);
});
});
Solution using a tag
$(document).ready(function () {
$("a").on("click", function (event) {
if (this.hash !== "") {
event.preventDefault();
var hash = this.hash;
$("html, body").animate(
{
scrollTop: $(hash).offset().top,
},
800,
function () {
window.location.hash = hash;
}
);
}
});
});
Source: w3schools
Apple browsers chrome cross-browser firefox property safari scroll scroll-behavior solution style