下拉刷新和上拉加载 php,基于iscroll.js实现下拉刷新和上拉加载效果

现在已经不是纯Android独霸天下的时代了,H5嵌入Android的Hybrid混合开发是大势所趋。今天给大家带来的就是移动端中常见的“上拉刷新,下拉加载”特效,这个特效将会基于H5来实现。

先看下运行效果:

abe9d7a9e2e62c7da0e74e5672e3f5a2.gif

是不是有点小小的‘鸡冻' ,它就是由我们今天要介绍的主人公‘iscroll.js'实现的,接下来我以最最简便的方式教给大家~~

实现步骤

一、准备好iscroll.js库

二、搭建页面结构

iScroll 实例:下拉刷新,滚动翻页

body,ul,li {

padding:0;

margin:0;

border:0;

}

body {

font-size:12px;

-webkit-user-select:none;

-webkit-text-size-adjust:none;

font-family:helvetica;

}

#header {

position:absolute;

top:0; left:0;

width:100%;

height:45px;

line-height:45px;

background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));

background-image:-moz-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);

background-image:-o-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);

padding:0;

color:#eee;

font-size:20px;

text-align:center;

}

#header a {

color:#f3f3f3;

text-decoration:none;

font-weight:bold;

text-shadow:0 -1px 0 rgba(0,0,0,0.5);

}

#footer {

position:absolute;

bottom:0; left:0;

width:100%;

height:48px;

background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));

background-image:-moz-linear-gradient(top, #999, #666 2%, #222);

background-image:-o-linear-gradient(top, #999, #666 2%, #222);

padding:0;

border-top:1px solid #444;

}

#wrapper {

position:absolute; z-index:1;

top:45px; bottom:48px; left:0;

width:100%;

background:#555;

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:14px;

}

#scroller li > a {

display:block;

}

/**

*

* 下拉样式 Pull down styles

*

*/

#pullDown, #pullUp {

background:#fff;

height:40px;

line-height:40px;

padding:5px 10px;

border-bottom:1px solid #ccc;

font-weight:bold;

font-size:14px;

color:#888;

}

#pullDown .pullDownIcon, #pullUp .pullUpIcon {

display:block; float:left;

width:40px; height:40px;

background:url(pull-icon@2x.png) 0 0 no-repeat;

-webkit-background-size:40px 80px; background-size:40px 80px;

-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);

}

下拉刷新...

  • 我是三冰 1
  • 我是三冰 2
  • 我是三冰 3
  • 我是三冰 4
  • 我是三冰 5
  • 我是三冰 6
  • 我是三冰 7
  • 我是三冰 8
  • 我是三冰 9
  • 我是三冰 10
  • 我是三冰 11
  • 我是三冰 12
  • 我是三冰 13

上拉加载更多...

代码非常简单,无需多言,仅仅搭建一个静态结构而已~~

效果如下:

2ea888350485fbd7b388c0328f5ea878.png

对照这个效果图看上面代码简直太easy,暂时与iscroll没有半毛钱关系,就是纯静态页面,此时你们唯一没有就是下面这个小图标,不用着急,文章最后会给到你的~~

8ba9ff07879ca9bdaf49639c26a20cbb.png

二、完整Js代码

var myScroll,

pullDownEl, pullDownOffset,

pullUpEl, pullUpOffset,

generatedCount = 0;

/**

* 下拉刷新 (自定义实现此方法)

* myScroll.refresh(); // 数据加载完成后,调用界面更新方法

*/

function pullDownAction () {

setTimeout(function () { //

var el, li, i;

el = document.getElementById('thelist');

for (i=0; i<3; i++) {

li = document.createElement('li');

li.innerText = '添加三冰 ' + (++generatedCount);

el.insertBefore(li, el.childNodes[0]);

}

myScroll.refresh(); //数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)

}, 1000); //

}

/**

* 滚动翻页 (自定义实现此方法)

* myScroll.refresh(); // 数据加载完成后,调用界面更新方法

*/

function pullUpAction () {

setTimeout(function () { //

var el, li, i;

el = document.getElementById('thelist');

for (i=0; i<3; 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); //

}

/**

* 初始化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, /* 此属性不知用意,本人从true改为false */

topOffset: pullDownOffset,

onRefresh: function () {

if (pullDownEl.className.match('loading')) {

pullDownEl.className = '';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';

} else if (pullUpEl.className.match('loading')) {

pullUpEl.className = '';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';

}

},

onScrollMove: function () {

if (this.y > 5 && !pullDownEl.className.match('flip')) {

pullDownEl.className = 'flip';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';

this.minScrollY = 0;

} else if (this.y < 5 && pullDownEl.className.match('flip')) {

pullDownEl.className = '';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';

this.minScrollY = -pullDownOffset;

} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {

pullUpEl.className = 'flip';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';

this.maxScrollY = this.maxScrollY;

} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {

pullUpEl.className = '';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';

this.maxScrollY = pullUpOffset;

}

},

onScrollEnd: function () {

if (pullDownEl.className.match('flip')) {

pullDownEl.className = 'loading';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';

pullDownAction(); // Execute custom function (ajax call?)

} else if (pullUpEl.className.match('flip')) {

pullUpEl.className = 'loading';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';

pullUpAction(); // Execute custom function (ajax call?)

}

}

});

setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);

}

//初始化绑定iScroll控件

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', loaded, false);

