How to fix the bug in counting the offset sticky block?

I have a project page which content block should be moved with the behavior, such sidbury kontaktika (te - scroller down - block skrollitsya to bottom and fixed up scroller - block skrollitsya to its top and a fixed). Plug-ins, I put the f*L. in your code Better to understand than somebody else's and significant.
Actually here is the page with the bug.

UPD: the page is now only for fullHD version and on the laptop should tomasturbate to -120%, sorry

Here is the code module on the page with the bug responsible for the sticky behavior:
var moduleName = 'projectBlock';
 var lastScrollTop = 0;
$(window).scroll(function(){
 var fix = $('.' + moduleName + '__fix');
 var baseMargin = fix.css('marginTop');
 var baseMarginNum = parseInt(baseMargin);
 var st = $(this).scrollTop();
 var windowBottom = st + $(this).outerHeight();

 //document vars
 var docHeight = $('body').outerHeight();
 var footHeight = $('.footer').outerHeight();
 var maxBottom = docHeight - footHeight;
 var maxBottomWithOffset = maxBottom - 120;
 //fix vars
 var fixHeight = fix.outerHeight();
 var fixOffTop = fix.offset().top;
 fixBottom = fixHeight + fixOffTop + 40;
 if (st > lastScrollTop){

 if(windowBottom >= fixBottom && fixBottom < (maxBottom + 93)){
 fix.css('position', 'fixed');
fix.css('bottom','40px');
 fix.css('top', 'auto');
 console.log(fixBottom, (maxBottom + 93), 'bug1');
}
 if(fixBottom > (maxBottom + 93)){
fix.css('position','absolute');
 fix.css('top', maxBottomWithOffset - fixHeight + 'px');
fix.css('bottom','auto');
 console.log(fixBottom, (maxBottom + 93), 'bug2');
}
}else{
fix.css('position','absolute');
fix.css('bottom','auto');
 fix.css('top', lastFixOffTop - 93 + 'px');
 if(fix.offset().top >= (st + 93)){
 fix.css('top', $(window).scrollTop() + 'px');
}
}
 lastFixOffTop = fixOffTop;
 lastScrollTop = st;
 });

I basically understand where the bug fixBottom recalculated from the translation fix.offset().top. But so is the allocation that fixBottom corresponds to one condition or another - I can not understand as well as I can not understand how such behavior to fix it....
August 23rd 19 at 10:46
0 answer

Find more questions by tags JavaScriptjQuery