一则C#简洁瀑布流代码

 更新时间:2014年06月11日 10:49:36   作者:  
最近想实现数据的延迟加载,网上找一下有很多例子,看了Masonry的例子启发,自己写了一个很简洁的代码。分享给大家

View页面。

复制代码 代码如下:

@{
        ViewBag.Title = "瀑布流";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
@section header{
    <script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
         <style type="text/css">
        .* {
         margin:0px;
         padding:0px;
        }
        body {
            margin-left:auto;
            margin-right:auto;
        }
        .ClearBoth {
            clear:both;
        }
        #bodyContent {
            width:1400px;
            margin-left:auto;
            margin-right:auto;
        }
        #head {
            width:100%;
            height:50px;
            margin-bottom:10px;
        }
        #LefMenue {
            width:20%;
            height:500px;
            float:left;
        }
        #RightContent {
            width:75%;
            float:right;
            border: thin solid #333;
        }
        #Footer {
            margin-top:10px;
            width:100%;
            height:40px;
        }
        .GreyBlock {
            border: thin solid #333;
            background-color:#CCC;
            width:100%;
        }
        .Item {
            float: left;
            width: 230px;
            margin:5px;
            border: 1px solid #CCC;
        }
        </style>
}
 <div id="bodyContent">
   <div id="head" class="GreyBlock">
     <h1>Head</h1>
   </div>
   <div>
           <!--Left-->
       <div id="LefMenue" class="GreyBlock">
         <ul>
           <li>1</li>
           <li>2</li>
           <li>3</li>
         </ul>
       </div>
       <!----right-->
       <div id="RightContent">
             <div id="ProductList">
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
               <div class="Item">
                   <dl>
                       <dt>
                           <img src="~/Content/Shose.jpg" /></dt>
                       <dd>What's up with you</dd>
                    </dl>
               </div>
             </div>
       </div>
     <div class="ClearBoth"></div>
   </div>
<div id="loading" class="loading-wrap">
    <span class="loading">加载中,请稍后...</span>
</div>
   <div class="ClearBoth"></div>
   <div id="Footer" class="GreyBlock"></div>
 </div>

@section scripts{
    <script type="text/javascript">
        var myContainer = $("#ProductList");
        //用户拖动滚动条,达到底部时ajax加载一次数据
        var loading = $("#loading").data("on", false);//通过给loading这个div增加属性on,来判断执行一次ajax请求
        $(window).scroll(function () {
            if ($("#loading").data("on"))//
            {
                return;
            }
            var isButtom = $(document).scrollTop() > ($(document).height() - $(window).height() - $("#Footer").height());
            if (isButtom) {//页面拖到底部了
                //加载更多数据
                loading.data("on",true).fadeIn();
                $.get("@Url.Action("GetData","Product")", function (data) {
                    var html = CreateHtml(data);
                    var $newElems = $(html).css({ opacity: 0 }).appendTo(myContainer);
                    $newElems.animate({ opacity: 1 },3000);
                    loading.data("on", false);
                    loading.fadeOut();
                },"json");
            }
        });
        function CreateHtml(data) {
            var html = "";
            if ($.isArray(data)) {
                for (var i in data) {
                    //html += "<div class=\"Item\" style=\"height:"+data[i]+"px\">";
                    html += "<div class=\"Item\">";
                    html += "<dl>";
                    html += "<dt>";
                    html += "<img src=\"../Content/Shose.jpg\" />";
                    html += "</dt>";
                    html += "<dd>";
                    html += "What's up with you " + data[i];
                    html += "</dd>"
                    html += "</dl>"
                    html += "</div>"
                }
            }
            return html;
        }
    </script>
    }

C#服务端:

复制代码 代码如下:

public JsonResult GetData()
        {
            Random ro = new Random();

            List<int> vListInt = new List<int>();
            for (int i = 0; i < 12; i++)
            {
                vListInt.Add(ro.Next(400, 500));
            }
            return Json(vListInt, JsonRequestBehavior.AllowGet);
        }

相关文章

  • C#8 的模式匹配实现

    C#8 的模式匹配实现

    这篇文章主要介绍了C#8 的模式匹配实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • c# 如何用组合替代继承

    c# 如何用组合替代继承

    这篇文章主要介绍了c# 如何用组合替代继承,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • C#编译器对局部变量的优化指南

    C#编译器对局部变量的优化指南

    这篇文章主要给大家介绍了关于C#编译器对局部变量的优化指南,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • C#中的==运算符

    C#中的==运算符

    这篇文章主要介绍了C#中的==运算符,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-06-06
  • C#中的协变与逆变深入讲解

    C#中的协变与逆变深入讲解

    这篇文章主要给大家介绍了关于C#中协变与逆变的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • C#中将字符串转换为整型的三种解决方法总结

    C#中将字符串转换为整型的三种解决方法总结

    本篇文章是对C#中将字符串转换为整型的三种解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 基于C# wpf实现桌面放大镜

    基于C# wpf实现桌面放大镜

    做桌面截屏功能时需要放大镜,显示鼠标所在位置的放大图像,所以本文为大家介绍了如何基于C# wpf实现桌面放大镜功能,有需要的小伙伴可以参考下
    2023-09-09
  • C#表达式目录树示例详解

    C#表达式目录树示例详解

    这篇文章主要给大家介绍了关于C#表达式目录树的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 积累Visual Studio 常用快捷键的动画演示

    积累Visual Studio 常用快捷键的动画演示

    在代码开发过程中,频繁的使用键盘、鼠标操作非常麻烦,影响程序的开发效率。如何操作能用键盘来操作,那就节省时间了。下面小编把我平时积累的有关visul studio 常用快捷键的动画演示分享给大家,仅供大家参考
    2015-10-10
  • c#实现sunday算法实例

    c#实现sunday算法实例

    Sunday算法思想跟BM算法很相似,在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符,下面是用C#实现sunday的实例代码,有需要的朋友可以参考一下
    2013-08-08

最新评论