1

我正在使用纯 javascript 处理淡入和淡出功能,代码如下:

<script>
function fadeIn(el, ms) {
    var opacity = 1,
    interval = 50,
    gap = interval / ms;

    window.setInterval(function{ 
        opacity -= gap;
        el.style.opacity = opacity;
        if(opacity <= 0) {
            window.clearInterval(fading); 
            el.style.display = 'none';
        }
    }, interval);
}
function fadeOut(el, ms){
    var opacity = 0,
    interval = 50,
    gap = interval / ms;
    el.style.display = 'block';
    el.style.opacity = opacity;
    window.setInterval(function{ 
        opacity += gap;
        el.style.opacity = opacity;
        if(opacity >= 1) {
            window.clearInterval(fading);
        }
    }, interval);   
}
</script>

在正文部分,代码是:

<input type="button" onclick="fadeOut(this, 50);" value="hi" />

页面加载时立即给出错误“错误意外令牌(”:

window.setInterval(function{ 

当我尝试单击输入按钮时,出现错误:“找不到变量fadeIn”

4

2 回答 2

0

您缺少函数声明的括号:

setTimeout(function() {

});

注意关键字()后面的添加。function您需要为和中的两个setTimeout功能执行此操作fadeInfadeOut

于 2013-10-09T12:14:19.827 回答
0

这是你需要在写在“匿名函数”之后添加()的正确代码window.setInterval(function{

<script>
function fadeIn(el, ms) {
    var opacity = 1,
    interval = 50,
    gap = interval / ms;

    window.setInterval(function(){ 
        opacity -= gap;
        el.style.opacity = opacity;
        if(opacity <= 0) {
            window.clearInterval(fading); 
            el.style.display = 'none';
        }
    }, interval);
}
function fadeOut(el, ms){
    var opacity = 0,
    interval = 50,
    gap = interval / ms;
    el.style.display = 'block';
    el.style.opacity = opacity;
    window.setInterval(function(){ 
        opacity += gap;
        el.style.opacity = opacity;
        if(opacity >= 1) {
            window.clearInterval(fading);
        }
    }, interval);   
}
</script>
于 2013-10-09T12:21:57.503 回答