D
D
Dmitry2015-10-08 15:06:29
css
Dmitry, 2015-10-08 15:06:29

How to handle page height when going to anchors?

Hello. Anchors are installed on the site, and when you navigate through them, due to the header glued at the top, the page shifts below the anchor exactly to the height of this header. for the anchors on the index page, I made smooth scrolling.

$("a.slide_to_anchor").click(function () {
        var elementClick = $(this).attr("href")
        var destination = $(elementClick).offset().top - $("#undefined-sticky-wrapper").height(); // отнимаем высоту хедера
        jQuery("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, 1000); // плавная анимация
        return false;
    });

But now how to process anchors on other pages (for example, transition from the main page to /news/) so that it is displayed normally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
littleguga, 2015-10-08
@Itachi261092

As an option, when loading the page, check for an anchor in the address and scroll up to the height of the header.

window.onload = function(){
    if(window.location.hash) {
      //если есть якорь, то скроллим вниз на высоту шапки
    } else {
      //здесь если нет, можно убрать блок else
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question