瀑布流布局與無限載入圖片相簿效果

來源:互聯網
上載者:User
這次給大家帶來瀑布流布局與無限載入圖片相簿效果,實現瀑布流布局與無限載入圖片相簿效果的注意事項有哪些,下面就是實戰案例,一起來看一下。

目錄

一、pic1.html頁面代碼如下:

二、類比資料庫資料的實體類Photoes.cs代碼如下:

三、伺服器返回資料給用戶端的一般處理常式Handler1.ashx代碼如下:

四、樣本下載:

五、瞭解更多瀑布流布局的的知識

首先給大家看一下瀑布流布局與無限載入圖片相簿:

一、pic1.html頁面代碼如下:

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>瀑布流布局與無限載入圖片相簿</title>    <style type="text/css">        * {            margin: 0;            padding: 0;        }        body {            background: url(../img/bg5.jpg);        }        #items {            width: 1060px;            margin: 0 auto;            border: 1px solid lightpink;        }        .item {            border: 1px solid lightpink;            width: 200px;            color: purple;            font-size: 30px;            font-weight: bolder;            margin: 5px;            text-align: center;            opacity: 0.8;        }        img {            width: 200px;        }    </style></head><body>    <p id="items">        <p class="item"><img src="img/1.jpg" />picture-1</p>        <p class="item"><img src="img/2.jpg" />picture-2</p>        <p class="item"><img src="img/3.jpg" />picture-3</p>        <p class="item"><img src="img/4.jpg" />picture-4</p>        <p class="item"><img src="img/5.jpg" />picture-5</p>        <p class="item"><img src="img/6.jpg" />picture-6</p>        <p class="item"><img src="img/7.jpg" />picture-7</p>        <p class="item"><img src="img/8.jpg" />picture-8</p>        <p class="item"><img src="img/9.jpg" />picture-9</p>        <p class="item"><img src="img/10.jpg" />picture-10</p>        <p class="item"><img src="img/11.jpg" />picture-11</p>        <p class="item"><img src="img/12.jpg" />picture-12</p>        <p class="item"><img src="img/13.jpg" />picture-13</p>        <p class="item"><img src="img/14.jpg" />picture-14</p>        <p class="item"><img src="img/15.jpg" />picture-15</p>        <p class="item"><img src="img/16.jpg" />picture-16</p>        <p class="item"><img src="img/17.jpg" />picture-17</p>        <p class="item"><img src="img/18.jpg" />picture-18</p>        <p class="item"><img src="img/19.jpg" />picture-19</p>        <p class="item"><img src="img/20.jpg" />picture-20</p>    </p>    <a href="Handler1.ashx" id="next">下一頁</a>    <script src="js/jquery-3.1.1.js" type="text/javascript" charset="utf-8"></script>    <!--外掛程式的引用-->    <script src="js/masonry.pkgd.min.js" type="text/javascript"></script>    <script src="js/imagesloaded.pkgd.min.js" type="text/javascript" charset="utf-8"></script>    <script src="js/jquery.infinitescroll.min.js"></script>    <script>        //此方法用來初始化圖片(圖片全部載入完成時調用)        var init = function () {            imagesLoaded(document.querySelector('#items'), function (instance) {                //此方法用來設定瀑布流布局                var msnry = new Masonry("#items", {                    itemSelector: ".item",                    columnWidth: 0 //列與列之間的寬度                });                //alert('所有的圖片都載入完成了');            });        }        init();        var num = 0;        //此方法是無限載入的方法        $("#items").infinitescroll({            navSelector: "#next",            nextSelector: "a#next",            itemSelector: ".item",            debug: true,            dataType: "json",            maxPage: 10,            appendCallback: false,            path: function (index) {                console.log(index);                return "Handler1.ashx?page=" + index;            }        }, function (data) {            num -= 20;            for (var i = 0; i < data.length; i++) {                $("<p class='item'><img src='img/" + (data[i].imgUrl + num) + ".jpg' />" + data[i].Name + "</p>").appendTo("#items")                console.log(data[i].imgUrl + "--" + data[i].Name);            }            init();        });    </script></body></html>

二、類比資料庫資料的實體類Photoes.cs代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace 瀑布流布局與無限載入圖片相簿{    public class Photoes    {        public int imgUrl { get; set; }        public string Name { get; set; }        //類比資料庫有兩百條資料        public static List<Photoes> GetData()        {            List<Photoes> list = new List<Photoes>();            Photoes pic = null;            for (int i= 21; i <=200; i++)            {                pic = new Photoes();                pic.imgUrl = i;                pic.Name = "Picture-" + i;                list.Add(pic);            }            return list;        }    }}

三、伺服器返回資料給用戶端的一般處理常式Handler1.ashx代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Script.Serialization;namespace 瀑布流布局與無限載入圖片相簿{    /// <summary>    /// 伺服器返回資料給用戶端的一般處理常式    /// </summary>    public class Handler1 : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            List<Photoes> result = Photoes.GetData();            int pageIndex = Convert.ToInt32(context.Request["page"]);            var filtered = result.Where(p => p.imgUrl >= pageIndex * 20 - 19 && p.imgUrl <= pageIndex * 20).ToList();            JavaScriptSerializer ser = new JavaScriptSerializer();            string jsonData = ser.Serialize(filtered);            context.Response.Write(jsonData);        }        public bool IsReusable        {            get            {                return false;            }        }    }}

總結:前段時間學習了瀑布流布局與圖片載入等知識,做了一個簡單的樣本,希望能鞏固一下自己所學的知識。

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

CSS實現3D按鈕效果

opacity透明度濾鏡的IE相容解決方案

HTML5+CSS3載入進度條與下載進度條實現

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.