Uncaught TypeError: Невозможно прочитать свойство 'top' из неопределенного - PullRequest
0 голосов
/ 10 марта 2020

Привет, я получил эту ошибку на моем новом веб-сайте, я не мог использовать мобильную навигацию по меню, любая помощь будет принята с благодарностью ... ура ..

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 голосов
/ 10 марта 2020

Решено ....

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();
...