Archive February 2014

javascript : scroll window or container to view element

prototypsjs framework is used for that.

– scroll container to view element inside : scrollTo(myElement, divContainer);
– scroll window to view element inside : scrollTo(myElement, null);

scrollTo(myElement, scrollContainer){
    if(scrollContainer){
        myElement.scrollIntoView();
    }else{
        currentWindowScrollY = document.documentElement.scrollTop;
        targetWindowScrollY = Element.cumulativeOffset(myElement)[1]-window.height()+myElement.getHeight();
        if(targetWindowScrollY>currentWindowScrollY){
            window.scrollTo(window.scrollX, targetWindowScrollY);
        }
    }
}