Ajax 在 XML 中加载图像 - 加载图像并淡入淡出?
我正在通过 ajax 加载一个包含多张照片的 XML 文档。 我的函数经历的过程是这样的:
打开 UL
开设 LI
打开 img 标签
将 src 设置为 = xml 文档中的 url(见下文)
关闭 img 标签
关闭李
开设 LI
打开 img 标签
将 src 设置为 = xml 文档中的 url(见下文)
关闭 img 标签
关闭李
close UL
它创建一个新的列表项,并将 img 标签放入 xml 文档中的每个单独标签中。
xml 是这样的,但有更多条目:
<images><image><url>0.jpg</url></image><image><url>1.jpg</url></image></images>
处理图像 src 的函数的片段是
for (i=0;i<x.length;i++)
{
txt=txt + "<li>";
xx=x[i].getElementsByTagName("url");
{
try
{txt=txt + "<img class='fade' src='" + xx[0].firstChild.nodeValue + "' />";
}
catch (er)
{txt=txt + "<li class='fade'><img src='images/ajax-loader.gif' /></li>";}
}
txt=txt + "</li>";
}
我的问题是,一旦加载它们,如何使它们淡入?我应该如何添加loading.gif 的后面?我正在考虑一个类似的函数,它使用loading.gif作为src编写图像标签,然后在上面的函数中通过id获取每个元素并更改src。尝试一下,欢迎任何其他解决方案。但主要是我需要弄清楚如何让这个坏男孩消失。
谢谢
I am loading in an XML document with multiple photos in it, via ajax.
The process my function goes through is this:
Open a UL
Open a LI
Open an img tag
set the src to = the url in the xml document (see below)
close img tag
close LI
open a LI
open an img tag
set the src to = the url in the xml document (see below)
close img tag
close LI
close UL
It creates a new List item and puts the img tag inside for each individual tag in the xml document.
And the xml is this, but with more entries:
<images><image><url>0.jpg</url></image><image><url>1.jpg</url></image></images>
and a snippit of my function that deals with the image src is
for (i=0;i<x.length;i++)
{
txt=txt + "<li>";
xx=x[i].getElementsByTagName("url");
{
try
{txt=txt + "<img class='fade' src='" + xx[0].firstChild.nodeValue + "' />";
}
catch (er)
{txt=txt + "<li class='fade'><img src='images/ajax-loader.gif' /></li>";}
}
txt=txt + "</li>";
}
My question is, how do I make these fade in once they have been loaded? and how should i go about adding the loading.gif's behind? I was thinking of a simliar function that writes image tags with loading.gif as the src, then in the function above getting each element by id and changing the src. gonna give this a go, any other soltions welcome. But mainly i need to work out howo to fade this bad boy.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加图像:
然后当你的函数完成添加图像时调用 jquery fade:
Add the images with:
And then when your function finishes adding the images call jquery fade:
正如 aviv 所说,您应该首先设置
display: none
,因为.fadeIn
会将图像的不透明度从当前状态设置为 100%。那么loading.gif 又如何呢?
有很多方法可以选择,例如首先可以将每个图像的
src
设置为loading.gif
,然后在ajax请求后,将更改src
到您的图像。在这种情况下,您应该在更改每个 src 之前设置display: none
。或者您可以拥有其他具有绝对位置的 img 元素(作为加载)。在这种情况下,您应该在 ajax 请求后将其样式更改为
display: none
。快乐编码;)
As aviv says, you should set the
display: none
at first, because.fadeIn
animates the opacity of image from it's current state to 100%.And what about loading.gif.
there are many ways you can go, for example at firts you can set the
src
of each image toloading.gif
, and then, after ajax request, you will change thesrc
to your image. in this case you should setdisplay: none
before changing each src.Or you can have other img elements(as loadings) with position absolute for example. In this case you should change their style to
display: none
after ajax request.happy coding;)
您好,感谢您的回答,但我仍在为这个问题绞尽脑汁。
基本上我可以让每个部分单独工作,但不能全部一起工作。
我无法解决的问题是:
为了能够淡出loading.gif 上的图像,该 gif 必须是背景图像。
要加载图像,需要一个ajax请求,并且图像只有在通过ajax成功加载后才会加载到页面上。
我需要将loading.gif 与页面一起加载,这意味着我必须在页面上设置一个包含 100 个 LI 的空白结构。
然后我可以使用
.html()
将插入 LI.. 是吗?
但我遇到的问题是,页面在执行此操作时会冻结,并且会立即加载所有图像,而不是一次加载一个图像。
所以我想,创建一个每个img都可以运行onload的函数,它启动ajax脚本来获取下一个要加载的图像。
我认为这不太好。我在编写函数时遇到问题。
其原理是:
image0 加载并淡入,
image0 完成加载并激活
loadNext()
在
loadnext()
中:id 增加,(这是 xml 中的一个 attr,从 0 到 100)
获取与新 id 相关的 url(xml 中的元素)
添加到无序列表,并在 image1 onload 上运行
loadNext()
。但我无法让它工作:(
请帮忙
Hi, thanks for the answers but I am still racking my brain over this problem.
basically i can get each part to work seperatly, but not all together.
The problem I cannot get my head around is this:
To be able to fade the image ontop of the loading.gif, the gif needs to be a bg image.
To load the images, an ajax request is needed and images are only loaded on the page once they have successfully loaded via ajax.
I need the loading.gif's to load with the page, which means I have to set up a blank structure of 100 LI's on the page.
I can then use
.html()
to insert the<img src=[url from xml file] />
into the LI.. yes?But the problem I am having is that the page freezes while it is doing this and it loads all the images at once as opposed to one at a time.
So I thought, create a function that each img can run onload, which starts the ajax script to get the next image to load.
I don't think this is great. And I am having a problem writing the function.
Theory of it goes:
image0 loads and fades in,
image0 completes loading and activates
loadNext()
In
loadnext()
:the id is increased, (which is an attr in the xml starting 0 to 100)
get the url (which is an element in xml) relevant to the new id
add to the unordered list, and run
loadNext()
on image1 onload.I cant get it to work though :(
help please
补丁不适用于 drupal,但 cdn 模块可与 pressflow 配合使用。
试试这个:
Patches will not work on drupal, however cdn module works with pressflow.
Try this: