sticky header probelm

sticky header probelm

i run a script which makes a header sticky. There are several posts under each other and each of them have a header.
What im trying to do is to make headers sticky (fixed) when they are 'top: 72px'. Before they become sticky they are absolute. When a new header is approaching the sticky header, eg. when the new header is 150px below the sticky header id like the sticky header to become absolute and when the new header is top 72 
 id like the new one to become the sticky.
By trial and error i managed to this, but as all posts differ in height it only worked for the 2 first, then everything got of set.  What i did was to add  line 24 and 25 and '-200' on line 33.
best
 
  1. function stickyTitles(stickies) {

  2.     this.load = function() {

  3.         stickies.each(function(){

  4.             var thisSticky = jQuery(this).wrap('<div class="followWrap" />');

  5.             jQuery.data(thisSticky[0], 'pos', thisSticky.offset().top);

  6.         });
  7.     }

  8.     this.scroll = function() {

  9.         stickies.each(function(i){


  10.             var thisSticky = jQuery(this),
  11.                 nextSticky = stickies.eq(i+1),
  12.                 prevSticky = stickies.eq(i-1),
  13.                 pos = jQuery.data(thisSticky[0], 'pos');
  14. var diff = -23;
  15. var differ = 201;


  16.             if (pos + diff <= jQuery(window).scrollTop()) {

  17.                 thisSticky.addClass("fixed");
  18.                 if (nextSticky.length  > 0 && thisSticky.offset().top + differ  >= jQuery.data(nextSticky[0], 'pos') - thisSticky.outerHeight()) {

  19.                     thisSticky.addClass("absolute").css("top", jQuery.data(nextSticky[0], 'pos') - thisSticky.outerHeight()-200);

  20.                 }

  21.             } else {

  22.                 thisSticky.removeClass("fixed");

  23.                 if (prevSticky.length > 0 && jQuery(window).scrollTop() + differ<= jQuery.data(thisSticky[0], 'pos')  - prevSticky.outerHeight()) {

  24.                     prevSticky.removeClass("absolute").removeAttr("style");

  25.                 }

  26.             }
  27.         });         
  28.     }
  29. }

  30. jQuery(document).ready(function(){

  31.     var newStickies = new stickyTitles(jQuery(".pagnering"));

  32.     newStickies.load();

  33.     jQuery(window).on("scroll", function() {

  34.         newStickies.scroll();

  35.     }); 
  36. });

  1.     .pagnering {
  2. margin-top: 72px;
  3.         position: absolute;
  4.     }
  5.     .pagnering.fixed {
  6.         position: fixed;
  7.         width: 99.6%;
  8.         z-index: 0;
  9.         top: 0px;
  10.     }
  11.     .pagnering.fixed.absolute {
  12.         position: absolute;
  13.     }