Vecta
Vecta

Reputation: 2350

jQuery scrollTop Trouble

I'm having trouble getting scrollTop to work on a search page I'm working on. Here's my code:

if(window.location.href.indexOf("Search") > -1) {
$("html, body").animate({scrollTop: $("#ditto_results").offset().top}, 2500);
});

I need to check the URL for the string "Search" to see if a search has been completed, and if so, scroll the page to the #ditto_results div. When I perform a search nothing happens though.

If I replace the first line with a click event it works fine. What am I doing wrong? Thanks for your help!

Upvotes: 0

Views: 172

Answers (1)

Robert Koritnik
Robert Koritnik

Reputation: 104999

You're probably immediately executing this code wherever it's positioned within your document... You should use document ready event instead which will execute immediately after document is ready so all elements are loaded (not necessarily their content ie. images) including your #ditto_results div element:

$(function(){
    if(window.location.href.indexOf("Search") > -1) {
        $("html, body").animate({scrollTop: $("#ditto_results").offset().top}, 2500);
    });
});

Upvotes: 1

Related Questions