Sticky scrolling shoppingcart with jQuery

Sticky scrolling shoppingcart with jQuery

This is a little jQuery snippet for a sticky scrolling shoppingcart like the one Apple uses at the Apple Onlinestore: http://store.apple.com/de/configure/MB950D/A?mco=MTM3NTgzMTk

 

Demo: http://michelgotta.de/demo-sticky-scrolling-shopping-cart/


jQuery:

$(document).ready(function() {  
    // check where the shoppingcart-div is  
    var offset = $('#shopping-cart').offset();  
    $(window).scroll(function () {    
        var scrollTop = $(window).scrollTop(); 
        // check the visible top of the browser     
        if (offset.top<scrollTop) {
            $('#shopping-cart').addClass('fixed'); 
        } else {
            $('#shopping-cart').removeClass('fixed');   
        }
    }); 
}); 


CSS:

 .fixed {    
    position: fixed;     
    top: 20px;    
    margin-left: 720px;    
    background-color: #0f0 ! important; 
}