Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2013, 02:14 PM   PM User | #1
naitsabes
New to the CF scene

 
Join Date: Jan 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
naitsabes is an unknown quantity at this point
Help with 'sticky' header like instagram

i run a function 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.

It works with this code when all posts have the same height, but as soon as posts have different heights it doesnt work anymore.

JS
Code:
 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();

    }); 
});
CSS

Code:
  .pagnering {
 margin-top: 72px;
        position: absolute;
    }
    .pagnering.fixed {
        position: fixed;
        width: 99.6%;
        z-index: 0;
        top: 0px;
    }
    .pagnering.fixed.absolute {
        position: absolute;
    }
naitsabes is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:38 AM.


Advertisement
Log in to turn off these ads.