[英]Infinite Scroll JQuery / PHP (Loading page issue )
在無限滾動的情況下,如何動態加載頁面。
問題是我必須手動創建php文件。 腳本先搜索2.php,然后搜索3.php,然后搜索4.php,依此類推...我不想手動創建這些頁面,我希望它是自動的嗎? :)
我有一些感覺,我必須創建某種類型的循環才能獲取新圖像。 目前我的代碼是
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<title>Adi</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Adi</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.masonry.min.js"></script>
<script src="jquery.infinitescroll.min.js"></script>
<script src="modernizr-transitions.js"></script>
</head>
<body>
<div id="container" class="transitions-enabled infinite-scroll clearfix">
<?php
// Make a MySQL Connection
mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());
// Retrieve all the data from the "test " table
$result = mysql_query("SELECT * FROM test limit 10")
or die(mysql_error());
// store the records of the "TABLENAME" table into $row array and loop through
while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
// Print out the contents of the entry
//echo "details: ".$row['id'];
echo '<div class="box">';
echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
echo '</div>';
}
?>
</div>
<p>Hi</p>
<p> </p>
<nav id="page-nav">
<a href="2.php"></a>
</nav>
<script >
$(function(){
var $container = $('#container');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.box',
columnWidth: 60
});
});
$container.infinitescroll({
navSelector : '#page-nav', // selector for the paged navigation
nextSelector : '#page-nav a', // selector for the NEXT link (to page 2)
itemSelector : '.box', // selector for all items you'll retrieve
loading: {
finishedMsg: 'No more pages to load.',
img: 'http://i.imgur.com/6RMhx.gif'
}
},
// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
}
);
});
</script>
</body>
</html>
頁面2.php
<?php
// Make a MySQL Connection
mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());
// Retrieve all the data from the "test " table
$result = mysql_query("SELECT * FROM test limit 5")
or die(mysql_error());
// store the records of the "TABLENAME" table into $row array and loop through
while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
// Print out the contents of the entry
//echo "details: ".$row['id'];
echo '<div class="box">';
echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
echo '</div>';
}
?>
制作一個包含諸如view.php?start=100&end=150
類的參數的頁面,然后更改查詢以反映該情況:
SELECT * FROM test LIMIT <escaped and sanitized "start">, <difference between "start" and "end">
開始和結束之間的差異也可以是傳遞一些值以反映每個頁面上顯示多少個元素。
然后,您可以使用JQuery感測頁面底部何時在用戶的視口中可見,您可以使用AJAX要求服務器為您提供下一組圖像。
因此,序列如下所示:
view.php
)。 您的腳本返回頁面的完整HTML,元素0-10或類似內容 $.post('view.php?ajax=true', { start: end+1, end: end+11 }, someFunctionThatResetsEndToReflectTheNewEndAndUpdateThePage );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.