JavaScript 递归问题
我正在尝试编写一个可以在图像数组之间变化的函数。我所拥有的对我来说是有意义的,但是当我按如下方式运行它时,第一个图像会加载,然后第二个图像会在某个时刻加载(透明度不会改变),然后什么也没有发生。下面是 javascript 代码:
var image1 = new Image();var image2 = new Image(); var image3 = new Image();
image1.src = "images/website6.jpg"; image2.src = "images/website7.jpg"; image3.src = "images/sunset.jpg";
var images = new Array("images/website6.jpg","images/website7.jpg","images/sunset.jpg");
/*document.slide.style.opacity = 1;
document.slide.stylebg.opacity = 0;*/
setTimeout(function() { fade(images,0); }, 2000);
function delay(arr,num)
{
document.slide.src = arr[num % 3];
document.slide.style.opacity = 1;
document.slidebg.style.opacity = 0;
document.slidebg.src = arr[(num+1)%3];
var number = num + 1;
setTimeout(function() { fade(arr,number); }, 2000);
}
function fade(arr,num)
{
/*alert('fadebefore ' + (document.slide.style.opacity).toString())*/
document.slide.style.opacity -= 0.1
/*alert('fade')*/
document.slide.stylebg.opacity += 0.1
if (document.slide.style.opacity == 0)
{
setTimeout(function() { delay(arr,num); }, 150);
}
else
{
setTimeout(function() { fade(arr,num); }, 1500);
}
}
HTML 很简单。我有两节课; style
和 stylebg
。 style
位于 stylebg
前面,然后我根据需要更改不透明度和图像。 javascript 对我来说似乎合乎逻辑,但它没有按预期工作。另外值得注意的是,有 3 条评论。第一条注释(第 3-4 行)尝试将不透明度设置为开始时应有的值。但是,如果我这样做,那么我获得的进展甚至比上面更少:第一个图像加载并且没有其他事情发生。后两个注释用于调试目的。如果我取消注释这些,那么图像变化会发生在两个警报之间,这似乎表明图像变化是由不透明度变化引起的。
那么有人可以解释为什么它没有按照我的预期进行吗?谢谢。
编辑:更多代码:
HTML(这是唯一受影响的部分):
<div style="position: relative;">
<img src="images/sunset.jpg" id="slide" />
<img src="images/website6.jpg" id="slidebg" />
</div>
CSS:
#slide{ display:block; margin-left:5; margin-right:auto; border: 1px solid black; z-index: 1; top: 0px; position: relative; } #slidebg{ display:block; margin-left:auto; margin-right:auto; border: 1px solid black; z-index: 0; top: 0px; position: absolute; }
I am trying to write a function that will change between an array of images. What I have makes sense to me, but when I run it as it is below then the first image loads, then the second image loads at some point (the transparency doesn't change) and then nothing happens. Here is the javascript code:
var image1 = new Image();var image2 = new Image(); var image3 = new Image();
image1.src = "images/website6.jpg"; image2.src = "images/website7.jpg"; image3.src = "images/sunset.jpg";
var images = new Array("images/website6.jpg","images/website7.jpg","images/sunset.jpg");
/*document.slide.style.opacity = 1;
document.slide.stylebg.opacity = 0;*/
setTimeout(function() { fade(images,0); }, 2000);
function delay(arr,num)
{
document.slide.src = arr[num % 3];
document.slide.style.opacity = 1;
document.slidebg.style.opacity = 0;
document.slidebg.src = arr[(num+1)%3];
var number = num + 1;
setTimeout(function() { fade(arr,number); }, 2000);
}
function fade(arr,num)
{
/*alert('fadebefore ' + (document.slide.style.opacity).toString())*/
document.slide.style.opacity -= 0.1
/*alert('fade')*/
document.slide.stylebg.opacity += 0.1
if (document.slide.style.opacity == 0)
{
setTimeout(function() { delay(arr,num); }, 150);
}
else
{
setTimeout(function() { fade(arr,num); }, 1500);
}
}
The HTML is simple. I have two classes; style
and stylebg
. style
sits in front of stylebg
and then I change opacities and images as needed. The javascript seems logical to me, but it doesn't work as expected. Also worth noting is there are 3 comments. The first comment (line 3-4) is attemping to set the opacities to what they should be at the beginning. However, if I do this then I get even less progress than above: the first image loads and nothing else happens. The second two comments are used for debugging purposes. If I uncomment these then the image change occurs between the two alerts, which would seem to say the image change is caused by the opacity change.
So can anybody explain why it isn't doing what I expect it to? Thanks.
EDIT: Some more code:
HTML (This is the only part being affected):
<div style="position: relative;">
<img src="images/sunset.jpg" id="slide" />
<img src="images/website6.jpg" id="slidebg" />
</div>
CSS:
#slide{ display:block; margin-left:5; margin-right:auto; border: 1px solid black; z-index: 1; top: 0px; position: relative; } #slidebg{ display:block; margin-left:auto; margin-right:auto; border: 1px solid black; z-index: 0; top: 0px; position: absolute; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了格式化而回答。我还没有调试看看什么会起作用,但这对我来说更有意义
如果你想使用 ID,请使用 document.getElementById,如果你想使用名称,请使用 document.images[imgname] 或 document.imagename
你也在混合名字和风格。 document.slide.stylebg.opacity 等
这会起作用。不确定这是否是您想要的:http://jsfiddle.net/EcShW/2/
Answering for formatting's sake. I have not debugged to see what would work, but this makes more sense to me
If you want to use ID, use document.getElementById, if you want to use name, use document.images[imgname] or document.imagename
You are mixing names and style too. document.slide.stylebg.opacity and such
This does something. Not sure it is what you want: http://jsfiddle.net/EcShW/2/
您不能仅通过“id”值引用页面元素;您必须使用“document.getElementById()”:
然后您将使用“slideElement.style”等而不是“document.slide.style”。
You can't just refer to page elements by "id" value like that; you have to use "document.getElementById()":
Then you'd work with "slideElement.style" etc. instead of "document.slide.style".
EDIT2
它对我有用(铬)
啊哈!
我们可以通过
document.img_id
获取,即使在 chromium 中也可以 好的,好的,删除了此链接 我们教你在 WEB DEV 网站上失败..
EDIT2
It works for me (chromium)
AHA!
<img id="img_id"/>
we can get bydocument.img_id
, even in chromiumOK, OK, removed links to this WE TEACH YOU TO FAIL AT WEB DEV site..