js 在 onLoad 中淡入淡出,for 循环、样式和 setInterval 不起作用?
为什么当调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
fadeIn()
函数存在几个问题:A. 您的 for 循环条件是
i==100
,这在第一次迭代中不成立,因此for 循环体永远不会被执行(i++
永远不会发生)。也许您的意思是i<=100
或i<100
(取决于您希望循环运行 101 次还是 100 次)?B. 您的
setInterval
代码存在语法错误编辑:,因为您已更新问题以删除引号 -setInterval
需要一个字符串或函数引用/表达式。因此,您需要向其传递一个不带括号和参数的函数名称,或者一个函数表达式,例如您可以在下面的代码中看到的匿名函数表达式。以您尝试构建传递给它的字符串的方式。你已经有了这个:,但你需要这个:
后者生成一个字符串,根据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 毫秒发生一次,那么您需要以下代码(或其一些变体):上面的代码所做的是定义
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 (thei++
won't ever happen). Perhaps you meanti<=100
ori<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:but you need this:
The latter produces a string that, depending oni
, looks like"Opacityto(elem,0)"
, i.e., it produces a valid piece of JavaScript that will call theOpacityto()
function.C. You probably want
setTimeout()
rather thansetInterval()
, becausesetInterval()
will run yourOpacityto()
function every 100ms forever, whilesetTimeout()
will runOpacityto()
exactly once after the 100ms delay. Given that you are calling it in a loop I'm sure you don't really want to callsetInterval()
100 times to cause your functionOpacityto()
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()
orsetTimeout()
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):What the above does is defines
fadeIn()
and then calls it passing no parameter. The function checks ifi
is undefined and if so sets it to -1 (which is what happens if you call it without passing a parameter). Then it incrementsi
and checks if the result is less than or equal to 100 and if so callsOpacityto()
passing a reference to the element andi
. Then it usessetTimeout()
to call itself in 100ms time, passing the currenti
through. Because thesetTimeout()
is inside the if test, oncei
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...
我的猜测是 setInterval 中有一个令人讨厌的逗号,弄乱了参数列表:
您可以尝试引用逗号,
但 eval 是邪恶的,所以不要这样做。好的方法是传递一个真正的回调函数:
请注意,需要中间函数生成函数来避免闭包和 for 循环之间令人讨厌的交互。
顺便说一句,当您这样做时,
您自己调用一次 func ,然后将其返回值传递给 setInterval 。由于 setInterval 收到的是垃圾值而不是回调,因此它不会按您想要的方式工作。
My guess is that there is a nasty comma inside the setInterval, messing the argument list:
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:
Do note that the intermediate function-making-function is needed to avoid the nasty interaction between closures and for-loops.
BTW, when you do
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.