• Register
0 votes
75 views

Problem :

I have written two different types of sticky menus for two different pages. Following is the error

Inside the anonymous function, use jquery to select the footer div. it has an id of 'footer'.

Following is the code for both Menus.

$(document).ready(function () {

    var contentNav = $('.content-nav').offset().top;

    var stickyNav = function () {

        var scrollTop = $(window).scrollTop();

        if (scrollTop > contentNav) {

            $('.content-nav').addClass('content-nav-sticky');

        } else {;

            $('.content-nav').removeClass('content-nav-sticky')

        }

    };

    stickyNav();

    $(window).scroll(function () {

        stickyNav();

    });

});

$(document).ready(function () {

    var stickyNavTop = $('.nav-map').offset().top;

    // var contentNav = $('.content-nav').offset().top;

    var stickyNav = function () {

        var scrollTop = $(window).scrollTop();

        if (scrollTop > stickyNavTop) {

            $('.nav-map').addClass('sticky');

            // $('.content-nav').addClass('sticky');

        } else {

            $('.nav-map').removeClass('sticky');

            // $('.content-nav').removeClass('sticky')

        }

    };

    stickyNav();

    $(window).scroll(function () {

        stickyNav();

    });

});

My issue is that the code for the sticky side menu on the bottom doesn't work because the second line of code var contentNav = $('.content-nav').offset().top; fires a error .

6.9k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I guess your document does not contain any element with class content-nav, So your method .offset() returns undefined which infact has no top property.

You can see for yourself in below code.

alert($('.content-nav').offset());

(you will get "undefined")

To avoid failing the whole code, you can have following code instead:

var top = ($('.content-nav').offset() || { "top": NaN }).top;

if (isNaN(top)) {

    alert("something is not working, no top");

} else {

    alert(top);

}

38.6k points

Related questions

0 votes
1 answer 11 views
11 views
Problem: I have no idea to solve this, somebody help : Inside the anonymous function, use jquery to select the footer div. it has an id of 'footer'.
asked Jun 11 asha 121k points
0 votes
1 answer 13 views
13 views
Problem: Where will I able to find an answer for this : Use jquery's html() method to insert the server response into the footer of the page.
asked Jun 13 nila 121k points
0 votes
1 answer 14 views
14 views
Problem: Is there a way to fix this : Uncaught typeerror cannot read property style of undefined?
asked Jun 23 Florina Gulnar 106k points
0 votes
1 answer 2 views
2 views
Problem: Match the words in bold to the correct term?
asked Jul 11 Nahil 128k points
0 votes
1 answer 24 views
24 views
Problem: I am trying to find out an answer but need someone’s help .. Remove classes from an element with jquery
asked May 21 Ifra 43.4k points
0 votes
1 answer 33 views
33 views
Problem: How to automatically include your header, navigation, and footer on every page.
asked Apr 28 Ryan evans 1.6k points
0 votes
1 answer 13 views
13 views
Problem: Select all p elements inside a div element?
asked Jul 13 Nahil 128k points
0 votes
1 answer 378 views
378 views
I want to select all p elements inside a div element.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
Problem: Change content of div when clicking on link/button
asked Jun 29 Shifa 18.2k points
0 votes
1 answer 34 views
34 views
Problem: I need your help on this one : Dynamically change height of div based on content.
asked May 23 Irenka Kiri 85.5k points