var container_1 = jQuery('#sticky-container_1'); var target_1 = jQuery('#sticky-target_1'); // first need to determine when to start scrolling and when to stop var container_offset_1 = container_1.offset(); var container_height_1 = container_1.height(); var target_height_1 = 0; var sticky_start_point_1 = 0; var target_stop_offset_1 = 0; var sticky_stop_point_1 = 0; var scroll_offset_1 = 0; var offset_sticky_stop_point_1 = 0 var scroll_offset_1 = 0; function calculate_start_stop_1(){ target_height_1 = target_1.height(); container_offset_1 = container_1.offset(); container_height_1 = container_1.height(); sticky_start_point_1 = container_offset_1.top + 80; target_stop_offset_1 = container_height_1 - target_height_1 - 0; sticky_stop_point_1 = container_offset_1.top + target_stop_offset_1 - 80; var tmp_offset_1 = container_1.offset(); scroll_offset_1 = tmp_offset_1.top - container_offset_1.top; offset_sticky_stop_point_1 = scroll_offset_1 + sticky_stop_point_1; /* console.log( '############ 1 #############' ); console.log( 'Scroll offset: ' + scroll_offset_1 ); console.log( 'target height ' + target_height_1 ); console.log( 'container_offset.top ' + container_offset_1.top ); console.log( 'target_stop_offset ' + target_stop_offset_1 ); console.log( 'start scrolling with page at ' + sticky_start_point_1 ); console.log( 'stop scrolling with page at ' + offset_sticky_stop_point_1 ); */ } jQuery(window).scroll(function() { if( window.innerWidth > 899 ){ calculate_start_stop_1(); var scroll_pos_1 = jQuery(window).scrollTop(); // console.log( 'Scroll position: ' + scroll_pos_1 ); if ( scroll_pos_1 > sticky_start_point_1 && scroll_pos_1 offset_sticky_stop_point_1 ){ target_1.css('position','absolute'); target_1.css('top', target_stop_offset_1+'px'); // console.log( "UNSTICK BOTTOM 1" ); } } else { jQuery(window).scroll(function(){ //console.log( 'container 1 height: ' + container_1.height() ); //console.log( 'target 1 height: ' + target_1.height() ); container_1.css( 'height' , target_1.height() ); }); } });