加载多个图像后按顺序淡入它们

发布于 2024-09-10 22:15:36 字数 1053 浏览 5 评论 0原文

我想要实现的是一个图像网格(10x10),首先在页面的其余部分完成加载后加载,然后在一个又一个之后淡入,

即 (仅代表一行)

----------
+---------
++--------
+++-------
++++------

等等... 到目前为止我所拥有的是这样的:

<script type="text/javascript">
var loadingImage = false;
function LoadImage(imageName,imageFile)
{
  if ((!document.images) || loadingImage) return;
  loadingImage = true;
  if (document.images[imageName].src.indexOf(imageFile)<0)
  {
    document.images[imageName].src = imageFile;
  }
  loadingImage = false;
}
LoadImage('0','images/0.jpg');
</script>

使用这个图像标签

<img class="fade" name="0" onLoad="LoadImage('1','images/1.jpg')" />

这给了我一半的解决方案,因为它按顺序加载所有图像,但我似乎无法为这些图像添加淡入淡出。图像设置在 UL 中,每个 LI 中有 10 张图片,总共 10 个 LI = 100 张图像。

我怎样才能淡入这些? 还有一种方法可以循环打印所有图像,而不是手动写入它们,这样用户就不必仔细检查并确保每个图像标签都正确命名? (我已经有了一个批量图像重命名器)

我发现这个 这与我需要的类似,但我无法弄清楚如何让它按我想要的方式加载。 谢谢

What I am trying to achieve is a grid of images (10x10) that firstly load after the rest of the page has finished loading, and secondly fade in 1 after another

i.e.
(represents only one line)

----------
+---------
++--------
+++-------
++++------

etc...
What I have so far is this:

<script type="text/javascript">
var loadingImage = false;
function LoadImage(imageName,imageFile)
{
  if ((!document.images) || loadingImage) return;
  loadingImage = true;
  if (document.images[imageName].src.indexOf(imageFile)<0)
  {
    document.images[imageName].src = imageFile;
  }
  loadingImage = false;
}
LoadImage('0','images/0.jpg');
</script>

With this image tag

<img class="fade" name="0" onLoad="LoadImage('1','images/1.jpg')" />

This gives me half the solution in as much as it loads all the images in sequentially, but I cant seem to add a fade to these images. The images are set up in an UL with 10 pictures in each LI, and 10 LI's in total = 100 images.

How can I fade these in?
Also is there a way to print out all the images in a loop insted of having them manually written in, so the user doesnt have to go through and make sure every image tag is correctly named? (ive already got a batch image renamer)

I found This which is similar to what I need, but I couldnt work out how to get it to load the way I want.
Thanks

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

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

发布评论

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

评论(1

z祗昰~ 2024-09-17 22:15:36

除了您拥有的之外,您还可以这样做,保持标记简单:

<img class="fade" name="0" src="images/1.jpg" />

然后在 CSS 中最初将它们隐藏:

.fade { display: none; }

然后在页面加载您需要按顺序显示它们的所有脚本是这样的:

$(window).load(function() {
  $(".fade").each(function(i) {
    $(this).delay(400*i).fadeIn();
  });
});

window.onload 与 document.ready 不同,它会等待所有图像都加载完毕,因此只需将该事件用于您的 .fadeIn() 代码。任何动画的默认持续时间均为 400 毫秒,并且在 .each() 中循环i是元素的索引(从0开始),因此第一个图像立即淡入(一旦所有图像加载完毕),第二个图像延迟400ms(通过.delay()),因此当第一个完成时它会消失,之后是第三个,依此类推。如果您想减慢速度,例如每次 1 秒,只需将其传递给两个函数,如下所示:

$(this).delay(1000*i).fadeIn(1000);

Instead of what you have, you can do this, keep the markup simple:

<img class="fade" name="0" src="images/1.jpg" />

Then in your CSS just have them hidden initially:

.fade { display: none; }

Then on page load all the script you need to show them sequentially is this:

$(window).load(function() {
  $(".fade").each(function(i) {
    $(this).delay(400*i).fadeIn();
  });
});

window.onload as opposed to document.ready waits until all images are loaded as well, so just use that event for your .fadeIn() code. The default duration of any animation is 400ms, and in a .each() loop i is the index (0-based) of the element, so the first image fades in instantly (once all are loaded), the second is delayed 400ms (via .delay()), so it fades when the first is done, the third after that, etc. If you want to fade slower, say 1 second each, just pass that to both functions, like this:

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