js 在 onLoad 中淡入淡出,for 循环、样式和 setInterval 不起作用?

发布于 2024-12-13 12:57:48 字数 480 浏览 1 评论 0原文

为什么当调用 fadeIn() onLoad 时浏览器会立即执行循环。换句话说,setInterval 或 Opacityto() 存在问题。

function Opacityto(elem,v){
    elem.style.opacity = v/100;
    elem.style.MozOpacity =  v/100;
    elem.style.KhtmlOpacity =  v/100;
    elem.style.filter=" alpha(opacity ="+v+")";}

function fadeIn(){
elem=document.getElementById('nav');
for (i=0;i==100;i++){
    setInterval(Opacityto(elem,i),100);}
}

我想有人会告诉我这可以用 jQuery 最简单地完成,但我对用 javascript 感兴趣。

谢谢!救命!

Why when calling fadeIn() onLoad does the browser run through the loop immediately. In other words there is an issue with either the setInterval or the Opacityto().

function Opacityto(elem,v){
    elem.style.opacity = v/100;
    elem.style.MozOpacity =  v/100;
    elem.style.KhtmlOpacity =  v/100;
    elem.style.filter=" alpha(opacity ="+v+")";}

function fadeIn(){
elem=document.getElementById('nav');
for (i=0;i==100;i++){
    setInterval(Opacityto(elem,i),100);}
}

I think someone will tell me this can be done easiest with jQuery but I'm interested in doing it with javascript.

Thanks!HelP!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

笑,眼淚并存 2024-12-20 12:57:48

您的 fadeIn() 函数存在几个问题:

A. 您的 for 循环条件是 i==100,这在第一次迭代中不成立,因此for 循环体永远不会被执行(i++ 永远不会发生)。也许您的意思是 i<=100i<100 (取决于您希望循环运行 101 次还是 100 次)?

B. 您的 setInterval 代码存在语法错误编辑:,因为您已更新问题以删除引号 - setInterval 需要一个字符串或函数引用/表达式。因此,您需要向其传递一个不带括号和参数的函数名称,或者一个函数表达式,例如您可以在下面的代码中看到的匿名函数表达式。 以您尝试构建传递给它的字符串的方式。你已经有了这个:

"Opacityto("+elem,i+")"

但你需要这个:

"Opacityto(elem," + i + ")"

后者生成一个字符串,根据i,看起来像“Opacityto(elem,0)”< /code>,即,它生成一段有效的 JavaScript 代码,它将调用 Opacityto() 函数。

C. 您可能需要 setTimeout() 而不是setInterval(),因为setInterval() 将每 100 毫秒运行一次 Opacityto() 函数永远,而 setTimeout() 将运行 Opacityto() 在 100 毫秒延迟后恰好执行一次。鉴于您在循环中调用它,我确定您真的不想调用 setInterval() 100 次来导致您的函数 Opacityto() 运行每 100 毫秒 100 次永远

D. 即使解决了上述所有问题,您的基本结构也不会达到您想要的效果。当您调用 setInterval()setTimeout() 时,它不会暂停当前代码块的执行。因此,整个 for 循环将运行并立即创建所有间隔/超时,然后当 100 毫秒结束时,它们或多或少都会立即触发。如果您的目的是逐渐改变不透明度,每次更改每 100 毫秒发生一次,那么您需要以下代码(或其一些变体):

function fadeIn(i){
    // if called with no i parameter value initialise i
    if (typeof i === "undefined") {
       i = -1;
    }

    if (++i <= 100) {
       Opacityto(document.getElementById('nav'), i);
       setTimeout(function() { fadeIn(i); }, 100);
    }
}

// kick it off:
fadeIn();

上面的代码所做的是定义 fadeIn() ,然后调用它,传递 no范围。该函数检查 i 是否未定义,如果是,则将其设置为 -1(如果您在不传递参数的情况下调用它,就会发生这种情况)。然后它递增 i 并检查结果是否小于或等于 100,如果是,则调用 Opacityto() 传递对元素和 i 的引用代码>.然后它使用 setTimeout() 在 100 毫秒内调用自身,传递当前的i。因为 setTimeout() 位于 if 测试中,一旦 i 变得足够大,函数就会停止设置超时,整个过程就会结束。

