Waterfall Stream-Ost javacript + Jquery implementation

Source: Internet
Author: User

<! Doctype>
<title> Waterfall Flow Layout </title>
<meta charset= "Utf-8"/>
<script type= "Text/javascript" src= "Js/jquery-1.8.3.min.js" ></script>
<style type= "Text/css" >
*{padding:0;margin:0;}
#main {
position:relative;
}
. pin{
padding:15px 0 0 15px;
Float:left;
}
. box{
padding:10px;
border:1px solid #ccc;
box-shadow:0 0 6px #ccc;
border-radius:5px;
}
. Box img{
width:162px;
Height:auto;
}
</style>
<body>
<div id= "Main" >
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
<div class= "Pin" >
<div class= "box" >

</div>
</div>
</div>
</body>
<script type= "Text/javascript" >

$ (window). On (' Load ', function () {
OnLoad ();
$ (window). On (' scroll ', function () {
var data = {' data ': [{' src ': ' images/1.jpg '},{' src ': ' images/2.jpg '},{' src ': ' images/3.jpg '},{' src ': ' images/4.jpg '}]} ;
if (Isonload ()) {
$.each (Data.data, function (index, item) {
var html = html + ' <div class= "pin" ><div class= "box" ></div& Gt;</div> ';
$ (' #main '). HTML (HTML);
var $oPin = $ (' <div> '). addclass (' pin '). AppendTo ($ ("#main"));
var $oBox = $ (' <div> '). addclass (' box '). AppendTo ($oPin);
$ (' '). attr (' src ', $ (item). attr (' src ')). AppendTo ($oBox);
});
OnLoad ();
}
})
})

function OnLoad () {
var parentnode = $ (' #main '),
Nodechilds = $ (' #main div.pin ');
var clientwidth = $ (window). width ();
var nodewidth = nodechilds.eq (0). Outerwidth ();
var num = Math.floor (clientwidth/nodewidth);
Parentnode.css ({
' Width ': num*nodewidth + ' px ',
' Margin ': ' 0 auto '
});

var childsarr = [];
Nodechilds.each (function (index, item) {
var nodeheight = nodechilds.eq (index). Outerheight ();
if (Index < num) {
Childsarr[index] = nodeheight;
}else{
var MinH = Math.min.apply (null, Childsarr);
var minhindex = $.inarray (MinH, Childsarr);
$ (item). CSS ({
' Position ': ' absolute ',
' Top ': MinH,
' Left ': Nodechilds.eq (Minhindex). Position (). Left
});
Childsarr[minhindex] + = NODECHILDS.EQ (index). Outerheight ();
}
});
}

function Isonload () {
var clientheight = $ (window). Height ();
var nodechilds = $ (' #main div.pin ');
var nodechildtop = Nodechilds.last (). Get (0). OffsetTop + Math.floor (Nodechilds.last (). Outerheight ()/2);
var scrolltop = $ (window). scrolltop ();
Return (Nodechildtop < clientheight + scrolltop)? True:false;
}
Window.onload = function () {
Onloadimg (' main ', ' pin ');

Window.onscroll = function () {
var data = {' data ': [{' src ': ' images/1.jpg '},{' src ': ' images/2.jpg '},{' src ': ' images/3.jpg '},{' src ': ' images/4.jpg '} ]};
var Mainid = document.getElementById (' main ');
if (isloadimg ()) {
for (var i = 0, len = data.data.length; i < Len; i++) {
var divhtml = ' <div class= pin ' ><div class= "box" > ';
var opindiv = document.createelement (' div ');
Opindiv.classname = ' pin ';
Mainid.appendchild (OPINDIV);
var opinboxdiv = document.createelement (' div ');
opinboxdiv.classname = ' box ';
Opindiv.appendchild (OPINBOXDIV);
var oimg = document.createelement (' img ');
OIMG.SRC = ' images/' + data.data[i].src;
Opinboxdiv.appendchild (OIMG);
mainid.innerhtml + = divhtml;
//       }
Onloadimg (' main ', ' pin ');
//     }
//   }
// }

function onloadimg (parent, Classbox) {
var documentwidth = Document.documentElement.clientWidth | | Document.body.clientWidth;
var oparent = document.getElementById (parent);
var Opin = GetClassName (oparent, Classbox);
var opinwidth = opin[0].offsetwidth;
var num = Math.floor (documentwidth/opinwidth);
Oparent.style.cssText = Opinwidth*num + ' px; margin:0 auto; ';

var opinarr = [];
for (var i = 0, len = opin.length; i < Len; i++) {
var opinheight = opin[i].offsetheight;
if (i < num) {
Opinarr[i] = opinheight;
}else{
var minheight = Math.min.apply (null, Opinarr);
var minindex = Getminheightindex (Opinarr, minheight);
opin[i].style.position = ' absolute ';
Opin[i].style.top = minheight + ' px ';
Opin[i].style.left = opin[minindex].offsetleft + ' px ';
Opinarr[minindex] + = opin[i].offsetheight;
//     }
//   }

// }

function GetClassName (parent, ClassName) {
var classnames = [];
var nodes = parent.getelementsbytagname (' * ');
for (var i = 0, len = nodes.length; i < Len; i++) {
if (Nodes[i].classname = = ClassName) {
Classnames.push (Nodes[i]);
//     }
//   }
return classnames;
// }

function Getminheightindex (arr, MinH) {
for (var i in arr) {
if (arr[i] = = MinH) {
return i;
//     }
//   }
// }

function isloadimg () {
var Mainid = document.getElementById (' main ');
var clientheight = Document.documentElement.clientHeight | | Document.body.clientHeight;
var Opin = getclassname (Mainid, ' pin ');
var olast = opin[opin.length-1];
var olastheight = olast.offsettop + Math.floor (OLAST.OFFSETHEIGHT/2);
var scrotop = Document.documentElement.scrollTop | | Document.body.scrollTop;
Return (Olastheight < Scrotop + clientheight)? True:false;
// }

</script>


Waterfall Stream-Ost javacript + Jquery implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.