所以我正在创建一个网站来显示一个文件夹,上面附有无限滚动功能。我只是把它弄下来,唯一的问题是我在javascript中有一个函数调用“createImge”函数,该函数从一个选择随机图像并返回该图像路径的php文件调用json。问题是,当我连续多次调用createImage时,它返回相同的图像,并且php不会创建一个新的随机数来选择文件。有关如何获取createImg()以使php在连续多次调用时选择不同的随机数的任何想法???
index.html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request json test</title>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
var totalLoad;
var loadedImg=new Array();
var maxLoaded=20;
var loadFirst=5;
var bufferLoad=3;
var imgSrc;
function loadTop(){
for(var i=0;i<loadFirst;i++){
createImg();
}
}
function remove(id)
{
$('.'+id).remove();
}
function createImg(){
$.getJSON('json-data.php', function(data) {
alert("Return json-data"+data.img);
$('#showdata').html($('#showdata').html()+"<div id="+getImageName(data.img)+" ><img width=\"450px\" src=\""+data.img+"\"></div><br/>");
});
}
function getImageName(src){
var path = src;
var tarr = path.split('/'); // ["static","images","banner","blue.jpg"]
var file = tarr[tarr.length-1]; // "blue.jpg"
var data = file.split('.')[0]; // "blue"
return data;
}
$(document).ready(function(){
$(window).scroll(function(){
// grab the scroll amount and the window height
var scrollAmount = $(window).scrollTop();
var documentHeight = $(document).height();
// calculate the percentage the user has scrolled down the page
var scrollPercent = (scrollAmount / documentHeight) * 100;
if(scrollPercent > 80) {
for(var i=0;i<bufferLoad;i++){
createImg();
}
}
if(totalLoad>maxLoaded){
}
});
});
</script>
</head>
<body>
<div id="showdata" style="width:450px;margin-left:auto;margin-right:auto;">
<script type="text/javascript">
loadTop();
</script> </div>
</body>
</html>
json-data.php代码:
$files = glob("Approved/*.*");
$no = sizeof($files)-1;
$random = rand(0, $no);
$imgHold=$files[$random];
$imgPull = array('img' =>$imgHold);
echo json_encode($imgPull);
?>
非常感谢