1

我正在从 mySQL 数据库中提取 50 条最新记录。每个都进入一个具有同位素并完美运行的 DIV - DIV 动画、重置等。

使用 AJAX 使用 OFFSET 调用接下来的 50 条记录,但是,新记录会加载到新的 DIV 中,但 Isotope 的类不会应用于它们(通过 Web Inspector 可以看到)。

设置:

index.php = 在浏览器中加载时调用数据库,同位素工作正常。index.php (a#update_newImages) 上的链接会触发侦听器加载“load-ajax.php”。

load-ajax.php = 只有 SQL SELECT 和 PDO 循环的外部页面。这些记录加载但未应用同位素,因此存在问题。

来自 index.php 的代码

 ...database connection info and query code go here

 $filter = ""; // appears in the echo'd DIV below, for filtering the ISOTOPE divs. Turned off til this injection problem is solved

 //ISOTOPE SETTINGS, in <HEAD>
 var $container = $('#theContent');
 $container.isotope({
 layoutMode : 'fitRows', //leave blank for default masonry
 filter: '*',
 animationOptions: {
 duration: 750,
 easing: 'linear',
 queue: false,
 }
 });


in BODY:

<div id="theContent">
<?php  
for($i=0; $links = $query_links->fetch(); $i++){

echo "<div class=\"".$filter." box\"><a href=\"#\" data-filter=\"." . $filter . "\" class=\"theCat " . $filter . "\">" . $links['ATtitle']."</a><br>" .  "#" . $links['LID']."-
<a href=\"". $links['URL']."\" target=\"_blank\" class=\"theURL\">". $links['descr']."</a></div>";
}
?>
</div><!-- theContent -->


<script> // RIGHT BEFORE BODY TAG CLOSES
var offset_newImages = 0; // initial offset value
var newImages = document.getElementById('update_newImages'); // a link on the page
newImages.addEventListener('click', function() {
  event.preventDefault();
     offset_newImages += 50; // increments batches of records
     $.get('load-ajax.php?loadDataType=newImages&offset='+offset_newImages, function(data) {
      $("#theContent").hide().html(data).fadeIn(600);

//**EDIT**
     alert('Load was performed.'); // callback on success, works - is this where the Isotope "appended" code would go?

    }, false);
   });
</script> 

来自 load-ajax.php 的代码

...database connection info goes here

$offset = $_GET["offset"]; // from URL
$filter = ""; // for filtering the ISOTOPE divs, turned off til the injection problem is solved

for($i=0; $links = $query_links->fetch(); $i++){
    $showList = "<div class=\"".$filter." box\"><a href=\"#\" data-filter=\"." . $filter . "\" class=\"theCat " . $filter . "\">" . $links['ATtitle']."</a><br>" .  "#" . $links['LID']."-
<a href=\"". $links['URL']."\" target=\"_blank\" class=\"theURL\">". $links['descr']."</a></div>";
echo $showList; // this is where ISOTOPE is not applied after each AJAX injection
}

我在想有一个回调解决方案,但我不确定下一步该做什么。

注意:我已经尝试过 Paul Irish 的 Isotope + 无限滚动,但在我可以将无限滚动的分页机制从 mySQL 转换为 JSON 之前,不能在这里使用它。下一个项目。


编辑:我已将index.php修改为如下所示。问题仍然存在,但我认为它几乎就在那里。ajax 正在工作,但是当 Isotope 启动时,它不会在新的 DIV 上添加它的类。

<head>

<script type="text/javascript">
$(document).ready(function(){
//ISOTOPE SETTINGS
var $container = $('#container');
$container.isotope({
       layoutMode : 'fitRows', //leave blank for default masonry
       filter: '*',
       animationOptions: {
        duration: 750,
        easing: 'linear',
        queue: false,
    }
});
});
</script>

就在之前</body>

<script>
var offset_newImages = 0; // initial offset value
var newImages = document.getElementById('update_newImages'); // a link on the page
newImages.addEventListener('click', function() {
 offset_newImages += 50;
 $.ajax({
    type: 'GET',
    url: "load-ajax.php?offset="+offset_newImages,
    success:function(data){
          //    alert(data); // works
          $("#container").hide().html(data).fadeIn(600) // fades in the new recordset
          $container.isotope('insert', data); 
      }
  });
});
</script>

所以总结一下,新数据加载到 DIV 中 - 我可以看到它,直到我以任何方式调整浏览器窗口的大小,这是 Isotope 开始并使用其 CSS 隐藏新 DIV 的地方。

4

1 回答 1

1

Isotope 有多种方法可以在动态插入新内容后重新计算布局。

于 2013-05-24T14:01:26.620 回答