0

I m trying to get offset().top of an element <div class='profile'></div> present in a ngView controlled by app.js but the console displays an error:

Uncaught TypeError: Cannot read property 'top' of undefined

jQuery code :

var topOfOthDiv = $('.profile').offset().top;

index.ejs

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="main/js/main.js"></script>
    ...
</head>
    <body>
       <div class="ui inverted top fixed nbar hidden menu">...</div>
       <div class="ui main container " ng-view>
       </div>
    </body>

main.js

$(document)
    .ready(function() {

        var topOfOthDiv = $('.profile').offset().top;
        $(window).scroll(function() {
                if($(window).scrollTop() > topOfOthDiv) { //scrolled past the other div?
                    $('.fixed.menu').transition('fade in'); //reached the desired point -- show div
                }
                if($(window).scrollTop() < topOfOthDiv) { //scrolled past the other div?
                    $('.fixed.menu').transition('fade out'); //reached the desired point -- show div
                }
            });
    })
  ;

In ng-view i load my different view which has .profile clss div The code works fine if .profile class is in index.ejs

4
  • have you loaded jquery correctly? Commented Dec 22, 2015 at 15:36
  • Can you provide sample plunkr? Commented Dec 22, 2015 at 15:57
  • yes i loaded it correctly. Commented Dec 22, 2015 at 16:03
  • jsfiddle.net/joshdmiller/NEuJ6 not working :| Commented Dec 22, 2015 at 16:16

1 Answer 1

1

Well, without looking at the entire code we can't really tell why, because this could be a number of reasons, however the most likely reason is:

  1. You're loading the Javascript file after you declare your HTML. This means the script is looking for something that is written to the browser after execution.
  2. You've not loaded jQuery properly.
Sign up to request clarification or add additional context in comments.

1 Comment

I am loading main.js and jquery in head and the code works fine if i use .profile class in index.ejs but not in ng-views. I added few more codes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.