Answer the question
In order to leave comments, you need to log in
How to make smooth scrolling to anchor?
I used this jq code:
$('a[href^="#"]').click(function(){
var target = $(this).attr('href');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 500);
});
Answer the question
In order to leave comments, you need to log in
school mathematics course. S = v * t
you need the same speed (v), so you need to select the correct time (t, which you will enter into the animate function) depending on the path (S).
in total we get the formula t = S/v;
so you are looking for a path from the current scrollTop to the offset().top of your element - this will be (S).
well, you already select the speed by poking.
If in Jquery, then something like this
$('a[href*="#"]').click(function(e) {
const $target = $(this.hash);
const topOffset = 100;
if ($target.length) {
e.preventDefault();
$("html, body")
.stop()
.animate(
{
scrollTop: $target.offset().top - topOffset
},
900,
"swing",
function() {
history.pushState(
{},
document.title,
window.location.pathname + target
);
return false;
// window.location.hash = target;
}
);
}
});
It may be enough for you to get by with CSS (scroll-snap property set) if browser support suits. A few articles on the topic:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question