user3766656
user3766656

Reputation: 23

How to add a css class into my function

Just starting out with jquery. I have written the following function:

jQuery(document).ready(function($){
    $('.tb-to-side-menu > li > a').on('click', function() {

        $('html, body').animate({
            scrollTop: $(this.hash).offset().top
        }, 1000);

        return false;
    });
});

This is for a 'one page' styled site, so when the use clicks on the nav they get smooth scrolling down. Problem is that I have a sticky nav and the content is sliding too far up. Is it possible to add a css class in with jquery that allows for the height of my nav div?

Thanks

Upvotes: 0

Views: 34

Answers (2)

Jason Foglia
Jason Foglia

Reputation: 2531

You can use: $(ele).addClass("classname")

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

If I've understood the issue, you could subtract the height of the fixed header from $(this.hash).offset().top, for example:

$('html, body').animate({
    scrollTop: $(this.hash).offset().top - $('#fixedHeader').height()
}, 1000);

Upvotes: 2

Related Questions