fadeIn / fadeOut函数不起作用

时间:2013-10-09 12:12:46

标签: javascript

我正在使用纯javascript处理fadein和fadeout函数,这里是代码:

<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”

2 个答案:

答案 0 :(得分:0)

您缺少函数声明的括号:

setTimeout(function() {

});

请注意()关键字后添加function。您需要对setTimeoutfadeIn

中的fadeOut个函数执行此操作

答案 1 :(得分: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>