Waypoint的循环,退出循环后属性未定义
Loop of Waypoint, attribut undefined after quit loop
需要一些路点的东西,我试着做一个循环,这可以帮助我正确地完成它
事实上,我的代码似乎很好,除了Waypoint在我滚动时不起作用之外。
btn选择&btn-transparent是css类,它帮助我向用户显示他在页面上的位置。
这是我的密码。
// array of id, my left menu
var tableMenu = new Array("#left-menu-top", "#left-menu-evo-details", "#left-menu-details-accordeon", "#left-menu-awesome", "#left-menu-prog-rerun", "#left-menu-features", "#left-menu-timeline", "#left-menu-upgrade");
// array of id, content section
var tableSection = new Array("top", "evo-details", "details-accordeon", "awesome", "prog-rerun", "features", "timeline", "upgrade");
// waypoint object array
var WaypointArray = {};
// array of offset
var OffsetWaypointArray = new Array("-50", "55", "55", "55", "55", "55", "55", "0");
function clearAllButtons() {
for(value in tableMenu) {
$(tableMenu[value]).removeClass("btn-selected");
$(tableMenu[value]).addClass("btn-tranparent");
}
}
function replaceClass(idLeftMenu) {
clearAllButtons();
$(idLeftMenu).removeClass("btn-tranparent");
$(idLeftMenu).addClass("btn-selected");
}
$(document).ready(function() {
console.log('activated');
for(var count=0; count < tableMenu.length; count++) {
WaypointArray[count] = new Waypoint({
element: document.getElementById(tableSection[count]),
handler: function(direction) {
clearAllButtons(),
replaceClass(tableMenu[count]),
console.log("you reached waypoint : "+tableSection[count]+" to : "+direction);
},
offset: OffsetWaypointArray[count]
});
console.log('counted '+count);
//OffsetWaypointArray = {WaypointArray[count]};
}
//for(var count = 0; count < tableMenu.length; count++) {
// console.log(WaypointArray[count]);
//}
});
经过一些滚动,控制台日志告诉我。
activated
scripts.js:32 you reached waypoint : top to : down
scripts.js:36 counted 0
scripts.js:32 you reached waypoint : evo-details to : down
scripts.js:36 counted 1
scripts.js:36 counted 2
scripts.js:36 counted 3
scripts.js:36 counted 4
scripts.js:36 counted 5
scripts.js:36 counted 6
scripts.js:36 counted 7
2scripts.js:32 you reached waypoint : undefined to : down
scripts.js:32 you reached waypoint : undefined to : up
scripts.js:32 you reached waypoint : undefined to : down
scripts.js:32 you reached waypoint : undefined to : up
2scripts.js:32 you reached waypoint : undefined to : down
scripts.js:32 you reached waypoint : undefined to : up
因此,退出循环^后,Waypoint似乎不再定义。(因为无论我在哪里,当我刷新页面时,Waypoint都会工作,它会显示我在哪个页面,但如果我滚动,那么我就会出错)
感谢您的帮助
这是JavaScript中的一个经典闭包问题。当异步handler
函数稍后运行时,用于循环的count
变量不会以其当前值捕获。换句话说,创建航路点时count
为4并不意味着执行时它的值为4。在您的示例中,当所有处理程序都执行并且数组在索引8处没有任何内容时,count
将为8。
MDN有一篇关于闭包的不错的文章,可以作为一个起点。
在@imakewebthings的启发下,我刚刚解决了类似的问题:
// create a function as the "environment", provide the needed parameter to it, return the waypoint object, this will ensure, when the action is completed, it will return the result one by one later in the loop.
function createWayPoint( eleID, index ){
return new Waypoint({
element: document.getElementById( eleID ),
handler: function(direction) {
hightlight( index );
}
});
};
window["wp"] = []; // putting it in global object for testing purpose
for( var i = 0; i < $("#floor-navigator li").length; i++ ){
createWayPoint( "floor" + (i + 1).toString(), i )
console.log(i);
// store it one by one and you can see, they are successfully created
window["wp"].push( createWayPoint( "floor" + (i + 1).toString(), i ) );
};
非常感谢,并希望这能帮助其他有类似困难的人。
相关文章:
- Javascript:循环属性内部的条件递增或递减
- 在循环中包含特定属性
- 借助for循环和数组在对象中添加属性
- 不可枚举的属性出现在 for..在 Chrome 中循环
- 我们可以设置属性'id'到不同的'按钮'通过使用javascript或jquery的循环
- 如何在循环中设置onclick属性
- 按数据属性循环元素并替换值
- 使用循环增加css属性值
- 循环浏览JavaScript对象并记录其属性
- ..的Javascript..循环中的对象没有在最后一个属性上运行
- 循环访问 JS 数组 + 数组属性
- 如何使用 jQuery 在循环中获取属性的值
- 为for循环中的对象添加新属性
- 在'用于'如果javascript中不存在对象属性,则循环
- Waypoint的循环,退出循环后属性未定义
- 如何循环对象中的javascript对象并查找属性
- 如何切换音频循环属性
- javascript对象的循环属性
- 帮助循环属性并从数组中引用它们
- 使用DOM方法循环属性