iscroll 下拉刷新,上拉加载
Posted 嘉煠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iscroll 下拉刷新,上拉加载相关的知识,希望对你有一定的参考价值。
新手,直接贴代码了
<!DOCTYPE html>
<html class="">
<head lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="css/amazeui.min.css" />
<link rel="stylesheet" href="css/acjl.css" />
<link rel="stylesheet" href="css/ydac_pm.css" />
<link href="css/mobiscroll.custom-2.15.1.min.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.12.3.js" type="text/javascript" charset="utf-8"></script>
<script src="js/mobiscroll.custom-2.15.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
document.getElementsByTagName("html")[0].style.fontSize = document.documentElement.clientWidth/20+"px";
</script>
<style type="text/css">
#receivableSheet ul li.title{font-weight: bold;color: #333;height: 40px;line-height: 40px;}
#receivableSheet .name{text-align: left;width: 33%;display: inline-block;float: left;}
#receivableSheet .receivable{text-align: center;width: 33%;display: inline-block;float: left;}
#receivableSheet .receivableed{text-align: right;width: 33%;display: inline-block;float: left;}
#wrapper { position:absolute; z-index:1; top:3rem; bottom:0.8rem; left:0.8rem;width:18.4rem;background:#fff;overflow:auto;}
#scroller { position:relative; /* -webkit-touch-callout:none;*/ -webkit-tap-highlight-color:rgba(0,0,0,0); float:left;width:100%;padding:0; }
#scroller ul { position:relative;list-style:none;padding:0;margin:0;width:100%;text-align:left;}
#scroller li {padding:0 10px;height:40px; line-height:40px;border-bottom:1px solid #ccc;border-top:1px solid #fff;background-color:#fafafa;font-size:0.8rem;}
#scroller li > a {display:block;}
/** ** 下拉样式 Pull down styles * */
#pullDown{ background:#fff; height:40px;line-height:40px;padding:5px 10px; border-bottom:1px solid #ccc;font-weight:bold;font-size:14px;color:#888;}
#pullUp{background:#fff; height:40px; line-height:40px; padding:5px 10px; font-weight:bold;font-size:14px;color:#888;width: 100%;}
#pullDown .pullDownIcon { display:block; float:left; width:30px; height:30px;background:url(css/[email protected]) 0 0 no-repeat;-webkit-background-size:30px 60px; background-size:30px 60px;-webkit-transition-property:-webkit-transform;-webkit-transition-duration:250ms; }
#pullUp .pullUpIcon { display:block; float:left; width:30px; height:30px;background:url(css/[email protected]) 0 0 no-repeat; -webkit-background-size:30px 60px; background-size:30px 60px;background-position:0 100%; -webkit-transition-property:-webkit-transform;-webkit-transition-duration:250ms; }
#pullDown .pullDownIcon { -webkit-transform:rotate(0deg) translateZ(0); }
#pullUp .pullUpIcon { -webkit-transform:rotate(-180deg) translateZ(0); }
/** * 动画效果css3代码 */
#pullDown.flip .pullDownIcon { -webkit-transform:rotate(-180deg) translateZ(0); }
#pullUp.flip .pullUpIcon { -webkit-transform:rotate(0deg) translateZ(0); }
#pullDown.loading .pullDownIcon, #pullUp.loading .pullUpIcon { background-position:0 100%; -webkit-transform:rotate(0deg) translateZ(0);-webkit-transition-duration:0ms;-webkit-animation-name:loading;-webkit-animation-duration:2s;-webkit-animation-iteration-count:infinite; -webkit-animation-timing-function:linear;}
@-webkit-keyframes loading {
from { -webkit-transform:rotate(0deg) translateZ(0); }
to { -webkit-transform:rotate(360deg) translateZ(0); }
}
</style>
<title>iscroll 下拉刷新,上拉加载</title>
</head>
<body id="receivableSheet">
<div id="wrapper">
<div id="scroller">
<div id="pullDown">
<span class="pullDownIcon"></span><span class="pullDownLabel">下拉刷新...</span>
</div>
<ul class="sheet" id="thelist">
<li class="title">
<span class="name">姓名</span>
<span class="receivable">待回款</span>
<span class="receivableed">已回款</span>
</li>
<li class="list">
<span class="name">李大国</span>
<span class="receivable">2000000.00元</span>
<span class="receivableed">4000000.00元</span>
</li>
</ul>
<div id="pullUp">
<span class="pullUpIcon"></span><span class="pullUpLabel">上拉加载更多</span>
</div>
</div>
</div>
<div class="noDate" style="display: none;" >暂无数据</div>
<script src="js/iscroll.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var myScroll,
pullDownEl, pullDownOffset,
pullUpEl, pullUpOffset,liNum,
generatedCount = 0;
console.log("高度为"+$("#wrapper").height())
liNum=parseInt($("#wrapper").height()/40);//每条li的高度为40,一页面显示的li的个数
console.log("列表的数目"+liNum)
listLoad();
function listLoad () {
setTimeout(function () { // 数据加载后启动定时器,刷新数据
for (i=0; i<liNum-1; i++) {
$("#thelist").append($($(".list")[0]).clone())//首页显示的数据,不用管
}
myScroll.refresh(); //数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}, 100); // <-- Simulate network congestion, remove setTimeout from production!
}
/**
* 下拉刷新 (自定义实现此方法)
* myScroll.refresh(); // 数据加载完成后,调用界面更新方法
*/
function pullDownAction () {
setTimeout(function () { // <-- Simulate network congestion, remove setTimeout from production!
var el, li, i;
el = document.getElementById(‘thelist‘);
myScroll.refresh(); //数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
/**
* 滚动翻页 (自定义实现此方法)
* myScroll.refresh(); // 数据加载完成后,调用界面更新方法
*/
function pullUpAction () {
setTimeout(function () { // <-- Simulate network congestion, remove setTimeout from production!
var el, li, i;
el = document.getElementById(‘thelist‘);
for (i=0; i<5; i++) {
li = document.createElement(‘li‘);
li.innerText = ‘添加三冰 ‘ + (++generatedCount);
el.appendChild(li, el.childNodes[0]);//上拉追加的数据,拉到页面最底进行加载数据
}
myScroll.refresh(); // 数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
/**
* 初始化iScroll控件
*/
function loaded() {
pullDownEl = document.getElementById(‘pullDown‘);
pullDownOffset = pullDownEl.offsetHeight;
pullUpEl = document.getElementById(‘pullUp‘);
pullUpOffset = pullUpEl.offsetHeight;
myScroll = new iScroll(‘wrapper‘, {
scrollbarClass: ‘myScrollbar‘, /* 重要样式 */
useTransition: false,
topOffset: pullDownOffset,
onRefresh: function () {
if (pullDownEl.className.match(‘loading‘)) {
pullDownEl.className = ‘‘;
pullDownEl.querySelector(‘.pullDownLabel‘).innerHTML = ‘下拉刷新...‘;
}
},
//滚动时
onScrollMove: function () {
pullUpEl.className = ‘‘;
console.log("滚动时"+this.y)
if (this.y > 5 && !pullDownEl.className.match(‘flip‘)) {//主要是判断下拉状态,给下拉加Class“flip”,下拉幅度达到显示pullDownOffset的高度,显示内容为松手开始更新..
pullDownEl.className = ‘flip‘;
pullDownEl.querySelector(‘.pullDownLabel‘).innerHTML = ‘松手开始更新...‘;
this.minScrollY = 0;//显示信息
console.log(this.y+"下拉事件")
} else if (this.y < 5 && pullDownEl.className.match(‘flip‘)) {//主要是判断下拉状态,给下拉加Class“flip”,下拉幅度未达到显示pullDownOffset的高度,显示内容为下拉刷新...
pullDownEl.className = ‘‘;
pullDownEl.querySelector(‘.pullDownLabel‘).innerHTML = ‘下拉刷新...‘;
this.minScrollY = -pullDownOffset;//需要下拉才能看见提示信息
} else{ //除了下拉状态,我(暂时)判定为都是上拉状态,满足条件后在进行加载,
// 上拉了
pullUpEl.className = ‘flip‘;
}
},
//离开滚动
onScrollEnd: function () {
if (pullDownEl.className.match(‘flip‘)) {//判断下拉的class名,判定下拉状态,进行通过加Calss名 loading 进行c3动画效果,执行下拉更新
pullDownEl.className = ‘loading‘;
pullDownEl.querySelector(‘.pullDownLabel‘).innerHTML = ‘加载中...‘;
pullDownAction(); // Execute custom function (ajax call?)
} else if (pullUpEl.className.match(‘flip‘)) {//判定为上拉状态,在进行判断,执行
console.log("满足条件了")
console.log(window.screen.height)
console.log($("#pullUp").offset().top)//获得显示上拉框的位移高度
if($("#scroller li").length>=22){//因为我的数据自己加载不确定,我给了个值,根据个人情况进行修改,不进行上拉加载
this.maxScrollY = this.maxScrollY;//表示下拉框的信息显示在页面,
pullUpEl.className = ‘‘;//消除上拉状态的判定条件,及C3动效
$("#pullUp .pullUpIcon").hide()//隐藏上拉状态信息图片
pullUpEl.querySelector(‘.pullUpLabel‘).innerHTML = ‘已经到底了!‘;
}else if((window.screen.height-20)>$("#pullUp").offset().top){//因为我的数据做了margin值,所以减了20的值,上滑如果$("#pullUp")显示,进行加载
if($("#scroller li").length>=22){//进行双重保险
his.maxScrollY = this.maxScrollY;//显示在页面
pullUpEl.className = ‘‘;
$("#pullUp .pullUpIcon").hide()
pullUpEl.querySelector(‘.pullUpLabel‘).innerHTML = ‘已经到底了!‘;
}
else{
pullUpEl.querySelector(‘.pullUpLabel‘).innerHTML = ‘加载中...‘;
pullUpEl.className = ‘loading‘;
pullUpAction();
}
}
}
}
});
setTimeout(function () { document.getElementById(‘wrapper‘).style.left = "0.8rem"; }, 800);
}
//初始化绑定iScroll控件
document.addEventListener(‘touchmove‘, function (e) { e.preventDefault(); }, false);
document.addEventListener(‘DOMContentLoaded‘, loaded, false);
</script>
</body>
</html>
一些细节自己研究的,感觉有些难点,所以记录一下,如有异议,欢迎大伙一起交流,QQ664726168,备注:H5
以上是关于iscroll 下拉刷新,上拉加载的主要内容,如果未能解决你的问题,请参考以下文章