JS 插件dropload下拉刷新、上拉加載使用小結(jié)
更新時間:2017年04月13日 14:26:04 投稿:mrr
這篇文章主要介紹了JS 插件dropload下拉刷新、上拉加載使用小結(jié),需要的朋友可以參考下
前端展示php代碼;
<?php
header("Content-type: text/html; charset=utf-8");
include_once("./config.php");
// 初始顯示界面數(shù)據(jù)獲取
$data = file_get_contents(URL."/interfaces/page.php?paging=1");
// echo $data;die;
$data = json_decode($data);
// var_dump($data);die;
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta name="viewport" content="initial-scale=1, user-scalable=0, minimal-ui" charset="UTF-8">
<title>加載頂部、底部</title>
<!-- 頁面布局 -->
<link rel="stylesheet" type="text/css" href="./css/display.css" rel="external nofollow" >
<!-- 框架布局 -->
<link rel="stylesheet" href="./css/dropload.css" rel="external nofollow" >
</head>
<body>
<div class="header">
<h1>就當我是新聞頁吧</h1>
</div>
<div class="content">
<div class="lists">
<!-- lists在外 避免造成多次循環(huán)輸出 -->
<?php for ($i=0; $i < count($data->data) ; $i++) { ?>
<a class="item" href="#" rel="external nofollow" >
<img src="./img/pic.jpg" alt="">
<h3>hehe</h3>
<span class="date"><?php echo $data->data[$i]->id; ?></span>
</a>
<?php } ?>
</div>
</div>
<div class="footer">
<a href="#1" rel="external nofollow" class="item">測試菜單</a>
<a href="#2" rel="external nofollow" class="item">只做展示</a>
<a href="#3" rel="external nofollow" class="item">無功能</a>
<a href="#4" rel="external nofollow" class="item">不用點</a>
</div>
<!-- jQuery1.7以上 或者 Zepto 二選一,不要同時都引用 -->
<script src="./js/jquery.min.js"></script>
<script src="./js/dropload.min.js"></script>
<script>
$(function(){
var paging = 1;//頁碼數(shù)
// dropload函數(shù)接口設(shè)置
$('.content').dropload({
scrollArea : window,
// 下拉刷新模塊顯示內(nèi)容
domUp : {
domClass : 'dropload-up',
// 下拉過程顯示內(nèi)容
domRefresh : '<div class="dropload-refresh">↓下拉過程顯示內(nèi)容-下拉刷新-自定義內(nèi)容</div>',
// 下拉到一定程度顯示提示內(nèi)容
domUpdate : '<div class="dropload-update">↑釋放更新-自定義內(nèi)容</div>',
// 釋放后顯示內(nèi)容
domLoad : '<div class="dropload-load"><span class="loading"></span>加載中-自定義內(nèi)容...</div>'
},
domDown : {
domClass : 'dropload-down',
// 滑動到底部顯示內(nèi)容
domRefresh : '<div class="dropload-refresh">↑上拉加載更多-自定義內(nèi)容</div>',
// 內(nèi)容加載過程中顯示內(nèi)容
domLoad : '<div class="dropload-load"><span class="loading"></span>加載中-自定義內(nèi)容...</div>',
// 沒有更多內(nèi)容-顯示提示
domNoData : '<div class="dropload-noData">暫無數(shù)據(jù)-自定義內(nèi)容</div>'
},
// 1 . 下拉刷新 回調(diào)函數(shù)
loadUpFn : function(me){
$.ajax({
type: 'GET',
// 每次獲取最新的數(shù)據(jù)即可
url: './interfaces/page.php?paging=1',
dataType: 'json',
success: function(data){
// $.ajax()雖然接口提供json字符串,但接收到的是 json對象
// alert(typeof(data));
var result = '';
// 循環(huán)拼接顯示內(nèi)容 DOM
// 刷新獲取多少數(shù)據(jù),顯示多少 使用html()重置 lists DOM
for(var i = 0; i < data.data.length; i++){
result += '<a class="item opacity" href="'+data.data[i].link+'" rel="external nofollow" rel="external nofollow" >'
+'<img src="'+data.data[i].pic+'" alt="">'
+'<h3>hehe</h3>'
+'<span class="date">'+data.data[i].id+'</span>'
+'</a>';
}
// 為了測試,延遲1秒加載
setTimeout(function(){
// 插入加載使用 html() 重置 DOM
$('.lists').html(result);
// 每次數(shù)據(jù)加載完,必須重置
me.resetload();
},1000);
},
// 加載出錯
error: function(xhr, type){
alert('Ajax error!');
// 即使加載出錯,也得重置
me.resetload();
}
});
},
// 2 . 上拉加載更多 回調(diào)函數(shù)
loadDownFn : function(me){
paging++; // 每次請求,頁碼加1
$.ajax({
type: 'GET',
url: './interfaces/page.php?paging='+paging,
dataType: 'json',
success: function(data){
// data = JSON.parse(data);
var result = '';
// 選擇需要顯示的數(shù)據(jù) 拼接 DOM
for(var i = 0; i < data.data.length; i++){
result += '<a class="item opacity" href="'+data.data[i].link+'" rel="external nofollow" rel="external nofollow" >'
+'<img src="'+data.data[i].pic+'" alt="">'
+'<h3>heheh</h3>'
+'<span class="date">'+data.data[i].id+'</span>'
+'</a>';
if(data.data.length<15){
// 再往下已經(jīng)沒有數(shù)據(jù)
// 鎖定
me.lock();
// 顯示無數(shù)據(jù)
me.noData();
break;
}
}
// 為了測試,延遲1秒加載
setTimeout(function(){
// 加載 插入到原有 DOM 之后
$('.lists').append(result);
// 每次數(shù)據(jù)加載完,必須重置
me.resetload();
},1000);
},
// 加載出錯
error: function(xhr, type){
alert('Ajax error!');
// 即使加載出錯,也得重置
me.resetload();
}
});
},
threshold : 50 // 什么作用???
});
});
</script>
</body>
</html>
后端分頁接口
<?php
header("Content-type: text/html; charset=utf-8");
// 包含數(shù)據(jù)庫配置信息
include_once('../config.php');
// 接收數(shù)據(jù)
$paging = isset($_REQUEST['paging'])?$_REQUEST['paging']:'';
// $paging = 1;
$num = 15;//每頁顯示記錄條數(shù)
$start_page = $num*($paging-1);// 每頁第一條記錄編號
// 用于返回數(shù)據(jù)
$return = array();
$data = array();
/* mysqli 面向?qū)ο?編程方式 */
// 1 . 創(chuàng)建數(shù)據(jù)庫鏈接
$conn = new mysqli($servername,$username,$password,$database);
if ($conn->connect_error) {
die("連接失敗 : ".$conn->connect_error);
}
// echo "鏈接成功";
// 設(shè)置字符集(以防出錯 每次都要寫)
$conn->query("SET NAMES utf8");
// 2 . 數(shù)據(jù)操作
$sql = "SELECT * FROM mission_news order by id desc limit $start_page , $num;";
// $sql = "SELECT * FROM mission_news order by id desc limit 0 , 15;";
// 3 . 執(zhí)行一條語句
$ret = $conn->query($sql);
// 4 . 循環(huán)獲取每條記錄
if ($ret->num_rows > 0) {
while ($row = $ret->fetch_assoc()) { //將每條記錄以 數(shù)組形式 返回
// var_dump($row);
// echo "id = ".$row['id']." mid = ".$row['mid']." context = ".$row['context']."<br>";
$tmp = array();//臨時數(shù)組整合信息
$tmp['id'] = $row['id'];
$tmp['mid'] = $row['mid'];
$tmp['context'] = $row['context'];
$tmp['turn'] = $row['turn'];
$tmp['created'] = $row['created'];
// 臨時數(shù)組 拼接到 返回的數(shù)組中
$data[] = $tmp; // 自增
}
// 拼接返回數(shù)組
$return['result'] = 1;
$return['data'] = $data;
}
// 5 . 關(guān)閉數(shù)據(jù)庫
$conn->close();
// 6 . 編碼為json字符串返回
echo json_encode($return);
?>
以上所述是小編給大家介紹的JS 插件dropload下拉刷新、上拉加載使用小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
實現(xiàn)JavaScript中數(shù)據(jù)響應(yīng)的方法總結(jié)
JavaScript 數(shù)據(jù)響應(yīng)是一種重要的前端開發(fā)概念,是指在應(yīng)用程序中的數(shù)據(jù)發(fā)生變化時,能夠自動更新與這些數(shù)據(jù)相關(guān)的用戶界面(UI)部分的能力,本文我們來總結(jié)一下目前可以簡單實現(xiàn) JavaScript 中的數(shù)據(jù)響應(yīng)的方法,需要的朋友可以參考下2023-09-09
微信小程序如何加載數(shù)據(jù)庫真實數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了微信小程序如何加載數(shù)據(jù)庫真實數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

