在javascript中展开/收回闪烁的错误

时间:2013-11-02 13:46:48

标签: javascript html javascript-events

我遇到了这个错误,我在javascript中有一个处理mousedonw事件操作的脚本。一个人做扩展,一个人使用javascript收回元素。这是通过timeloop完成并增加div的高度,直到高度适合内容或减少直到div被隐藏。当你点击提取,等到它提取,然后点击撤回所有工作正常。当您快速单击提取然后缩回(在提取结束之前)时会发生此问题。然后出现魔法虫。这些行动一直在提取和重新训练,而且永远不会结束。 我认为问题应该是循环变量(循环结束条件)。 有没有人看到问题在哪里以及如何解决?谢谢。
这里的代码:
如果你只是将代码复制到:something.html和extract_retract.js,你可以看到我正在处理的问题。

Html文档:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
p{ padding:10px 20px; background:#D9ECFF; }
div.mydivs {
    background: #97D6FB;
    width: 500px;
    height:0px;
    overflow:hidden;
}
div.mydivs > p{ padding:4px 16px; background: #97D6FB;}
</style>
<script type="text/javascript" src="expand_retract.js"></script>
</head>
<body>
<h3>Programming Raw JavaScript expand() and retract() Animation Functions</h3>
<p>
  <a href="#" onclick="return false" onmousedown="expand('div1')">Expand Box 1</a> | 
  <a href="#" onclick="return false" onmousedown="retract('div1')">Retract Box 1</a>
</p>
<div id="div1" class="mydivs">
  <p>Box 1 Content</p>
  <p>Box 1 Content</p>
  <p>Box 1 Content</p>
</div>
<p>
  <a href="#" onclick="return false" onmousedown="expand('div2')">Expand Box 2</a> | 
  <a href="#" onclick="return false" onmousedown="retract('div2')">Retract Box 2</a>
</p>
<div id="div2" class="mydivs">
  <p>Box 2 Content</p>
  <p>Box 2 Content</p>
  <p>Box 2 Content</p>
  <p>Box 2 Content</p>
  <p>Box 2 Content</p>
</div>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dokument bez názvu</title>
</head>

<body>
</body>
</html>

下面的expand_retract.js代码:

 function expand(element){
    var target = document.getElementById(element);
    var h = target.offsetHeight;
    var sh = target.scrollHeight;
    var loopTimer = setTimeout('expand(\''+element+'\')',8);
    if(h < sh){
        h += 5;
    } else {
        clearTimeout(loopTimer);
    }
    target.style.height = h+"px";
}
function retract(element){
    var target = document.getElementById(element);
    var h = target.offsetHeight;
    var loopTimer = setTimeout('retract(\''+element+'\')',8);
    if(h > 0){
        h -= 5;
    } else {
        target.style.height = "0px";
        clearTimeout(loopTimer);
    }
    target.style.height = h+"px";
}

1 个答案:

答案 0 :(得分:0)

很棒的代码。我建议这个来解决这个问题:

var loopTimer = 0;

function expand(element) {
  clearInterval(loopTimer);
  loopTimer = setInterval('expandA(\''+element+'\')',8);
}

function expandA(element){
    var target = document.getElementById(element);
    var h = target.offsetHeight;
    var sh = target.scrollHeight;
    if(h < sh){
        h += 5;
    } else {
        clearInterval(loopTimer);
    }
    target.style.height = h+"px";
}

function retract(element) {
  clearInterval(loopTimer);
  loopTimer = setInterval('retractA(\''+element+'\')',8);
}

function retractA(element){
    var target = document.getElementById(element);
    var h = target.offsetHeight;
    if(h > 0){
        h -= 5;
    } else {
        target.style.height = "0px";
        clearInterval(loopTimer);
    }
    target.style.height = h+"px";
}