还有其他几种方法可以实现此目的,但这只是我开始输入时发生的第一种方法......

You've got several problems with your fadeIn() function:

A. Your for loop condition is i==100, which is not true on the first iteration and thus the body of the for loop will never be executed (the i++ won't ever happen). Perhaps you meant i<=100 or i<100 (depending on whether you want the loop to run 101 or 100 times)?

B. Your setInterval code has a syntax error EDIT: since you've updated your question to remove the quotes - setInterval expects either a string or a function reference/expression. So you need to either pass it the name of a function without parentheses and parameters, or a function expression like the anonymous function expression you can see in my code down below. in the way you try to build the string you are passing it. You've got this:

"Opacityto("+elem,i+")"

but you need this:

"Opacityto(elem," + i + ")"

The latter produces a string that, depending on i, looks like "Opacityto(elem,0)", i.e., it produces a valid piece of JavaScript that will call the Opacityto() function.

C. You probably want setTimeout() rather than setInterval(), because setInterval() will run your Opacityto() function every 100ms forever, while setTimeout() will run Opacityto() exactly once after the 100ms delay. Given that you are calling it in a loop I'm sure you don't really want to call setInterval() 100 times to cause your function Opacityto() to be run 100 times every 100ms forever.

D. Even fixing all of the above, your basic structure will not do what you want. When you call setInterval() or setTimeout() it does not pause execution of the current block of code. So the entire for loop will run and create all of your intervals/timeouts at once, and then when the 100ms is up they'll all be triggered more or less at once. If your intention is to gradually change the opacity with each change happening every 100ms then you need the following code (or some variation thereon):

function fadeIn(i){
    // if called with no i parameter value initialise i
    if (typeof i === "undefined") {
       i = -1;
    }

    if (++i <= 100) {
       Opacityto(document.getElementById('nav'), i);
       setTimeout(function() { fadeIn(i); }, 100);
    }
}

// kick it off:
fadeIn();

What the above does is defines fadeIn() and then calls it passing no parameter. The function checks if i is undefined and if so sets it to -1 (which is what happens if you call it without passing a parameter). Then it increments i and checks if the result is less than or equal to 100 and if so calls Opacityto() passing a reference to the element and i. Then it uses setTimeout() to call itself in 100ms time, passing the current i through. Because the setTimeout() is inside the if test, once i gets big enough the function stops setting timeouts and the whole process ends.

There are several other ways you could implement this, but that's just the first that happened as I started typing...

薄荷→糖丶微凉 2024-12-20 12:57:48

我的猜测是 setInterval 中有一个令人讨厌的逗号,弄乱了参数列表:

"Opacityto("+elem,i+")"
                ^^^
                here

您可以尝试引用逗号,

+ "," + 

但 eval 是邪恶的,所以不要这样做。好的方法是传递一个真正的回调函数:

function make_opacity_setter(elem, i){
    return function(){
        OpacityTo(elem, i);
    };
}

...

setTimeout( make_opacity_setter(elem, i), 1000);

请注意,需要中间函数生成函数来避免闭包和 for 循环之间令人讨厌的交互。


顺便说一句,当您这样做时,

setInterval(func(), 1000)

您自己调用一次 func ,然后将其返回值传递给 setInterval 。由于 setInterval 收到的是垃圾值而不是回调,因此它不会按您想要的方式工作。

My guess is that there is a nasty comma inside the setInterval, messing the argument list:

"Opacityto("+elem,i+")"
                ^^^
                here

You could try quoting the comma

+ "," + 

but eval is evil so don't do that. The good way is to pass a real callback function:

function make_opacity_setter(elem, i){
    return function(){
        OpacityTo(elem, i);
    };
}

...

setTimeout( make_opacity_setter(elem, i), 1000);

Do note that the intermediate function-making-function is needed to avoid the nasty interaction between closures and for-loops.


BTW, when you do

setInterval(func(), 1000)

you call func once yourself and then pass its return value to setInterval. since setInterval receives a junk value instead of a callback it won't work as you want to.

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