0

this is my navbar

  <ul id="jump" class="dropdown-menu" role="menu">
      <li><a href="/main_page#top_something">People</a></li>
      <li><a href="/main_page">Main Page</a></li>
  </ul>

and I am using this function

(function($){

var jump=function(e)
{
   if (e){
       e.preventDefault();
       var target = $(this).attr("href");
   }else{
       var target = location.hash;
   }

   $('html,body').animate(
   {
       scrollTop: $(target).offset().top
   },1000,function()
   {
       location.hash = target;
   });

}

$('html, body').hide()

$(document).ready(function()
{
    $('#jump a[href]').bind("click", jump);

    if (location.hash){
        setTimeout(function(){
            $('html, body').scrollTop(0).show()
            jump()
        }, 0);
    }else{
      $('html, body').show()
    }
});

})(jQuery)

main_page is a ejs file , and on /main_page route, node.js will render main_page.ejs file.

I am trying to click on navbar on A page and go to B page on div section. This give me : jump.js:18 Uncaught TypeError: Cannot read property 'top' of undefined error

Here is the same topic , jQuery scroll to ID from different page but I can't get it work ...thanks

Difference is that I have node.js and *.ejs files

Community
  • 1
  • 1
katrin
  • 26
  • 5
  • If you're using nodejs I suppose you're not routing to static `.html` files, how are your routes defined? – Lucas Lazaro Oct 05 '16 at 22:33
  • app.get('/main_page', function (req, res, next) { res.render('main_page', {title:'Something', headerTitle:'sometnig1', userid: req.session.userid}); }); and main_page is a .ejs file – katrin Oct 05 '16 at 22:53

1 Answers1

0
(function (){
  if(location.hash === '#top_something'){
      window.setTimeout(
        function(){ 
            $('html, body').animate({
            scrollTop: $('#top_something').offset().top
            }, 'slow');
        },2000);
  }
})();

This is what worked for me in this case :)

katrin
  • 26
  • 5