一些多么简单的脚本。可以看到出了什么问题
这是包含脚本的页面的链接。 http://signsourceak.com/index1.html
这是我的脚本,由于某些原因所有函数都会触发无需鼠标悬停。谁能告诉我我的脚本有什么问题
window.onload = sliding;
var tags = new Array('tag1','tag2','tag3','tag4','tag5','tag6','tag7','tag8');// List of headings
var pics = new Array('popout1','popout2','popout3','popout4','popout5','popout6','popout7','popout8');// list of images that slide out
function sliding(){ // assing event
for(var i=0; i< tags.length; i++){
document.getElementById(tags[i]).onmouseover = slideout(tags[i],pics[i]); // <-- The Problem is Here Function runs with out the actual event
document.getElementById(tags[i]).onmouseout = slidein(tags[i],pics[i]);
//alert('this worked,'+ tags[i] + pics[i]);
}
}
function slideout(hid,picid){
document.images[picid].style.visibility = "visible";
document.images[picid].style.MozOpacity = 0.7;// need browser compatability
moveout(hid,picid);
}
function moveout(hid,picid){
if(currpos(picid) > 0){
document.images[picid].style.top = currpos(picid) - 1 + "px";
setTimeout(moveout,10);
}else{
clearTimeout(moveout);
}
function currpos(element){
return document.getElementById(element).offsetTop;
}
}
function slidein(hid,picid){
document.images[picid].style.MozOpacity = 0.5;// need browser compatability
movein(hid,picid);
}
function movein(hid,picid){
if(currpos(picid) < 210){
document.images[picid].style.top = currpos(picid) + 1 + "px";
setTimeout(movein,10);
}else{
clearTimeout(movein);
document.images[picid].style.visibility = "hidden";
}
function currpos(element){
return document.getElementById(element).offsetTop;
}
}
Here is the link to the page with the script.
http://signsourceak.com/index1.html
Here is my script and for some reasons all the functions fire with out mouse over. Can anyone tell me what is wrong with my script
window.onload = sliding;
var tags = new Array('tag1','tag2','tag3','tag4','tag5','tag6','tag7','tag8');// List of headings
var pics = new Array('popout1','popout2','popout3','popout4','popout5','popout6','popout7','popout8');// list of images that slide out
function sliding(){ // assing event
for(var i=0; i< tags.length; i++){
document.getElementById(tags[i]).onmouseover = slideout(tags[i],pics[i]); // <-- The Problem is Here Function runs with out the actual event
document.getElementById(tags[i]).onmouseout = slidein(tags[i],pics[i]);
//alert('this worked,'+ tags[i] + pics[i]);
}
}
function slideout(hid,picid){
document.images[picid].style.visibility = "visible";
document.images[picid].style.MozOpacity = 0.7;// need browser compatability
moveout(hid,picid);
}
function moveout(hid,picid){
if(currpos(picid) > 0){
document.images[picid].style.top = currpos(picid) - 1 + "px";
setTimeout(moveout,10);
}else{
clearTimeout(moveout);
}
function currpos(element){
return document.getElementById(element).offsetTop;
}
}
function slidein(hid,picid){
document.images[picid].style.MozOpacity = 0.5;// need browser compatability
movein(hid,picid);
}
function movein(hid,picid){
if(currpos(picid) < 210){
document.images[picid].style.top = currpos(picid) + 1 + "px";
setTimeout(movein,10);
}else{
clearTimeout(movein);
document.images[picid].style.visibility = "hidden";
}
function currpos(element){
return document.getElementById(element).offsetTop;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是如何使用clearTimeOut。
setTimeout 返回一个必须传递给clearTimeOut 的计时器ID:
that is not how to use clearTimeOut.
setTimeout returns a timer id that have to be passed to clearTimeOut:
您将slideout() 和slidein() 的结果分配为处理程序。您还需要隔离闭包变量;自调用函数将确保 i 循环变量不被所有事件处理程序共享
You are assigning the result of slideout() and slidein() as the handlers. You also need to isolate the closure variables; self calling functions will make sure the the i looping variable is not shared by all the event handlers
这里有一个问题:
在该循环中,您调用“slideout”和“slidein”函数,尽管很明显这不是您想要做的。您想要是分配一个以适当方式调用“slideout”或“slidein”的函数。为此,您需要另一层功能:
正如 @BiAiB 所指出的,您对“setTimeout”和“clearTimeout”的调用也需要一些注意。
One problem is here:
In that loop, you're calling the "slideout" and "slidein" functions, though it's obvious that that's not what you want to do. What you want is to assign a function that calls "slideout" or "slidein" the appropriate way. To do that, you'll need another layer of function:
As @BiAiB notes, your calls to "setTimeout" and "clearTimeout" need some attention too.