sidebar jquery

sidebar jquery

Hi,

I would like my animate sidebar to start to shift only when its bottom part reached the middle (vertical position) of the window.
So far the jquery script which I use makes my sidebar to move immediately after the top of the sidebar reach the top of the window...
This situation was ok until my sidebar was of small dimensions... but now, I added new content and it scroll down before i can reach the end of it.

what to do?

This is the jQuery code: 

  1. jQuery(function() {

        var jQuerysidebar = jQuery("#sidebar"),
            jQuerywindow = jQuery(window),
            offset = jQuerysidebar.offset(),
            topPadding = 15,
            stop = jQuery("#footer").offset().top;

        jQuerywindow.scroll(function() {
            if (jQuerywindow.scrollTop() > offset.top) {
                if (jQuerywindow.scrollTop() + jQuerysidebar.height() < stop) {
                    jQuerysidebar.stop().animate({
                        marginTop: jQuerywindow.scrollTop() - offset.top + topPadding
                    });
                }
            } else {
                jQuerysidebar.stop().animate({
                    marginTop: 0
                });
            }
        });

    });
























I believe I need to add a new if statement... i tried to "play" with the bottom position of the sidebar (offset.top - jQuerysidebar.height(), ) but I was not lucky...

Any help will be appreciate (I am new with Jquery...)

Thanks a lot:)