gpt4 book ai didi

javascript - Ajax 调用替换图库图像后,lightGallery 无法正常工作

转载 作者:行者123 更新时间:2023-12-03 00:53:36 25 4
gpt4 key购买 nike

我安装了一个 lightGallery,当我加载页面时它效果很好。

但是当我执行 Ajax 调用来更新 lightGallery 图像时,lightSlider 不起作用。

我的问题是,我可以在 Ajax 后更新内容并全屏查看图像,但每次执行此 Ajax 调用时,这些行都会重新初始化。

enter link description here

换句话说,在 HTML 中我遇到以下问题:

enter image description here

在我的文件 main.js 中我初始化它:

ma​​in.js

    const settings = {
gallery:true,
item:1,
loop:false,
thumbItem:4,
slideMargin:0,
enableDrag: true,
currentPagerPosition:'left',
onSliderLoad: function(el) {
el.lightGallery({
selector: '#imageGallery .lslide'
});
}
}
const slider = Main.selectors.gallery.find('ul').lightSlider(settings);

Main.selectors.gallery.addClass('init');//I added this line to be able to view the list of images as slides

在我的 secondaryFile.js 中,我调用 Ajax 调用,并在成功后更新图片库的图像

secondFile.js

success: function (response) {
if($('#imageGallery').html().length != 0 && response.responseDto.mediaProps.length != 0) {
$('#imageGallery').empty();
}
if (response.responseDto.mediaProps.length != 0) {
$.each(response.responseDto.mediaProps, function(i, item) {
$('#imageGallery').append('<li class="lslide" style="width: 411.656px; margin-right: 0px;" data-thumb="' + item + '" data-src="' + item + '"><img src="' + item + '"></li>');
});
}

if (response.responseDto.videoProps.length != 0 && response.responseDto.videoProps[0] != null) {
$.each(response.responseDto.videoProps, function (i, item) {
$('#imageGallery').append('<li class="lslide" style="width: 411.641px; margin-right: 0px;" data-thumb= "http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif" data-poster="http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif" href=' + item + '><img src="http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif"><span class="icon-youtube"></span></li>');
});
}

// Destroy lightSlider and lightGallery
Main.selectors.gallery.find('ul').data('lightGallery').destroy(true);
slider.destroy();

// re-initialize
Main.selectors.gallery.find('ul').lightSlider(settings);

/* if (initial) {
Main.initProductDetails();
}*/

此 lightSlider 的内容位于名为“imageGallery”的 div 内

            <div class="col-sm-12 col-md-7">
<div class="field field-gallery">
<ul id="imageGallery"></ul>
</div>
</div>

最佳答案

添加新项目后,您需要销毁并重新启动 lig​​htGallery。

main.js

const settings = {
gallery:true,
item:1,
loop:false,
thumbItem:4,
slideMargin:0,
enableDrag: true,
currentPagerPosition:'left',
onSliderLoad: function(el) {
el.lightGallery({
selector: '#imageGallery .lslide'
});
}
}
const slider = Main.selectors.gallery.find('ul').lightSlider(settings);

seccondFile.js

success: function (response) {
if($('#imageGallery').html().length != 0 && response.responseDto.mediaProps.length != 0) {
$('#imageGallery').empty();
}
if (response.responseDto.mediaProps.length != 0) {
$.each(response.responseDto.mediaProps, function(i, item) {
$('#imageGallery').append('<li class="lslide" style="width: 411.656px; margin-right: 0px;" data-thumb="' + item + '" data-src="' + item + '"><img src="' + item + '"></li>');
});
}

if (response.responseDto.videoProps.length != 0 && response.responseDto.videoProps[0] != null) {
$.each(response.responseDto.videoProps, function (i, item) {
$('#imageGallery').append('<li class="lslide" style="width: 411.641px; margin-right: 0px;" data-thumb= "http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif" data-poster="http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif" href=' + item + '><img src="http://www.monitor.co.ug/image/view/-/688914/data/54/-/euvqa8z/-/video.gif"><span class="icon-youtube"></span></li>');
});
}

// Destroy lightSlider and lightGallery
Main.selectors.gallery.find('ul').data('lightGallery').destroy(true);
slider.destroy();

// re-initialize
Main.selectors.gallery.find('ul').lightSlider(settings);



if (initial) {//If Ajax call was NOTTTT performed
Main.initProductDetails();
}
}

文档:lightSlider - http://sachinchoolur.github.io/lightslider/examples.html

文档:lightGallery - http://sachinchoolur.github.io/lightGallery/demos/methods.html

关于javascript - Ajax 调用替换图库图像后,lightGallery 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52945579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com