What I'd like to do is set the offset for scrollspy as well as give it a smooth transition to each section. I would prefer to not use a plugin or data-attributes (If data-attributes are necessary then ok), but I would like a solution that takes the javascript below and adds the smooth animated scroll effect as well. Jquery solution if possible.
Scrollspy works and the offset works with this code below: Possibly someone can take this code and add to it to set the smooth transition as well?
var offset = 60;
$('.navbar li a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
My HTML/CSS code is setup as follows:
I have a container div with the class .main-content. This is set in the css with a margin which offsets it from my header and footer which are fixed top and bottom and have a height of 60px each.
The CSS for <div class="main-content"></div>
is as follows:
.main-content {position: realtive; width: 100%; height: 100%; margin: 60px 0 60px 0;}
I have sections within .main-content and each section has an id="..." and the HTML looks like this. I've removed all content so you can see the sections clearly.
<div class="main-content" data-spy="scroll" data-target="#dl-menu">
<section id="top" class="top">
...
</section>
<section id="services" class="services">
...
</section>
<section id="portfolio" class="portfolio">
...
</section>
<section id="pricing" class="pricing">
...
</section>
<section id="faq" class="faq">
...
</section>
<section id="quote" class="quote">
...
</section>
</div>
The menu I'm using has a structure like so:
<div id="dl-menu" class="dl-menuwrapper">
<button class="dl-trigger">Open Menu</button>
<ul class="dl-menu">
<li><a href="#top">BACK TO TOP</a></li>
<li><a href="#services">OUR SERVICES</a></li>
<li><a href="#portfolio">OUR PORTFOLIO</a></li>
<li><a href="#faq">FAQ</a></li>
<li><a href="#quote">GET YOUR QUOTE</a></li>
</ul>
</div>
I'm quite new to Jquery/JavaScript and have tried every solution I could find, but have not found a solution that works with my layout. If anyone can help me figure this out I would greatly appreciate it!