一些多么简单的脚本。可以看到出了什么问题

发布于 2024-10-16 01:02:16 字数 1832 浏览 2 评论 0原文

这是包含脚本的页面的链接。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-10-23 01:02:16

这不是如何使用clearTimeOut。

setTimeout 返回一个必须传递给clearTimeOut 的计时器ID:

var timer = setTimeout( fn, 10 );
clearTimeout( timer);

that is not how to use clearTimeOut.

setTimeout returns a timer id that have to be passed to clearTimeOut:

var timer = setTimeout( fn, 10 );
clearTimeout( timer);
这个俗人 2024-10-23 01:02:16

您将slideout() 和slidein() 的结果分配为处理程序。您还需要隔离闭包变量;自调用函数将确保 i 循环变量不被所有事件处理程序共享

function sliding(){ // assing event 
    for(var i=0; i< tags.length; i++){

        document.getElementById(tags[i]).onmouseover =(function(index){
            return function() {
               slideout(tags[index],pics[index]);
            }
        })(i); 

        document.getElementById(tags[i]).onmouseout = (function(index){
            return function() {
                slidein(tags[index],pics[index]);
            }
        })(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

function sliding(){ // assing event 
    for(var i=0; i< tags.length; i++){

        document.getElementById(tags[i]).onmouseover =(function(index){
            return function() {
               slideout(tags[index],pics[index]);
            }
        })(i); 

        document.getElementById(tags[i]).onmouseout = (function(index){
            return function() {
                slidein(tags[index],pics[index]);
            }
        })(i);
    }
}
西瓜 2024-10-23 01:02:16

这里有一个问题:

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]);
    }
}

在该循环中,您调用“slideout”和“slidein”函数,尽管很明显这不是您想要做的。您想要是分配一个以适当方式调用“slideout”或“slidein”的函数。为此,您需要另一层功能:

function makeHandlers(index) {
  return {
    'out': function() { slideout(tags[index], pics[index]; },
    'in': function() { slidein(tags[index], pics[index]; }
  };
}

function sliding() {
  for (var i = 0; i < tags.length; ++i) {
    var handlers = makeHandlers(i), tag = document.getElementById(tags[i]);
    tag.onmouseover = handlers.in;
    tag.onmouseout = handlers.out;
  }
}

正如 @BiAiB 所指出的,您对“setTimeout”和“clearTimeout”的调用也需要一些注意。

One problem is here:

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]);
    }
}

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:

function makeHandlers(index) {
  return {
    'out': function() { slideout(tags[index], pics[index]; },
    'in': function() { slidein(tags[index], pics[index]; }
  };
}

function sliding() {
  for (var i = 0; i < tags.length; ++i) {
    var handlers = makeHandlers(i), tag = document.getElementById(tags[i]);
    tag.onmouseover = handlers.in;
    tag.onmouseout = handlers.out;
  }
}

As @BiAiB notes, your calls to "setTimeout" and "clearTimeout" need some attention too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文