瀑布流載入圖片
阿新 • • 發佈:2018-11-03
這個效果大家見過很多了 在百度搜索圖片的時候 下面補上我學習的製作這個的過程 當然 圖片是從別的地方抓取的 或者在資料庫中讀取的 我這裡只是放在了一個json裡面 作為表示。廢話不多說 上程式碼
css部分
<title>瀑布流效果</title>
<style type="text/css">
*{margin:0;padding:0;}
body{background:#f3f2f3;}
.pic{width:1000px;margin:0 auto;}
.pic .list{width:248px;text-align :center;float:left;}
</style>
<link type="text/css" rel="stylesheet" href="css/animate.min.css">
HTML部分
<body onload="initLoad();">
<div class="pic">
<div class="list"></div>
<div class="list"></div>
<div class="list" ></div>
<div class="list"></div>
</div>
jq部分
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var json = [
{"src":"images/1.jpg","alt":"豪車","height":"340"},
{"src" :"images/2.jpg","alt":"豪車","height":"310"},
{"src":"images/3.jpg","alt":"豪車","height":"360"},
{"src":"images/4.jpg","alt":"豪車","height":"340"},
{"src":"images/5.jpg","alt":"豪車","height":"330"},
{"src":"images/6.jpg","alt":"豪車","height":"340"},
{"src":"images/7.jpg","alt":"豪車","height":"360"},
{"src":"images/8.jpg","alt":"豪車","height":"340"},
{"src":"images/9.jpg","alt":"豪車","height":"360"},
{"src":"images/10.jpg","alt":"豪車","height":"340"},
{"src":"images/11.jpg","alt":"豪車","height":"340"},
{"src":"images/12.jpg","alt":"豪車","height":"345"},
{"src":"images/13.jpg","alt":"豪車","height":"336"},
{"src":"images/14.jpg","alt":"豪車","height":"340"},
{"src":"images/15.jpg","alt":"豪車","height":"340"},
{"src":"images/16.jpg","alt":"豪車","height":"331"}
];
//迴圈迭代圖片陣列,解析json物件
function initLoad(){
for(var i = 0;i < json.length;i++){
var car = "";
car+= '<div class="car animated bounceIn">';
car+= '<img src="'+json[i].src+'" width="240" height="'+json[i].height+'" alt="'+json[i].alt+'" />';
car+= '</div>';
//呼叫方法 拼接物件上去
getListDiv($(".list")).append(car);
};
};
//獲取list div 物件,進行不規則佈局
function getListDiv(obj){
//獲取總列數
var length = obj.length;
//高度無窮大
var h = Infinity;
//獲取每一列
var getList;
for(var i = 0;i < length;i++){
//如果當前列小於無窮大 就無線迴圈
if(obj.eq(i).height() < h){
h = obj.eq(i).height();
getList = obj.eq(i);
}
}
return getList;
};
//滾動條的高度
var scrollHeight = 0;
//滾動條距離頂部的高度
var scrollTop = 0;
//頁面一載入就執行此函式
$(function(){
//瀏覽器的高度
var _height = $(window).height();
//滑鼠滾動事件
$(window).scroll(function(){
scrollHeight = document.body.scrollHeight;
scrollTop = document.body.scrollTop;
//滾動條+瀏覽器的高度 大於
if(scrollTop + _height >= scrollHeight){
initLoad();
}
});
});
</script>
好了大功告成!oye