How to get Isotope working with dynamic DOM nodes?

折月煮酒 提交于 2019-12-06 07:29:20

问题


I the context of Backbone views, I add and remove views in the DOM. However, the container that contains Isotope elements only works during the initial load; after that, when the container is removed, and added again, Isotope is not working as expected.

I did quick JSfiddle here: http://jsfiddle.net/mulderp/T8aSQ/6/ -->

When I add the Isotope container:

var list = '<div data-foo="bar" id="container"><div class="item red"></div><div class="item blue"></div></div>';
$container.html(list);
$container.isotope('shuffle');

Isotope is not running anymore, although the DOM structure would look similar to the first.

Anyone has an idea what happens, and how to have Isotope read the elements a new as during the first initialization?


回答1:


I stumbled upon the same issue some time ago, and I resolve it in this way, if you want to keep Isotope's animation between operations:

In your remove function instead of $container.html(''); write:

var elToRemove = $container.data('isotope').$filteredAtoms;
$container.isotope('remove',elToRemove);

And in your create function, instead of $container.html(list);:

$container.isotope('insert',$(list));

Otherwise if you don't need animation between operations you could keep your functions as they are and just re-initialize Isotope in you create function before calling shuffle.

Working fiddle: http://jsfiddle.net/T8aSQ/7/



来源:https://stackoverflow.com/questions/14896464/how-to-get-isotope-working-with-dynamic-dom-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!