Answer the question
In order to leave comments, you need to log in
How to make a smooth transition to the anchor (layout)?
It just doesn't work from what I've found. no smooth, no scripts, nothing...
Answer the question
In order to leave comments, you need to log in
For example, like this:
html {
scroll-behavior: smooth;
}
// Проходимся по всем элементам "linkList", и делаем выборку по ссылкам (a с атрибутом href, который равен "#")
const anchors = document.querySelectorAll('.linkList a[href*="#"]')
for (let anchor of anchors) {
anchor.addEventListener('click', function (e) {
e.preventDefault()
const blockID = anchor.getAttribute('href').substr(1)
document.getElementById(blockID).scrollIntoView({
behavior: 'smooth',
block: 'start'
})
})
}
Solution on jquery + vanilla
Pros - works beautifully, neatly, including with dynamically loaded elements
Cons - jkveri is needed, although it can be rewritten without it, but it will be longer and I'm lazy )
$("body").on('click', '[href*="#"]', function(e){
e.preventDefault();
var fixed_offset = 100;
if(this.hash){
var tp = parseInt($(document.querySelector(this.hash)).offset().top);
$('html,body').stop().animate({ scrollTop: tp - fixed_offset }, 1000);
};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question