为了账号安全,请及时绑定邮箱和手机立即绑定

如何创建尽可能多的动态El,就像我们从PHP收到的那样

如何创建尽可能多的动态El,就像我们从PHP收到的那样

PHP
慕尼黑8549860 2022-09-17 17:27:35
在主页上有图库,点击一个图像需要打开光画廊幻灯片与在ftp目录上找到的图像,与画廊名称点击主页上。PHP用于查找图像并计数:<?phpheader("Content-Type: application/json");$dirname = $_POST["galleryName"];$imgGallery = glob("../gallery/" . $dirname . "/".$dirname."_*.*");$thumbGallery = glob("../gallery/" . $dirname . "/thumb_*.*");$countImages = count($imgGallery);echo json_encode(array("imgNormal" => $imgGallery, "imgThumb" => $thumbGallery, "imgCount" => $countImages));?>断续器:$('.info').click(function() {    var galleryName = $(this).closest('.imgGallery').find('img.img-thumbnail').attr('name');    $.ajax({        url: "gallery/imgFinder.php",        dataType: "json",        type: "post",        data: {            galleryName: galleryName        },        success: function(xhr) {            if (xhr.imgCount != 0) {                console.log(xhr.imgNormal);                console.log(xhr.imgThumb);                console.log(xhr.imgCount);                for (var i = 1; i <= xhr.imgCount; i++) {                    $(this).lightGallery({                        dynamic: true,                        dynamicEl: [{ //We need to create as much as we received w "xhr.imgCount"                                        "src": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg",                                        "thumb": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg"                        }]                    })                    return;                }            } else {                console.log('Gallery \'' + galleryName + '\' has no images');                return;            }        },        error: function(xhr) {            console.error("Some error found");        }    });});我们需要使用图像/拇指变量创建尽可能多的动态El,就像我们收到 xhr.imgCount 一样要获得类似这样的东西:dynamicEl: [{    "src": '...',    'thumb': '...'}, {    'src': '...',    'thumb': '...'}, {    'src': '...',    'thumb': '...'}, {    'src': '...',    'thumb': '...'}]
查看完整描述

1 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

在你的JS代码中,你需要更新如下内容:


if (xhr.imgCount != 0) {

        console.log(xhr.imgNormal);

        console.log(xhr.imgThumb);

        console.log(xhr.imgCount);

        var dynamicEls = [];

        for (var i = 0; i <= xhr.imgCount; i++) {


              dynamicEls[i] = { 

                    "src": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg",

                    "thumb": "/gallery/" + galleryName + "/" + galleryName + "_" + i + ".jpg"

                };

        }

        dynamicEls.pop(); //For remove unrealized last url/data

        $(this).lightGallery({

           dynamic: true,

           dynamicEl: dynamicEls

        });

    }

因此,我正在使用和温度变量,并在循环后将其填充在正确的位置。dynamicEls


查看完整回答
反对 回复 2022-09-17
  • 1 回答
  • 0 关注
  • 144 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号