• jQuery+Asp.net 实现简单的下拉加载更多功能


    原来做过的商城项目现在需要增加下拉加载的功能,简单的实现了一下。大概可以整理一下思路跟代码。

    把需要下拉加载的内容进行转为JSON处理存在当前页面:

    <script type="text/javascript">var objson = eval([{"Id":"5991","product_name":"正品璐瑶颈肩按摩器  披肩颈椎按摩","order_by":"98569","is_show":"True","price":"428","sale_price":"399","Images":"saxjua16.jpg"},{"Id":"5990","product_name":"多功能绞肉机  家用料理机 电动搅拌机","order_by":"98568","is_show":"True","price":"429","sale_price":"399","Images":"d2yjivhl.jpg"},....更多省略]</script>

    下面就是javascript代码的事情了

    <script type="text/javascript">
            $(document).ready(function () {
                var totalheight = 0;
                function loadData() {
                    totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
                    if ($(document).height() <= totalheight) {
                        GetProducts();//加载数据
                    }
                }
                $(document).scroll(function () {
                    loadData();
                });
    
                function GetProducts() {
                    var htmlSource;
                    var iIndex = 6; //每次加载元素数量
                    var icount = parseInt($("#hidJsonCount").val()); //已加载数量 作为for下标值
                    var obj = objson;
                    if (obj.length - icount < 6) {
                        iIndex = obj.length - icount;
                    }
                    if (icount < obj.length) {
                        for (var i = icount; i < (icount+iIndex); i++) {
                            var val = obj[i];
                            htmlSource += '<div class="deal">';
                            htmlSource += '<a href="p_show.aspx?productId=' + val.Id + '" target="_blank">';
                            htmlSource += '<img src="http://www.baidu.com/upload-file/images/product/' + val.Images + '" alt="" border="0" title="' + val.product_name + '"></a>';
                            htmlSource += '<h3>';
                            htmlSource += '<a href="p_show.aspx?productId=' + val.Id + '" title="' + val.product_name + '" target="_blank">' + val.product_name + '</a>';
                            htmlSource += '</h3>';
                            htmlSource += '<h4>';
                            htmlSource += '<em><b>¥</b>' + val.sale_price + '</em><a href="p_show.aspx?productId=' + val.Id + '" target="_blank">查看</a>';
                            htmlSource += '</h4>';
                            htmlSource += '<span class="newicon"></span>';
                            htmlSource += '</div>';
                        }
                        $("#hidJsonCount").val((icount + iIndex).toString());
                    }
                    $("#moreProduct").append(htmlSource);
                }
            });
        </script>
    $("#hidJsonCount").val();此处为一个input hidden的隐藏标签 用来存储已经加载了多少数据。

    至此功能完结。
  • 相关阅读:
    POJ 2559 Largest Rectangle in a Histogram(单调栈)
    POJ 1631 Bridging signals(最长上升子序列LIS)
    POJ 3977 Subset(二分+折半枚举)
    POJ 1742 Coins(dp多重背包)
    【洛谷P2622】关灯问题II【BFS】【状压】
    【洛谷P2622】关灯问题II【BFS】【状压】
    【洛谷P4281】紧急集合 / 聚会【LCA】
    【洛谷P4281】紧急集合 / 聚会【LCA】
    【洛谷P2420】让我们异或吧【DFS】
    【洛谷P2420】让我们异或吧【DFS】
  • 原文地址:https://www.cnblogs.com/pandait/p/3662082.html
Copyright © 2020-2023  润新知