Erik
Erik

Reputation: 5791

Scroll to bookmark on same page using jQuery?

I made a Q&A page. I'd like to click on the question and have the page scroll to the question using jQuery. What script do I use? Does anyone have an example I can learn from?

Upvotes: 1

Views: 2235

Answers (3)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23098

Really late at the party. Using bookmarks is the simplest solution (DVK's answer), but scrolling can be done in a more fancy way with animate:

$('html, body')
    .animate({
            // for fixed top div, you must substract its height from top
            scrollTop: $("#jumpBookmark").offset().top;
        },
        // animation duration
        500);

Upvotes: 0

Ian Devlin
Ian Devlin

Reputation: 18880

I'd suggest LocalScroll which I've used in the past and found very easy to implement.

Upvotes: 0

DVK
DVK

Reputation: 129519

In simple cases (depends on how you "made" your Q&A page), you don't even need JQuery or any JavaScript.

Simply mark your answers with a name tag (for example <A NAME="Answer11"/> ) and then have every question be wrapped into a link to correct tag, e.g. <LI><A HREF="#Answer11">Question 11</A>.

Upvotes: 1

Related Questions