0

I'm using scrollsaver.js (http://en.hasheminezhad.com/scrollsaver) to maintain scroll positions of elements on postback. There are instances where I wish to reset scroll position though, and I am having trouble doing so.

Paging through a list, I wish to scroll to the top on each page change. I do this by registering a call to resetGridScroll (below) in the paging handler.

scrollsaver.js ties to the page load event. When I do the same, my function runs before scrollsaver.js loads positions. Is there a way to force mine to run after page load?

function resetGridScroll() {
    $(document).ready(function () {
        $("div.items").animate({ scrollTop: 0, easing: 'swing', queue: false }, 15000);
    });
}

EDIT: I ended up clearing the cookie scrollsaver.js sets to store scroll positions. It gets cleared before the script loads so all positions are reset. Not elegant, but workable.

2 Answers 2

2

If you need to load an external js script and execute code afer it you could use $.getScript() and put the code in the callback:

$.getScript("scrollsaver.js", function(){
    //code to execute after the script has loaded here
});
Sign up to request clarification or add additional context in comments.

Comments

0

Put the function that calls the resetGridScroll() within :

$(document).ready(function () {
 //here
});

Not your function being called for example :

$(document).ready(function () {
   resetGridScroll();
});


function resetGridScroll() {
    $("div.items").scrollType(0);
    $("div.items").animate({ scrollTop: 0, easing: 'swing', queue: false }, 15000);
}

4 Comments

That still runs before scrollsaver.js does its thing. Also, I'm unclear as to the difference between .ready(innerFunction()) and outerFunction(.ready(innerFunction())). I'm using outerFunction to call the reset only when I need it.
the ready() function in jQuery is actually an event - its an event thats triggered the DOM has loaded - you cannot put it within a function and call it the way you were..... if your script is running too early you need to attach it to an even that the other script triggers ? if there is one ? or call the method that init's the other script first then call this method ... if you include some more details in your question it might help - ie how you call the other function
Perhaps there is a callback available for onload in scrollsaver that you can use. Or you can always write one in.
@Matt Maybe you can call a callback after the script has loaded

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.