简体   繁体   中英

Javascript Error Uncaught TypeError: Cannot read property 'top' of undefined

i was coding my page and i use a template. I don't understand javascript very well so im getting this error only when i go to portfolio.html page and switch to the mobile view. in the index.html is not showing any error. index page uses the nav links like: <li><a href="#portfolio-section" class="nav-link">Portfolio</a></li> and works right. in the portfolio page navlinks i wanted to go to other file like this: <li><a href="index.html" class="nav-link">Inicio</a></li> .

my code give this error:

Javascript Error Uncaught TypeError: Cannot read property 'top' of undefined

any tips to solve?

my html:

<html lang="pt-br">
<head>
  <title>Nova Pintura</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">enter code here

  <link href="https://fonts.googleapis.com/css?family=Work+Sans:400,700,900&display=swap" rel="stylesheet">

  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/jquery-ui.css">
  <link rel="stylesheet" href="css/owl.carousel.min.css">
  <link rel="stylesheet" href="css/owl.theme.default.min.css">
  <link href="https://fonts.googleapis.com/css?family=Dosis|Oswald&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="css/jquery.fancybox.min.css">
  <link rel="stylesheet" href="css/bootstrap-datepicker.css">
  <link rel="stylesheet" href="fonts/flaticon/font/flaticon.css">
  <link rel="stylesheet" href="fonts/icomoon/style.css">
  <link rel="stylesheet" href="css/aos.css">
  <link rel="stylesheet" href="css/style.css">

</head>

<body data-spy="scroll" data-target=".site-navbar-target" data-offset="300">

  <div class="site-wrap">

    <div class="site-mobile-menu site-navbar-target">
      <div class="site-mobile-menu-header">
        <div class="site-mobile-menu-close mt-3">
          <span class="icon-close2 js-menu-toggle"></span>
        </div>
      </div>
      <div class="site-mobile-menu-body"></div>
    </div>
  </div>

    <header class="site-navbar py-4 js-sticky-header site-navbar-target" role="banner">

      <div class="container">
        <div class="row align-items-center">
          <div class="col-6 col-xl-2">
            <h1 class="mb-0 site-logo">
              <a href="index.html">
                <img src="images/logo/np.png" width="200" height="55" alt=""></img>
              </a>
            </h1>

          </div>

          <div class="col-12 col-md-10 d-none d-xl-block">
            <nav class="site-navigation position-relative text-right" role="navigation">
              <ul class="site-menu main-menu js-clone-nav mr-auto d-none d-lg-block">
                <li><a href="index.html" class="nav-link">Inicio</a></li>
                <li><a href="index.html#about-section" class="nav-link">Sobre Nós</a></li>
                <li><a href="portfolio.html" class="nav-link active">Portfolio</a></li>
                <li><a href="index.html#services-section" class="nav-link">Serviços</a></li>
                <li><a href="index.html#contact-section" class="nav-link">Contato</a></li>
              </ul>
            </nav>
          </div>

          <div class="col-6 d-inline-block d-xl-none ml-md-0 py-3" style="position: relative; top: 3px;"><a href="#"
              class="site-menu-toggle js-menu-toggle float-right"><span class="icon-menu h3"></span></a></div>
        </div>
      </div>
    </div>

the js function that is giving error :

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

      var hash = this.hash;

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

    });
  };
  OnePageNavigation();

  var siteScroll = function() {



    $(window).scroll(function() {

        var st = $(this).scrollTop();

        if (st > 100) {
            $('.js-sticky-header').addClass('shrink');
        } else {
            $('.js-sticky-header').removeClass('shrink');
        }

    }) 

  };

Cannot read property 'propName' of undefined is very common error. This is pretty self explanatory. In other words an object doesn't contain a property because it doesn't exist. Quickly scanning your code 'scrollTop': $(hash).offset().top is the issue. Ensure that you are checking for the property before using it.

Below snippet will ensure that your object exist with the property

'scrollTop': $(hash) &&& $hash.offset() && $hash.offset().top ? $hash.offset().top : aDefaultValue; ///You should assign your own default value

Today I faced the same problem (and the code was exactly the same as yours) and I managed to solve the problem just by correcting one of the solution proposals above:

Replace

'scrollTop': $(hash).offset().top

Per

'scrollTop': $(hash) && $hash.offset() && $hash.offset().top && $hash.offset().top(0)

If you work with any language in which you need to change the parameters of href ="", don't forget to change this in the script.

FOR EXAMPLE, if I work with django I will have to use href = "{% url 'index'%}" then you would have to do the following:

Replace [href^='#'] Per [href^='{% url']

(Since my reference between pages will always have to start with "{% url)

I hope I helped although so much time has passed and you probably have already found a solution to it: D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM