Uncaught TypeError: Cannot read property 'top' of undefined
Check if the jQuery object contains any element before you try to get its offset:
var nav = $('.content-nav');
if (nav.length) {
var contentNav = nav.offset().top;
...continue to set up the menu
}
Your document does not contain any element with class content-nav
, thus the method .offset()
returns undefined which indeed has no top
property.
You can see for yourself in this fiddle
alert($('.content-nav').offset());
(you will see "undefined")
To avoid crashing the whole code, you can have such code instead:
var top = ($('.content-nav').offset() || { "top": NaN }).top;
if (isNaN(top)) {
alert("something is wrong, no top");
} else {
alert(top);
}
Updated fiddle.