Erik
Erik

Reputation: 5791

Add an anchor to scrollTop

Is it possible to add an anchor to this script? Instead of scrolling all the way to the top, I would like to scroll to a particular anchor in the page.

Here is the current script:

if (isError) {
$('html, body').animate({
    scrollTop: 700
}, 2500);
$("#error-div").show();
} else {
$("#error-div").hide();
}

Upvotes: 0

Views: 1493

Answers (3)

GNi33
GNi33

Reputation: 4509

There's not really the need to include a huge plugin for this simple task.

With jQuerys position() - function (jQuery Doc) you can get the position of an element in the DOM (as long as it's visible). It returns an object with a top and left - value, relative to the parent. If you want values relative to the document, use the offset() - function, usage stays the same.

I created a little fiddle to show you the basic usage:

http://jsfiddle.net/DtADS/

Upvotes: 3

laxris
laxris

Reputation: 481

If I understood your question, you need to place something like that on the place in your page you want to scroll <div id="scrollHere"></div> and in your jquery:

window.location.href = "#scrollHere";

this method goes the page right where the #scrollHere rest, if that the case.

Upvotes: 0

user1289347
user1289347

Reputation: 2407

Use the scrollto plugin, you can scroll the entire page to any element, or even scroll inside of elements. http://demos.flesler.com/jquery/scrollTo/

Upvotes: 0

Related Questions