Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript adding margin-top to scroll.js

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37763
    amyth91
    Participant

    hi, here is the scroll.js, which i am using to create a smooth scrolling effect, now in the page, when i use the scroll.js and click on my links the scroll works fine, but the page sticks right to the top, as it margin-top is set to 0.

    how do i add a margin of “50” to this js, so that whenever i scroll, the page has a margin-top:50px

    #101714
    Taufik Nurrohman
    Participant

    Decrease the offset top:

    var targetOffset = $target.offset().top-50;
    #101741
    Taufik Nurrohman
    Participant

    It is because the animation callback change the URL hash after animation has finished. See here:

    $(this).click(function(event) {
    event.preventDefault();
    $(scrollElem).animate({scrollTop: targetOffset}, 1500, function() {
    location.hash = target; // <== remove this!
    });
    });

    change to:

    $(this).click(function(event) {
    event.preventDefault();
    $(scrollElem).animate({scrollTop: targetOffset}, 1500);
    });

    http://codepen.io/pen/74/1

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.