-1

I'm making a website and I need a nav item to scroll the website down. I already tried the easy way, which is to make the href link to an element and give scroll-behavior: smooth;. The issue I have with this solution is that I have quite a big header, so I need the scroll to stop a little bit before the actual element. So,I would like to know a way to scroll to a specific point on the webpage, rather than the actual element. Thank you

  • Does this answer your question? [Javascript scrollintoview smooth scroll and offset](https://stackoverflow.com/questions/49820013/javascript-scrollintoview-smooth-scroll-and-offset) – Andreas Jan 20 '20 at 17:35
  • Welcome to SO. Cou can just define an anchor a little bit where you want to ideally stop(no js, possible extra element needed for the stopping point). Of course you can scroll to a specific point which you need js for. But you might want to read https://stackoverflow.com/help/how-to-ask – Huangism Jan 20 '20 at 17:36

1 Answers1

0

Okay here is the way that I'm doing smooth scrollspy with JavaScript(JQuery), maybe it can help you .

$(document).ready(function () {
$('body').scrollspy({
    // Your navbar
    target: ".navbar",
    offset: 200
});
 // links in navbar
$("#myNavbar a").on('click', function (event) {
    if (this.hash !== "") {
        event.preventDefault();
        var hash = this.hash;
        $('html, body').animate({
            scrollTop: $(hash).offset().top
        }, 800, function () {


            window.location.hash = hash;
        });
    }
  });
});
GomuGomu
  • 304
  • 2
  • 9