I’m implementing smooth scrolling on my website using JavaScript, and I’ve encountered an issue with CSS scroll-margin-top causing a jump at the end. I’m using the following CSS for scroll margins:
class or div {
scroll-margin-top: 6rem;/*any size*/
}
I have a smooth scroll JavaScript function as well. The problem is that when scrolling to these sections, there’s a noticeable jump when reaching the end of the scroll.
Here is the JavaScript code:
var ssSmoothScroll = function () {
$('a[href^="#"], area[href^="#"]').on('click', function (e) {
var target = $($(this).attr('href'));
if (target.length) {
e.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, cfg.scrollDuration, 'swing', function () {
window.location.hash = target.attr('id');
});
}
});
};
How can I resolve this issue and maintain smooth scrolling without the jump at the end?
2
Answers
I think you don’t have to use
scroll-margin-top
, you can use justmargin
.Thanks.
the
scroll-margin-top
value for the target element and subtracts it from the scrolling destination.