我的代码遇到问题。它在html中工作正常。当我转换为Wordpress时,按下按钮显示底部隐藏信息时会收到错误消息。

单击此按钮后,该错误消失
Uncaught ReferenceError: topPos is not defined

这是我的代码

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

最佳答案

在点击功能的侧面或侧面定义topPos变量,如下所示

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;


您可以阅读有关scope of variable in Javascript的更多信息。

08-19 07:16