简体   繁体   English

我想以“波浪”形式加载图像。

[英]I would like to load images in a “wave”..

<html>
<section>

<style>
img{
 width: 150px;
 height:150px;
 float:left;
 }
 </style>
 </section>


 <script>

 var img = undefined,
 section = document.querySelector('section'),
 images=[
 "http://www.psdgraphics.com/file/red-number-0.jpg",
 "http://www.psdgraphics.com/file/red-number-1.jpg",
 "http://www.psdgraphics.com/file/red-number-2.jpg",
 "http://www.psdgraphics.com/file/red-number-3.jpg",
 "http://www.psdgraphics.com/file/red-number-4.jpg",
 "http://www.psdgraphics.com/file/red-number-5.jpg",
 "http://www.psdgraphics.com/file/red-number-6.jpg"
  ];

  function loadImage( i ){
   img = document.createElement('img');
   section.appendChild(img);
   img.dataset['index'] = i;
   img.src=images[i];
 }
 for(var i=0;i<images.length;i++){

  loadImage(i)

  }

  </script>


  </html>

This is my code, at the moment I receive all the images at the same time... I would like to load each image only when the previous one has been load.. 这是我的代码,目前我同时接收所有图像...仅在加载前一个图像时才想加载每个图像。

Any help will be gratitude 任何帮助将不胜感激

Simply assign a pseudorecursive callback to the onload handler: 只需将伪递归回调分配给onload处理程序即可:

function loadImage( i ){
  if(i >= images.length) return;
  var img = document.createElement('img');
  section.appendChild(img);
  img.dataset['index'] = i;
  img.src=images[i];
  img.onload = () => loadImage(i+1);
}

loadImage(0);

Or use some new ES 7 stuff: 或使用一些新的ES 7内容:

(async function(){

 for(var i = 0; i < images.length; i++){
  var img = document.createElement('img');
  section.appendChild(img);
  img.dataset['index'] = i;
  img.src=images[i];

  await new Promise(r => image.onload = r );
 }

 })()

Probably some setTimeout along with recursion can give a nice effect: 可能一些setTimeout以及递归可以带来很好的效果:

 var img = undefined, section = document.querySelector('section'), images = [ "http://www.psdgraphics.com/file/red-number-0.jpg", "http://www.psdgraphics.com/file/red-number-1.jpg", "http://www.psdgraphics.com/file/red-number-2.jpg", "http://www.psdgraphics.com/file/red-number-3.jpg", "http://www.psdgraphics.com/file/red-number-4.jpg", "http://www.psdgraphics.com/file/red-number-5.jpg", "http://www.psdgraphics.com/file/red-number-6.jpg" ]; function loadImage(i) { img = document.createElement('img'); section.appendChild(img); img.dataset['index'] = i; img.src = images[i]; } var index = 0; function loadNext() { if(index < images.length) { loadImage(index); setTimeout(function(idx){ loadNext(); },200, index++); } } loadNext(); 
 img { width: 150px; height: 150px; float: left; } 
 <html> <section> </section> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在我的网络中放置一个三角形并用图像填充它 - I would like to have a triangle in my web and filled it with images 在这种情况下,我如何让加载更多按钮工作? - How would I make load more button work in a case like this? 我想在div中移动图像,以便包含div的图像准确显示其大小 - I would like to move images in a div so the containing div displays them exactly their size 我希望下拉菜单选项在选择不同的选项时能够发布不同的图像 - I would like drop down menu selections to bring post different images when different choices are selected 我想从一个 Google 表格单元格中拆分多个 URL,然后将它们作为图像添加到 Google Doc 中 - I would like to split multiple URLs from a single Google Sheets cell and then add them as images into a Google Doc 我想在DIV中合并所有图像并允许用户下载(js powered) - I would like to merge all images within DIV and allow user to download (js powered) 加载外部页面并使用它的元素,就像我通常会使用它们一样? - Load external page and use it's elements like I would use them normally? 希望在我创建的自定义表单框中直观地加载图片(也可以删除) - Would like to have a picture visually load in a custom form box I have created (also able to delete) 我希望能够通过单击超链接来加载/编辑 handlebars.js 模板(快速服务器),有什么建议吗? - I would like to be able to load/edit handlebars.js templates ( express server ) by clicking on a hyperlink , any suggestions? 我想在任何浏览器的开发控制台(JavaScript)中将网址列表加载到var - I would like to load a list of urls to a var in any browsers' dev console (JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM