A
A
AlinaHeldman2021-06-08 10:29:07
css
AlinaHeldman, 2021-06-08 10:29:07

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

3 answer(s)
B
BornTo FreeFall, 2021-06-08
@BornToFreeFall

For example, like this:

html {
    scroll-behavior: smooth;
}

But there is one side effect that not all browsers support this approach. But without JS)
Or with elements of a small JS code:
// Проходимся по всем элементам "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'
  })
  })
}

Y
Yaroslav, 2021-06-08
@shap3n

You can see FullPage. js

T
ThunderCat, 2021-06-08
@ThunderCat

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 question

Ask a Question

731 491 924 answers to any question