开发者

Some what simple script. Can see what's wrong

开发者 https://www.devze.com 2023-02-09 05:45 出处:网络
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 w

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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消