Uncaught TypeError:无法读取未定义的属性“ top”

时间:2020-03-10 07:11:44

标签: javascript html dom typeerror mobile-website

您好,我的新网站出现此错误,我无法使用移动菜单导航,我们将不胜感激...欢呼..

var OnePageNavigation = function() {
    var navToggler = $('.site-menu-toggle');
    $("body").on("click", ".main-menu li a[href^='#'], .smoothscroll[href^='#'], .site-mobile-menu .site-nav-wrap li a", function(e) {
      e.preventDefault();

      var hash = this.hash;

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

    });
  };
  OnePageNavigation();

1 个答案:

答案 0 :(得分:0)

解决了。...

top为空,将返回NaN,top必须为0

var OnePageNavigation = function() {
var navToggler = $('.site-menu-toggle');
$("body").on("click", ".main-menu li a[href^='#'], .smoothscroll[href^='#'], .site-mobile-menu .site-nav-wrap li a", function(e) {
  if (this.hash !== "" && this.pathname == window.location.pathname) {
e.preventDefault();
var target = this.hash;
var topOffset = 0; //#top should default to 0 so no need to calculate the difference between top and top :)
if (target != "#top") { //If the target is not "#top", then calculate topOffset
var topOffset = $(target).offset().top;
}
  $('html, body').animate({
    'scrollTop': $(target).offset().top
  }, 600, 'easeInOutCirc', function(){
    window.location.hash = target;
  });
  }
});
};
OnePageNavigation();