Дтдця
Дтдця

Reputation: 354

Uncaught ReferenceError: topPos is not defined

I am facing a issue with my code. its work fine in html. when i convert to Wordpress i receive a error when i press button to show bottom hidden info.

and this error shwoing after click the button Uncaught ReferenceError: topPos is not defined

here is my code

 $(".info-data").hide();
    var activeInfoData = $(".info-nav .active a").attr("href");
    $(activeVehicleData).show();

$('.info-nav-scroll').click(function () {
    var direction = $(this).data('direction');
    var scrollHeight = $('.info-nav li').height() + 1;
    var navHeight = $('#info-nav-container').height() + 1;
    var actTopPos = $(".info-nav").position().top;

    // Scroll Down
    if (direction == 'down' && -navHeight <= actTopPos - (scrollHeight * 2)) {
        topPos = actTopPos - scrollHeight;
        $(".info-nav").css('top', topPos);
    }

    // Scroll Up
    if (direction == 'up' && 0 > actTopPos) {
        topPos = actTopPos + scrollHeight;
        $(".info-nav").css('top', topPos);
    }
    return false;
});

Upvotes: 1

Views: 943

Answers (2)

Nishit Maheta
Nishit Maheta

Reputation: 6031

define topPos variable in side or out side of click function like below

var topPos = '';  // or  var topPos = 0;
var direction = $(this).data('direction');
var scrollHeight = $('.info-nav li').height() + 1;
var navHeight = $('#info-nav-container').height() + 1;
var actTopPos = $(".info-nav").position().top;

You can read more about scope of variable in Javascript.

Upvotes: 1

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2817

in the click event itself declare the topPos variable and since it is height assign 0 to it (a number). If you intend to use it every where then remove var from the declaration to give it a global scope.

   $('.info-nav-scroll').click(function () {
    var topPos = 0; // declare as topPos = 0; if you intend to use it as a global variable
    var direction = $(this).data('direction');
    var scrollHeight = $('.info-nav li').height() + 1;
    var navHeight = $('#info-nav-container').height() + 1;
    var actTopPos = $(".info-nav").position().top;

Upvotes: 1

Related Questions