views:

236

answers:

1

Hi folks,

First of all I did not write this code. I found it on somebody else's website and I want to learn from it by trying it out myself. However I can't make it work. I've googled for the code in case it's a jQuery plugin that's freely available or anything, but I can't find it anywhere on the web.

I have my sidebar (with id #sidebar) and have given it the class "sticky", I've included jQuery at the top of the page, and I've put this code in place in the head:

<!-- Floating sidebar jQuery --> 
        <script type="text/javascript"> 
            var Sticky = function( $obj, opts ){

               $(window).scroll( 
                  function(e){
                     Sticky.onScroll(e, $obj, opts );
                  });

            }
            Sticky.onScroll = function( e, $o, opts ){

               var iScrollTop = $(window).scrollTop();
               var sClass = "sticky";

               //set original data
               if( !$o.data(sClass) ){
                  $o.data(sClass, {css:{position:$o.css('position'),top:$o.css('top')}, offset:$o.offset()} );
               }
               var oOrig = $o.data(sClass);
               var bIsSticky = $o.hasClass(sClass);

               if( iScrollTop > oOrig.offset.top && !bIsSticky ){
                  $o.css({position:'fixed',top:0}).addClass(sClass);
               }else if(iScrollTop < oOrig.offset.top && bIsSticky){
                  $o.css(oOrig.css).removeClass(sClass);
               }   

            }

            Sticky( $('#sidebar') );

        </script> 

As you can see, the final JS line Sticky( $('#sidebar') ); fires on the #sidebar element. However, when you scroll down, this error is written to Chrome's log:

Uncaught TypeError: Cannot read property 'offset' of undefined

Firebug is a bit more verbose, and says:

oOrig is undefined: if( iScrollTop > oOrig.offset.top && !bIsSticky ){

I'm trying my best to understand this but can somebody help me see why it's not working?

Thanks!

Jack

+4  A: 

Wow, new answer...Thanks felix

wrap the function call in a ready function.

$(function() {
    Sticky($('#sidebar'));
});

The dom is most likely not ready when you call Sticky($('#sidebar')) so when .data is used to set data on $o it actual does nothing:

$o.data(sClass, {css:{position:$o.css('position'),top:$o.css('top')}, offset:$o.offset()} );].  

So when it gets the data on line:

var oOrig = $o.data(sClass);

it cannot actualy get the data.

This is because the dom elements are not ready to be manipulated because the dom is not ready yet.

OLD ANSWER (NOT RIGHT!)

$.offset is a function.

The problem in the line:

if( iScrollTop > oOrig.offset.top && !bIsSticky ){

is that: oOrig.offset is a function, not a variable. So oOrig.offset.top is not valid. Simply call the function and it will return a variable with the top property which you can access:

if( iScrollTop > oOrig.offset().top && !bIsSticky ){

Explination:

oOrig.offset is a reference to a function (the offset function in jquery).

You must call the function to access the .top property.

Bob Fincheimer
I'm afraid that still triggers the same error, the Console just gives the same message but with a couple of brackets after offset! :(EDIT: Chrome now says "Uncaught TypeError: Cannot call method 'offset' of undefined"
Jack Webb-Heller
It still says that oOrig is undefined.
Jack Webb-Heller
This answer is wrong. Why those upvotes? `offset` is not a function on `oOrig`. If you look closely, then `oOrig` is the object that is stored in `$o.data(sClass)` which is `{css:{position:$o.css('position'),top:$o.css('top')}, offset:$o.offset()}`. And even if it was a function, then it would throw another error and *not* that *`oOrig` is undefined*.
Felix Kling
@Bob: in the `if` statement you're talking about, `oOrig.offset` actually _is_ a variable. `oOrig` comes from `.data()`, so it should be a plain old object with 3 properties, `css`, `top`, and `offset`. Look at the line immediately below the `//set original data` comment. It looks to me like the problem is that the `$o.data(sClass, {/*...*/})` isn't happening correctly.
Matt Ball
Wow good catch Felix, I jumped the gun on this one....
Bob Fincheimer
@Bob Fincheimer: Can happen ;) I hope you are not offended by the first two sentences of my previous comment. I just wondered why people so quickly up-vote answers that sound reasonable without verifying them.
Felix Kling
Glad we've got that sorted, but, uh - can anyone help out any further? I'm really trying my best to understand all this. Y'know, we've all got to start somewhere...
Jack Webb-Heller
Updated the answer, maybe its right this time :-P
Bob Fincheimer
This could be indeed the problem, good idea! Actually by reading the question, it should be clear: *I've put this code in place in the head*. +1 good catch!
Felix Kling
Sometimes it would be better if `jQuery` did not fail silently....
Felix Kling
Hooray! Thanks Bob, that solved the problem. Cheers for your help :)
Jack Webb-Heller
lol @Felix, you are so right
Bob Fincheimer