简单瀑布流
偶尔做个小东西 这里mark 下 以后逐渐优化
<script>
var Global = Global || {};
Global.begin = 16;
Global.isProcessing = 0;
$(function(){
$(window).scroll(function(){
var win_h=$(window).height()-200;
var sct=$(document).scrollTop();
var news_h=$("#news_label").offset().top;
if((news_h-sct)<=win_h){
if(Global.isProcessing == 0){
doNewNews();
}
}
})
})
function doNewNews(){
Global.isProcessing = 1;
$.ajax({
cache: false,
type: "GET",
url:"/api/news.php",
data:{begin:Global.begin,limit:4},// 你的formid
async: false,
xhrFields: {
withCredentials: true
},
error: function(request) {
alert("Connection error");
},
success: function(data) {
var tmp_html = '';
data=eval("("+data+")");//转换为json对象
var tmp_len = 0;
for(i in data){
var tmp_record = data[i];
tmp_html +='<li>';
tmp_html +='<span class="image"><img src="/app/'+tmp_record['NewsPhoto']+'" /></span>';
tmp_html +='<span class="text">';
tmp_html +='<b>'+tmp_record['NewsTitle']+'</b>';
tmp_html +='<a href="aboutus_news_detail.php?id='+tmp_record['ID']+'"><img src="images/icon_lian.png" border="0" /></a>';
tmp_html +='</span>';
tmp_html +='</li>';
tmp_len ++;
}
if (tmp_len == 0){
alert('没有更多了!');
return;
}
Global.begin = tmp_len + Global.begin;
$(".moment ul").append(tmp_html);
Global.isProcessing = 0;
console.log(data);
return;
}
});
}
</script>
后台PHP
<?php
//Sort:排序,0按后台指定的数字排序[降序],1按添加时间[降序],2按点击次数[降序],3按推荐优先排序,4按添加时间[升序]。[默认为0]
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
$dbh = new PDO("mysql:host=localhost;dbname=;",'root','');
$limit = '';
$order = '';
$begin = 0;
$orderConf = array(
'3'=>' ORDER BY NewsRecommended DESC',
);
if( isset($_GET['order']) && isset($orderConf[$_GET['order']])){
$order = $orderConf[$_GET['order']];
}
if(isset($_GET['begin'])){
$begin = intval($_GET['begin']);
}
if(isset($_GET['limit'])){
$limit = ' LIMIT '.$begin.','.intval($_GET['limit']);
}
$article = $dbh->query('SELECT ID,NewsTitle,NewsPhoto FROM song_news WHERE NewsLang=1'.$order.$limit)->fetchAll();
$data = array();
foreach ($article as $key=>$value){
$data[$value['ID']] = $value;
}
die(json_encode($data));
