Jquery Cycle 插件 - Ajax 图像加载的大问题
我遇到了 Jquery Cycle 插入问题。
我通过 Ajax 动态加载图库的图像。
当我单击图库的下一个或上一个按钮时,序列无法正常工作。
有时,activeSlide 类不会更改元素,或者会丢失图库中的某个元素。
如果我刷新页面它就可以正常工作。
当我进行 ajax 调用并更改图库内的图像时,我会在重新声明插件之前和之后销毁该插件。
如果我设置自动滑动超时,一切正常。
我发布代码。
有人可以帮助我吗?我花了三天时间试图找到解决方案:((((((
谢谢 Davide。
这是我的代码:
success: function(data){
$('#imgcaption').cycle('destroy');//Before load data I destroy the gallery
$('#dettaglio #imgcaption').empty();//I clean the div's
$('#dettaglio #nav').empty();
//I insert the new Images
$.each(data.foto, function(index, value){
$('#imgcaption').append('<div><img src="' + value + '" alt="" /></div>');
});
//I redeclare the Cycle
$('#imgcaption').cycle({
timeout: 0,
fx: 'scrollHorz',
speed: 500,
pager: '#nav',
pagerAnchorBuilder: function(idx, slide) {
return '<a href="#"></a>';
}
});
I've got a problem with the Jquery Cycle pluging.
I load dinamically via Ajax the images of the gallery.
When I click the buttons next or prev of the gallery, the sequence doesn't work correctly.
Sometimes the class activeSlide doesn't change element or an element of the gallery is missed.
If i refresh the page it work correctly.
When i make the ajax call and I change the images inside the gallery I destroy the plugin before and after I redeclare it.
IF I set timeout for the autosliding allworks correctly.
I post the code.
Someone can help me? It's 3 days I try to find a solution :(((((((
Thank you Davide.
This is my code:
success: function(data){
$('#imgcaption').cycle('destroy');//Before load data I destroy the gallery
$('#dettaglio #imgcaption').empty();//I clean the div's
$('#dettaglio #nav').empty();
//I insert the new Images
$.each(data.foto, function(index, value){
$('#imgcaption').append('<div><img src="' + value + '" alt="" /></div>');
});
//I redeclare the Cycle
$('#imgcaption').cycle({
timeout: 0,
fx: 'scrollHorz',
speed: 500,
pager: '#nav',
pagerAnchorBuilder: function(idx, slide) {
return '<a href="#"></a>';
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了同样的问题。显然 jQuery Cycle 在 ajax 加载后仍然可以看到旧的 DOM 元素。因此,当您将 .cycle() 应用于新的 AJAX DOM 时,jQuery Cycle 会将其绑定到来自 AJAX 的新 DOM 和“删除”的旧 DOM。
这就是为什么你的分页全部搞砸了(更具体地说,第一组页面链接引用旧的 DOM,后面一组引用新的 AJAX DOM)。
因此,解决方法是在执行 .cycle('destroy') 之后手动删除与循环关联的 DOM 元素
,或者就您的情况而言:
我希望有帮助!网上有很多关于这个问题的帖子,但没有提出解决方案。现在我们有了一个。 :)
I was having this same issue. Apparently jQuery Cycle still sees the old DOM elements after an ajax load. So when you go to apply the .cycle() to the fresh AJAX DOM comes in, jQuery Cycle binds it to both the new DOM from AJAX and the old DOM that was "removed".
That's why your pagination is all screwed up (more specifically, the fist set of page links refer to the old DOM and the later set refer to the fresh AJAX DOM).
So, a fix is to manually remove the DOM elements associated with the cycle after you do your .cycle('destroy')
Or in your case:
I hope that helps! There are lot of posts online about this issue with no proposed solutions. Now we have one. :)