这么多代码看啊,不用着急,挑几个重点给你说说:

1)onRefresh函数是指页面刷新或调用refresh()函数会触发此函数,里面代码中主要做【一些重置样式和文字】的处理。

2)onScrollMove函数是指拖拽页面,不松手的时候会触发此函数,里面代码中主要做【箭头有个旋转效果和松手提示】的处理。

3)onScrollEnd函数是指松开手,停止拖拽的时候会触发的函数,里面代码中主要做【刷新数据和一些加载动画效果】的处理。

4)topOffset属性主要是可以隐藏一个高度,正好把下拉刷新给隐藏掉

5)函数pullDownAction和pullUpAction中,我是自己用createElement函数造数据,但是真实开发中,这里可以换成Ajax请求服务器数据即可

看看现在的效果如何:

9bc97ddac6d8c6881020e70632a67b25.gif

现在数据什么的都能刷新了,只差那么一点点,如果下拉和上拉的时候,加载的小图标有个动画效果那就超级“完美”了:

既然是要来点动画效果,肯定是用最新的CSS3技术呀,废话不多说,直接贴完整代码了:

iScroll 实例:下拉刷新,滚动翻页

body,ul,li {

padding:0;

margin:0;

border:0;

}

body {

font-size:12px;

-webkit-user-select:none;

-webkit-text-size-adjust:none;

font-family:helvetica;

}

#header {

position:absolute;

top:0; left:0;

width:100%;

height:45px;

line-height:45px;

background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));

background-image:-moz-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);

background-image:-o-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);

padding:0;

color:#eee;

font-size:20px;

text-align:center;

}

#header a {

color:#f3f3f3;

text-decoration:none;

font-weight:bold;

text-shadow:0 -1px 0 rgba(0,0,0,0.5);

}

#footer {

position:absolute;

bottom:0; left:0;

width:100%;

height:48px;

background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));

background-image:-moz-linear-gradient(top, #999, #666 2%, #222);

background-image:-o-linear-gradient(top, #999, #666 2%, #222);

padding:0;

border-top:1px solid #444;

}

#wrapper {

position:absolute; z-index:1;

top:45px; bottom:48px; left:0;

width:100%;

background:#555;

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:14px;

}

#scroller li > a {

display:block;

}

/**

*

* 下拉样式 Pull down styles

*

*/

#pullDown, #pullUp {

background:#fff;

height:40px;

line-height:40px;

padding:5px 10px;

border-bottom:1px solid #ccc;

font-weight:bold;

font-size:14px;

color:#888;

}

#pullDown .pullDownIcon, #pullUp .pullUpIcon {

display:block; float:left;

width:40px; height:40px;

background:url(pull-icon@2x.png) 0 0 no-repeat;

-webkit-background-size:40px 80px; background-size:40px 80px;

-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); }

}

下拉刷新...

  • 我是三冰 1
  • 我是三冰 2
  • 我是三冰 3
  • 我是三冰 4
  • 我是三冰 5
  • 我是三冰 6
  • 我是三冰 7
  • 我是三冰 8
  • 我是三冰 9
  • 我是三冰 10
  • 我是三冰 11
  • 我是三冰 12
  • 我是三冰 13

上拉加载更多...

var myScroll,

pullDownEl, pullDownOffset,

pullUpEl, pullUpOffset,

generatedCount = 0;

/**

* 下拉刷新 (自定义实现此方法)

* myScroll.refresh(); // 数据加载完成后,调用界面更新方法

*/

function pullDownAction () {

setTimeout(function () { //

var el, li, i;

el = document.getElementById('thelist');

for (i=0; i<3; i++) {

li = document.createElement('li');

li.innerText = '添加三冰 ' + (++generatedCount);

el.insertBefore(li, el.childNodes[0]);

}

myScroll.refresh(); //数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)

}, 1000); //

}

/**

* 滚动翻页 (自定义实现此方法)

* myScroll.refresh(); // 数据加载完成后,调用界面更新方法

*/

function pullUpAction () {

setTimeout(function () { //

var el, li, i;

el = document.getElementById('thelist');

for (i=0; i<3; 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); //

}

/**

* 初始化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, /* 此属性不知用意,本人从true改为false */

topOffset: pullDownOffset,

onRefresh: function () {

if (pullDownEl.className.match('loading')) {

pullDownEl.className = '';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';

} else if (pullUpEl.className.match('loading')) {

pullUpEl.className = '';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';

}

},

onScrollMove: function () {

if (this.y > 5 && !pullDownEl.className.match('flip')) {

pullDownEl.className = 'flip';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';

this.minScrollY = 0;

} else if (this.y < 5 && pullDownEl.className.match('flip')) {

pullDownEl.className = '';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';

this.minScrollY = -pullDownOffset;

} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {

pullUpEl.className = 'flip';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';

this.maxScrollY = this.maxScrollY;

} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {

pullUpEl.className = '';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';

this.maxScrollY = pullUpOffset;

}

},

onScrollEnd: function () {

if (pullDownEl.className.match('flip')) {

pullDownEl.className = 'loading';

pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';

pullDownAction(); // Execute custom function (ajax call?)

} else if (pullUpEl.className.match('flip')) {

pullUpEl.className = 'loading';

pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';

pullUpAction(); // Execute custom function (ajax call?)

}

}

});

setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);

}

//初始化绑定iScroll控件

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', loaded, false);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值