Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Created October 11, 2014 08:02
Show Gist options
  • Save alanerzhao/91393691b00eefdf5a1a to your computer and use it in GitHub Desktop.
Save alanerzhao/91393691b00eefdf5a1a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul id="postswrapper">
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
</ul>
<script src="./jquery.js"></script>
<script type="text/javascript">
(function($){
function LoadNewCont(o) {
var o = o || {};
this.url = "./data.js";
this.data = "";
this.doc_view = $(document);
this.win_view = $(window);
this.up_wrap = $(o.wrapper) || $(document);
this.load_gif = $("<div>load……</div>").hide();
this.init();
this.load();
}
LoadNewCont.prototype = {
init : function () {
this.bind();
},
bind : function () {
var self = this,
// todo 只有内容窗口超过可视区才执行bind
isView = (this.win_view.height() < this.up_wrap.height());
_loadFlog = false;
if(isView) {
this.win_view.bind('scroll',function() {
var _scrollTop = self.win_view.scrollTop(),
_docHeight = self.doc_view.height() - self.win_view.height();
console.log(_scrollTop,_docHeight);
if(_scrollTop == _docHeight) {
self.fetch();
}
})
}
},
load : function () {
this.up_wrap.after(this.load_gif);
},
fetch : function () {
var self = this;
this.load_gif.show();
$.ajax({
url:self.url,
dataType: "json",
success: function (data) {
self.update(data);
},
error: function () {
console.log(error)
}
})
},
update : function (o) {
console.log(o)
var item = $("<li>"+o['1']+"</li>")
var self = this;
if(o.len) {
this.up_wrap.append(item)
} else {
this.up_wrap.append("Not Cont")
//todo unbind
this.win_view.unbind("scroll");
}
setTimeout(function(){
self.load_gif.hide();
},300);
}
}
var load = new LoadNewCont({"wrapper":$("#postswrapper")});
}(jQuery))
</script>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment