您好,登錄后才能下訂單哦!
這篇文章主要介紹了CSS3如何實現(xiàn)瀑布流布局與無限加載圖片相冊,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一、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> <div 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> </div> <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> //此方法用來初始化圖片(圖片全部加載完成時調(diào)用) 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>
二、模擬數(shù)據(jù)庫數(shù)據(jù)的實體類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; } //模擬數(shù)據(jù)庫有兩百條數(shù)據(jù) 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; } } }
三、服務器返回數(shù)據(jù)給客戶端的一般處理程序Handler1.ashx代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; namespace 瀑布流布局與無限加載圖片相冊 { /// <summary> /// 服務器返回數(shù)據(jù)給客戶端的一般處理程序 /// </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; } } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“CSS3如何實現(xiàn)瀑布流布局與無限加載圖片相冊”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。