//定义日期 Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } //上下刷新 function pullData(wapper, loadDown, loadUp) { wapper.offset = 100; wapper.text = ["下拉查看最新", "上拉加载更多", "释放刷新", "数据加载中", "成功加载0条数据", "已经是最新数据", "已经到达页面底部"]; wapper.down = wapper.querySelector(".pullDown"); wapper.up = wapper.querySelector(".pullUp"); wapper.list = wapper.querySelector(".pullList"); wapper.downText = wapper.querySelector(".pullDown>div>p"); wapper.downTime = wapper.querySelector(".pullDown>div>span"); wapper.upText = wapper.querySelector(".pullUp>div>p"); wapper.downText.innerHTML = wapper.text[0]; wapper.upText.innerHTML = wapper.text[1]; wapper.timer = null; wapper.loadUp = loadUp; wapper.loadDown = loadDown; wapper.addEventListener("touchstart", function (e) { var target = e.currentTarget; target.startX = e.targetTouches[0].pageX; target.startY = e.targetTouches[0].pageY; target.startScrollTop = target.scrollTop; }); wapper.addEventListener("touchend", function (e) { var target = e.currentTarget; if (this.downText.innerHTML == this.text[2]) { this.down.className = "pullDown loading transition"; this.downText.innerHTML = this.text[3]; this.down.style.height = ""; this.down.style.paddingTop = ""; this.loadDown.apply(this); } else { this.down.style.height = ""; this.down.style.paddingTop = ""; this.down.className = "pullDown transition"; } }); wapper.addEventListener("touchmove", function (e) { var target = e.currentTarget; X = e.targetTouches[0].pageX - target.startX, Y = e.targetTouches[0].pageY - target.startY; //垂直上=>下 if (target.startScrollTop == 0 && Math.abs(Y) > 2 * Math.abs(X) && Y > 0) { e.preventDefault(); if (Y - target.startScrollTop > this.offset) { this.downText.innerHTML = this.text[2]; this.down.className = "pullDown release transition"; } else { this.downText.innerHTML = this.text[0]; this.down.className = "pullDown ready"; this.down.style.height = Y - target.startScrollTop + "px"; this.down.style.paddingTop = (Y - target.startScrollTop - 60) / 2 + "px"; } } }); wapper.addEventListener("scroll", function (e) { if (this.timer != null) { clearTimeout(this.timer); this.timer = null; } wapper.timer = window.setTimeout(this.checkEnd, 100, this); }); wapper.PullDown = function (html) { if (html != "") { var tmp = document.createElement("ul"); tmp.innerHTML = html; var el = tmp.childNodes; var newCount = el.length; var last = null; if (this.list.childNodes.length == 0) { last = this.list.appendChild(el[el.length - 1]); } else { last = this.list.insertBefore(el[el.length - 1], this.list.childNodes[0]); } while (el.length > 0) this.list.insertBefore(el[el.length - 1], last); this.downText.innerHTML = this.text[4].replace(/[0-9]+/, newCount); } else { this.downText.innerHTML = this.text[5]; } this.downTime.innerHTML = new Date().Format("最后更新:M月d日 hh:mm"); this.down.className = "pullDown loaded transition"; if (this.timer != null) { clearTimeout(this.timer); this.timer = null; } var me = this; this.timer = window.setTimeout(function () { me.down.style.height = ""; me.down.style.paddingTop = ""; me.down.className = "pullDown transition"; }, 1000); } wapper.PullUp = function (html) { if (html.length != "") { var tmp = document.createElement("ul"); tmp.innerHTML = html; var el = tmp.childNodes; var newCount = el.length; while (el.length > 0) this.list.appendChild(el[0]); this.upText.innerHTML = this.text[4].replace(/[0-9]+/, newCount); } else { this.upText.innerHTML = this.text[6]; } this.up.className = "pullUp loaded"; if (this.timer != null) { clearTimeout(this.timer); this.timer = null; } } wapper.checkEnd = function (target) { if (target.scrollTop == (target.scrollHeight - target.clientHeight)) { target.upText.innerHTML = target.text[3]; target.up.className = "pullUp loading"; target.loadUp.apply(target); } } }