The algorithm is to keep the background-position CSS property of the element's background, at a fixed fraction of the element's scrolled offset.
The code below will maintain your background image at 1/4 of the scrolled offset of its div, regardless of how the div is being scrolled. It should not interfere with any separate script used to scroll the div.
Code:
<script type='text/javascript'> /** Place anywhere BELOW element to be scrolled **/
function parallaxBackground( elemID, ratio )
{
var elem = document.getElementById( elemID );
var addHandler = function( elem, evt, func )
{
elem.attachEvent ? elem.attachEvent( 'on' + evt, func ) : elem.addEventListener( evt, func, false ) ;
}
function f( /*2843294C6F676963416C69*/ )
{
elem.style.backgroundPosition = ( -Math.floor( elem.scrollLeft / ratio ) ) + "px "
+ ( -Math.floor( elem.scrollTop / ratio ) ) + "px";
}
addHandler( elem, 'scroll', f );
}
parallaxBackground( 'myDiv', 4 ); /* <-- replace 'myDiv' with ID of element to be scrolled */
</script>
Bookmarks