6

I am using jquery scrollTop(), and I have some issues

This is HTML

<a class="jumper" href="#first">Jump</a>

<div class="first"></div>
<div id="second"></div>
<div id="third"></div>
<div id="fourth"></div>
<div id="fifth"></div>

And Jquery

  $(document).ready(function () {

        $('.jumper').click(function () {

            $('html, body').animate({
                scrollTop: $("#fourth").offset().top
            }, 2000);

        });


    });

It is working OK, but what i need, is not to scroll element #third to top of page, just to scroll it little under about few px smaller like 100px, because i want to leave something in previous element to be seen to, is that possible?

Here is working fiddle

http://jsfiddle.net/X9FUg/4/

I want to leave yellow element to be seen abaout 100px?

1
  • 1
    $("#fourth").offset().top - 100? Commented Apr 15, 2014 at 6:40

2 Answers 2

14

Note that there may be better solutions... but the first thing that spings to mind is just subtracting 100.

$("#fourth").offset().top - 100

offset().top returns a number without the 'px' part, so doing offset().top - 100 should work just fine. (https://api.jquery.com/offset/)

Sign up to request clarification or add additional context in comments.

Comments

3

Yes, Try with below solution and it will work for you. Here instead of 100 you can set whatever value you want.

    $(document).ready(function () {
        $('.jumper').click(function () {
            $('html, body').animate({
              scrollTop: $("#fourth").offset().top - 100
            }, 2000);
        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.