Back to Top jQuery - Uncaught Typeerror: Cannot read Property 'top' of Undefined

Asked

Viewed 1,404 times

0

I have the script to get back to TOPO. On the home page of the site, it works perfectly, however, on the internal pages it does not go up and presents the following error:

Uncaught Typeerror: Cannot read Property 'top' of Undefined

jQuery

$(document).ready(function () {
  'use strict';
  /* ========================================================================
     Fullscreen burger menu
   ========================================================================== */
  $(".menu-trigger, .mobilenav").click(function () {
    $(".mobilenav").fadeToggle(500);
  });
  $(".menu-trigger, .mobilenav").click(function () {
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
  });

  /* ========================================================================
     On click menu item animate to the section
   ========================================================================== */
  $(".mobilenav li, .back-to-top").on('click', function() {
    var target = $(this).data('rel');
    var $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top
    }, 900, 'swing');
  });

Error forwards the following line:

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

Where I’d be wrong?

  • If you do console.log($(target)); what it prints?

  • main.js:26 Uncaught Typeerror: Cannot read Property 'top' of Undefined(Anonymous Function) @main.js:26jQuery.event.Dispatch @jquery-1.11.1.js:4641elemData.Handle @jquery-1.11.1.js:4309

  • Appears like this....

  • It is because this variable has nothing. Put the relevant part of your HTML

  • Hmmmm, you may not be finding your destination so I’ll try here!

  • I asked a question here, see if it can help you: http://answall.com/questions/30703/volta-homeda-p%C3%A1gina

  • It was the destination that was incorrect, so I put a <top id="top"></top> and it worked on all pages :) But obg anyway.

  • @Andrébaill, don’t invent element tags without proper implementation... if the goal is just to mark an anchor on your document, use the appropriate tag for this: <a id="topo" href="#topo"></a>

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.