$(function(){
var index = 0;
function LScroll(opts){
var options = {
id: "",
style: "",
pullUpElId: ".l-pullUp",
pullUpAction: undefined,
pullDownElId: ".l-pullDown",
pullDownAction: undefined,
pullDownMsg1: "下拉刷新...",
pullDownMsg2: "松手开始更新...",
type: 1, // 1:正常 上拉加载 下拉刷新 2:下拉加载
page: 1,
scrollCallBack: undefined,
scrollEndCallBack: undefined,
top: 0,
// IScroll 配置
iscrollOpt: null
}
var g= this;
g.options = $.extend(true, options, opts);
g.iScroll = undefined;
g.noMore = false;
}
LScroll.prototype.refresh = function(noMore){
var g= this,
p = g.options;
if(typeof(noMore) == 'boolean'){
g.noMore = noMore;
}
g.iScroll.refresh();
}
LScroll.prototype.scrollToElement = function(el, time, offsetX, offsetY, easing){
var g= this,
is = g.iScroll;
is.scrollToElement(el, time, offsetX, offsetY, easing);
}
LScroll.prototype.refreshToEnd = function(time){
var g= this,
p = g.options,
is = g.iScroll;
is.refresh();
time = time || 0;
is.scrollTo(0, is.maxScrollY, time);
}
LScroll.prototype.refreshToTop = function(time){
var g= this,
p = g.options,
is = g.iScroll;
is.refresh();
time = time || 0;
is.scrollTo(0, 0, time);
}
LScroll.prototype.scrollToEnd = function(time){
var g= this,
p = g.options,
is = g.iScroll;
time = time || 0;
var wrapperHeight = $(myScroll.iScroll.wrapper).height();
is.scrollTo(0, wrapperHeight-is.scrollerHeight, time);
}
LScroll.prototype.init = function(){
var g= this,
p = g.options;
p.id = p.id || ("wrapper"+ index);
p.style = p.style || "";
if(p.top){
p.style = "top:"+ p.top + "px;"+p.style;
}
p._el.wrap('
')
var _el = $('#'+p.id);
if(p.pullDownAction){
$('#'+p.id+ ' div.l-scroller').prepend('');
}
if(p.pullUpAction){
$('#'+p.id+ ' div.l-scroller').append('');
}
var pullUpEl= _el[0].querySelector(p.pullUpElId),
pullUpOffset,
pullDownEl= _el[0].querySelector(p.pullDownElId),
pullDownOffset;
g.iScroll = new IScroll(_el[0], $.extend({
probeType: 3
},g.options.iscrollOpt));
var myScroll = g.iScroll;
myScroll.on('refresh', function(){
$(pullUpEl).show();
$(pullDownEl).show();
if (pullUpEl) {
if(g.noMore){
pullUpEl.className = 'noMoreData';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '没有更多数据...';
} else{
pullUpEl.className = 'l-pullUp';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
}
} else if(pullDownEl) {
if(g.noMore){
if(p.type == 2){
pullDownEl.className = 'noMoreData';
pullDownEl.querySelector('.pullDownLabel').innerHTML = "没有更多数据...";
} else if(pullUpEl) {
pullUpEl.className = 'noMoreData';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '没有更多数据...';
}
} else{
pullDownEl.className = 'l-pullDown';
pullDownEl.querySelector('.pullDownLabel').innerHTML = p.pullDownMsg1;
if(pullUpEl){
pullUpEl.className = 'l-pullUp';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
}
}
}
});
myScroll.on('scroll', function(){
if (pullDownEl && this.y > 30 && !pullDownEl.className.match('l-flip')) {
if(pullDownEl.className.match('noMoreData'))
return;
pullDownEl.className = 'l-flip';
pullDownEl.querySelector('.pullDownLabel').innerHTML = p.pullDownMsg2 ;
this.minScrollY = pullDownEl.offsetHeight;
} else if (pullDownEl && this.y < 30 && pullDownEl.className.match('l-flip')) {
if(pullDownEl.className.match('noMoreData'))
return;
pullDownEl.className = 'l-pullDown';
pullDownEl.querySelector('.pullDownLabel').innerHTML = p.pullDownMsg1;
// this.minScrollY = pullDownOffset;
} else if (pullUpEl && this.y < (this.maxScrollY - 30) && !pullUpEl.className.match('l-flip')) {
if(pullUpEl.className.match('noMoreData'))
return;
pullUpEl.className = 'l-flip';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始加载...';
this.maxScrollY = this.maxScrollY;
} else if (pullUpEl && this.y > (this.maxScrollY + 30) && pullUpEl.className.match('l-flip')) {
if(pullUpEl.className.match('noMoreData'))
return;
pullUpEl.className = 'l-pullDown';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
this.maxScrollY = pullUpEl.offsetHeight;
}
if(p.scrollCallBack)
p.scrollCallBack(this);
});
myScroll.on('scrollEnd', function(){
if (pullDownEl && pullDownEl.className.match('l-flip')) {
if(pullDownEl.className.match('noMoreData'))
return;
pullDownEl.className = 'l-loading';
pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
if(p.type == 2)
++p.page;
else
p.page = 1;
p.noMore = false;
pullDownEl.className= "l-loading";
if(p.pullDownAction) p.pullDownAction(g);
} else if (pullUpEl && pullUpEl.className.match('l-flip')) {
if(pullUpEl.className.match('noMoreData'))
return;
pullUpEl.className = 'l-loading';
pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
++p.page;
if(p.pullUpAction) p.pullUpAction(g);
}
if(p.scrollEndCallBack)
p.scrollEndCallBack(this);
});
// setTimeout(function () { p._el[0].style.left = '0'; }, 800);
return myScroll;
}
$.fn.lscroll = function (opts) {
index++;
opts = opts || {};
opts._el = $(this);
var o = new LScroll(opts);
var myScroll = o.init();
this.data("LScroll", myScroll);
return o;
};
$.fn.getLScroll = function () {
return this.data("LScroll");
};
document.addEventListener('touchmove', function (e) {
//判别输入框的id
var that = e;
if(that.target.parentNode.parentNode.id && that.target.id != "input_content"){
if(that.target.parentNode.parentNode.id != "history_search"){
that.preventDefault();
}
}else{
if(that.target.id != "input_content"){
that.preventDefault();
}
}
}, false);
})