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
- function stickyTitles(stickies) {
- this.load = function() {
- stickies.each(function(){
- var thisSticky = jQuery(this).wrap('<div class="followWrap" />');
- jQuery.data(thisSticky[0], 'pos', thisSticky.offset().top);
- });
- }
- this.scroll = function() {
- stickies.each(function(i){
- var thisSticky = jQuery(this),
- nextSticky = stickies.eq(i+1),
- prevSticky = stickies.eq(i-1),
- pos = jQuery.data(thisSticky[0], 'pos');
- var diff = -23;
- var differ = 201;
- if (pos + diff <= jQuery(window).scrollTop()) {
- thisSticky.addClass("fixed");
- if (nextSticky.length > 0 && thisSticky.offset().top + differ >= jQuery.data(nextSticky[0], 'pos') - thisSticky.outerHeight()) {
- thisSticky.addClass("absolute").css("top", jQuery.data(nextSticky[0], 'pos') - thisSticky.outerHeight()-200);
- }
- } else {
- thisSticky.removeClass("fixed");
- if (prevSticky.length > 0 && jQuery(window).scrollTop() + differ<= jQuery.data(thisSticky[0], 'pos') - prevSticky.outerHeight()) {
- prevSticky.removeClass("absolute").removeAttr("style");
- }
- }
- });
- }
- }
- jQuery(document).ready(function(){
- var newStickies = new stickyTitles(jQuery(".pagnering"));
- newStickies.load();
- jQuery(window).on("scroll", function() {
- newStickies.scroll();
- });
- });
- .pagnering {
- margin-top: 72px;
- position: absolute;
- }
- .pagnering.fixed {
- position: fixed;
- width: 99.6%;
- z-index: 0;
- top: 0px;
- }
- .pagnering.fixed.absolute {
- position: absolute;
- }