JQuery plug in iScroll to achieve drop down refresh scroll page effects
- 2020-03-30 03:23:57
- OfStack
JQuery plugin: iScroll
Page layout:
<div id="wrapper">
<div id="scroller">
<div id="pullDown">
<span class="pullDownIcon"></span><span class="pullDownLabel"> The drop-down refresh ...</span>
</div>
<ul id="thelist">
<li>
<img src="img/page1_img1.jpg" />
</li>
<li>
<img src="img/page1_img2.jpg" />
</li>
<li>
<img src="img/page1_img3.jpg" />
</li>
<li>
<img src="img/page1_img1.jpg" />
</li>
<li>
<img src="img/page1_img2.jpg" />
</li>
<li>
<img src="img/page1_img3.jpg" />
</li>
</ul>
<div id="pullUp">
<span class="pullUpIcon"></span><span class="pullUpLabel"> Pull up loads more ...</span>
</div>
</div>
Page-turning is an ajax request that passes the page number to the general handler, where the paged data is returned to the json array object.
Pull-down refresh:
/**
* The drop-down refresh (custom implementation of this method)
* myScroll.refresh(); //When the data is loaded, the interface update method is called
*/
function pullDownAction() {
setTimeout(function () {
var el, li, i;
el = document.getElementById('thelist');
//==========================================
$.ajax({
type: "GET",
url: "LoadMore.ashx",
data: { page: generatedCount },
dataType: "json",
success: function (data) {
var json = data;
$(json).each(function () {
li = document.createElement('li');
// li.innerText = 'Generated row ' + (++generatedCount);
li.innerHTML = '<img src="' + this.src + '"/>';
el.insertBefore(li, el.childNodes[0]);
})
}
});
myScroll.refresh(); //After the data is loaded, call the interface update method Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
The pull to refresh
function pullUpAction() {
setTimeout(function () {
var el, li, i;
el = document.getElementById('thelist');
//==========================================
$.ajax({
type: "GET",
url: "LoadMore.ashx",
data: { page: generatedCount },
dataType: "json",
success: function (data) {
var json = data;
$(json).each(function () {
li = document.createElement('li');
// li.innerText = 'Generated row ' + (++generatedCount);
li.innerHTML = '<img src="' + this.src + '"/>;
el.insertBefore(li, el.childNodes[0]);
})
}
});
//============================================
myScroll.refresh(); //After the data is loaded, call the interface update method Remember to refresh when contents are loaded (ie: on ajax completion)
}, 1000); // <-- Simulate network congestion, remove setTimeout from production!
}
Initialize the
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 = ' The drop-down refresh ...';
} else if (pullUpEl.className.match('loading')) {
pullUpEl.className = '';
pullUpEl.querySelector('.pullUpLabel').innerHTML = ' Pull up loads more ...';
}
},
onScrollMove: function () {
if (this.y > 5 && !pullDownEl.className.match('flip')) {
pullDownEl.className = 'flip';
pullDownEl.querySelector('.pullDownLabel').innerHTML = ' Let go of the update ...';
this.minScrollY = 0;
} else if (this.y < 5 && pullDownEl.className.match('flip')) {
pullDownEl.className = '';
pullDownEl.querySelector('.pullDownLabel').innerHTML = ' The drop-down refresh ...';
this.minScrollY = -pullDownOffset;
} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
pullUpEl.className = 'flip';
pullUpEl.querySelector('.pullUpLabel').innerHTML = ' Let go of the update ...';
this.maxScrollY = this.maxScrollY;
} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
pullUpEl.className = '';
pullUpEl.querySelector('.pullUpLabel').innerHTML = ' Pull up loads more ...';
this.maxScrollY = pullUpOffset;
}
},
onScrollEnd: function () {
if (pullDownEl.className.match('flip')) {
pullDownEl.className = 'loading';
pullDownEl.querySelector('.pullDownLabel').innerHTML = ' In the load ...';
pullDownAction(); // Execute custom function (ajax call?)
} else if (pullUpEl.className.match('flip')) {
pullUpEl.className = 'loading';
pullUpEl.querySelector('.pullUpLabel').innerHTML = ' In the load ...';
pullUpAction(); // Execute custom function (ajax call?)
}
}
});
setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}
//Initializes the bound iScroll control
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);