function Map() //自定义map { this.container = new Object(); } Map.prototype.put = function(key, value) { this.container[key] = value; }; Map.prototype.get = function(key) { return this.container[key]; }; Map.prototype.keySet = function() { var keyset = new Array(); var count = 0; for (var key in this.container) { // 跳过object的extend函数 if (key == 'extend') { continue; } keyset[count] = key; count++; } return keyset; }; /** * 保存所有刷新器,键是containerId + "_loadBtn" */ pullToRefreshers = new Map(); /** * 自动加载 */ var PullToRefresh = function(containerId, getDataFunc, loadOnInit, manualLoad) //下拉调用 { this.containerId = containerId; //为tag名字 this.getDataFunc = getDataFunc; //获取下一页的函数
this.loadOnInit = loadOnInit; //loadOnInit为true时,自动加载完将触发scroll事件,需要忽略 this.firstLoad = true; /** *正在加载 */ this.loading = false; pullToRefreshers.put(containerId + "_loadBtn", this); this.onScroll = function() { if(pullToRefreshers.get(containerId + "_loadBtn").loadOnInit && pullToRefreshers.get(containerId + "_loadBtn").firstLoad) { pullToRefreshers.get(containerId + "_loadBtn").firstLoad = false; return; } if(!pullToRefreshers.get(containerId + "_loadBtn").loading) { //兼容各浏览器 var clientHeight = document.documentElement.clientHeight; if(clientHeight == 0) { clientHeight += document.body.clientHeight; } var scrollTop = document.documentElement.scrollTop; if(scrollTop == 0) { scrollTop += document.body.scrollTop; } var scrollHeight = document.documentElement.scrollHeight; if(scrollHeight == 0) { scrollHeight += document.body.scrollHeight; } //滚动到底部 if(clientHeight+scrollTop>=scrollHeight) { pullToRefreshers.get(containerId + "_loadBtn").loadNextPage(); } } }; this.loadNextPage = function () { loading = true; var data = getDataFunc(); if(manualLoad) { $("#" + containerId + "_loadBtn").remove(); } $("#" + containerId).append(data); if(manualLoad) { var loadBtn = "<div id='" + containerId + "_loadBtn'" + " class='col-xs-12' style='cursor:pointer; text-align:center;' onclick='pullToRefreshers.get(this.id).loadNextPage()'>加载更多...</div>"; $("#" + containerId).append(loadBtn); } loading = false; }; if(manualLoad) { var loadBtn = "<div id='" + containerId + "_loadBtn'" + " class='col-xs-12' style='cursor:pointer; text-align:center;' onclick='pullToRefreshers.get(this.id).loadNextPage()'>加载更多...</div>"; $("#" + containerId).append(loadBtn); } else { window.onscroll = this.onScroll; } if(loadOnInit) { this.loadNextPage(); } };
goodsList.tag
<%@ tag body-content="empty" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- goodsList是静态限时的商品列表,与nextPageFunc不能同时存在 --%> <%@ attribute name="goodsList" required="false" rtexprvalue="true" type="java.util.List"%> <%-- nextPageFunc是动态加载商品列表时调用的action名称,与goodsList不可同时存在 --%> <%@ attribute name="nextPageFunc" required="false" rtexprvalue="false" type="java.lang.String"%> <%-- loadOnInit为"true"时,打开页面会自动请求一次数据 --%> <%@ attribute name="loadOnInit" required="false" rtexprvalue="false" type="java.lang.String"%> <%-- manualLoad为"true"时,滚动页面不会自动加载数据,需要点击加载更多按钮 --%> <%@ attribute name="manualLoad" required="false" rtexprvalue="false" type="java.lang.String"%> <%-- 生成nextPageFunc所需参数的js方法名 --%> <%@ attribute name="paramFunc" required="false" rtexprvalue="false" type="java.lang.String"%> <script type="text/javascript"> $(function(){ /* document.getElementById("centerButton").style.top=(document.documentElement.scrollTop)+"px"; document.getElementById("centerButton").style.left=(document.documentElement.scrollLeft+document.documentElement.clientWidth-document.getElementById("centerButton").offsetWidth)+"px"; */ if("${nextPageFunc}" != "") { new PullToRefresh("goodsList", nextPage, ${true == loadOnInit}, ${true == manualLoad}); } }); var currentPage = 1; var numPerPage = 10; <c:if test="${not empty paramFunc}"> var customParam = ${paramFunc}(); </c:if> function nextPage() { <c:if test="${not empty paramFunc}"> var param = customParam; </c:if> <c:if test="${empty paramFunc}"> var param = {}; </c:if> param["currentPage"] = currentPage; param["numPerPage"] = numPerPage; var data = eval(invokeJava("${nextPageFunc}", param)); var dom = ""; for(var o in data) { dom += '<div class="col-xs-6" onclick="buy(' + data[o].goodsId + ')" style="cursor:pointer"><ul class="ul2"><img src="' + data[o].picUrl + '"/><br>' +'<li style="color: #b7b7b7; font-size: 12px;">'+data[o].storeName + '</li>' +'<li style="color: #929292; font-size: 14px;">'+ data[o].goodsName + '</li>' + '<li style="color: #ef3646; font-size: 15px;">'+data[o].discount + '</li>' +'<li style="color: #959595; font-size: 12px;">'+data[o].price + '</li>' + '<li><i class="glyphicon glyphicon-shopping-cart"></i><p>购买</p></li>' +'</ul>'+'<br></div>' /* dom += '<div class="col-xs-6" onclick="buy(' + data[o].goodsId + ')" style="cursor:pointer"><img src="' + data[o].picUrl + '"/><br>' + data[o].goodsName + '<br></div>' */ } currentPage++; /* dom + = '<div name="centerButton" id="centerButton"><i class="glyphicon glyphicon-th-large"></i></div>' */ return dom; } function buy(goodsId) { document.location = "/UshequMobile/getGoodsInfo_Goods?goodsId=" + goodsId; } </script> <div id="goodsList"> <c:forEach var="goods" items="${goodsList}" varStatus="status"> <div class="col-xs-6" onclick="buy(${goods.goodsId})" style="cursor:pointer"> <img src="${goods.picUrl}"/><br> ${goods.goodsName}<br> </div> </c:forEach> </div>
页面调用
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@taglib prefix="hs" tagdir="/WEB-INF/tags" %> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html> 5 <html lang="zh-cn"> 6 <head> 7 <title>热销商品</title> 8 <script type="text/javascript"> 9 $(function() 10 { 11 $("#flag").val( "${flag}"); 12 }); 13 14 function getParam() 15 { 16 var data={}; 17 data["flag"] = "${flag}"; 18 19 return data; 20 } 21 22 </script> 23 </head> 24 <body> 25 <div class="container"> 26 <div class="row"> 27 <div class="col-xs-12" id="refreshContainer"> 29 <hs:goodsList nextPageFunc="getHotSellGoodsAjax" paramFunc="getParam" loadOnInit="true" manualLoad="false"/> 30 </div> 31 32 </div> 33 </div> 34 </body> 35 </html> 36