我的代码遇到了问题。它的工作在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;
});
答案 0 :(得分:1)
在点击功能的侧面或外侧定义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的更多信息。
答案 1 :(得分:1)
声明topPos
变量,因为它是高度赋值0(数字)。如果您打算在每个位置使用它,那么从声明中删除var
以使其具有全局范围。
$('.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;