Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:22:42+00:00 2026-05-16T04:22:42+00:00

First of all I did not write this code. I found it on somebody

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:22:42+00:00Added an answer on May 16, 2026 at 4:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.