简体   繁体   English

使用Laravel / jQuery异步加载图像

[英]Loading images asynchronously with Laravel/jquery

I have a bunch of images in a folder and I have the following doubts: What should I return from Laravel action? 我的文件夹中有一堆图像,并且我有以下疑问:Laravel操作应该返回什么? How to display some text while the images are loading on the client? 在客户端上加载图像时如何显示一些文本?

Normally I include in my question some code I've tried but I don't even know how to start. 通常我会在问题中加入一些我尝试过的代码,但我什至不知道如何开始。 Google doesn't help much because the keywords "image" and "ajax" leads to uploading not downloading issues. Google并没有太大帮助,因为关键字“ image”和“ ajax”会导致上传不下载的问题。

you can do something like this: 您可以执行以下操作:

HTML HTML

<div id="loadingDiv"><p>i'm loading...</p></div>        

JS JS

var $loading = $('#loadingDiv').hide();

$(document)
  .ajaxStart(function () {
     $loading.show();
   })
   .ajaxStop(function () {
     $loading.hide();
   });


// your ajax calls

more infos here: How to show loading spinner in jQuery? 更多信息在这里: 如何显示在jQuery中加载微调器?

I would suggest you sending back a JSON response. 我建议您发送回JSON响应。 The Controller's method would find the right image names/relative paths and return all that set packaged in a JSON response. Controller的方法将找到正确的图像名称/相对路径,并返回打包在JSON响应中的所有设置。 You can add any other information along too. 您也可以添加任何其他信息。

return response()->json([
      'images' => <image-array-set>,
      'something_else' => $something_else_here
]);

Then in your client side/Javascript you'd parse this information and load the images accordingly, and do anything else you want in the process. 然后,在客户端/ Javascript中,您将解析此信息并相应地加载图像,并在此过程中执行任何其他操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM