这个 javascript 滑动菜单不起作用——调试器给出的唯一错误是第一个 IF 语句是一个块命令并且应该有花括号,我不明白。
有没有人看到明显的错误?我不确定我是否正确使用了 'this' 关键字(以及函数 showSlide 中的变量 slideList ......请帮助和对我的格式(空白)的任何反馈也很感激!
window.onload = makeMenus;
var currentSlide = null;
var timeID = null;
var leftPos = 0;
function makeMenus() {
var slideMenus = new Array();
var allElems = document.getElementsByTagName("*");
for (var i = 0; i < allElems.length; i++) {
if (allElems[i].className == "slideMenus") menus.push(allElems[i]);
}
for (var i = 0; i < slideMenus.length; i++) {
slideMenus[i].onclick = showSlide;
slideMenus[i].ul.style.left = "0px"; //for each object in slideMenus Array, ref 1st ul element within that object and set the value of the ul elements left style property to 0px
}
document.getElementById("head").onclick = closeSlide;
document.getElementById("main").onclick = closeSlide;
}
function showSlide() {
var slideList = this.id + ".ul[0]"; //stores object ref to the 1st ul element nested within the current object.
if (currentSlide !== null) {
closeSlide();
} else {
closeSlide();
currentSlide = slideList;
currentSlide.style.display = "block";
timeID = setInterval("moveSlide()", 1);
}
}
function closeSlide() {
if (currentSlide !== null) {
clearInterval(timeID);
currentSlide.style.left = "0px";
currentSlide.style.display = "none";
currentSlide = null;
}
}
function moveSlide() {
if ((leftPos + 5) <= 220) {
currentSlide.style.left = leftPos + "px"
} else {
clearInterval(timeID);
leftPos = 0;
}
}