JQuery implements waterfall flow layout

  • 2020-03-30 04:34:35
  • OfStack

HTML


 <div id="main">
         <div class="box">
             <div class="pic">
                 <img src="images/0.jpg" alt="">
             </div>
         </div>
         <div class="box">
             <div class="pic">
                 <img src="images/1.jpg" alt="">
             </div>
         </div>
         <div class="box">
             <div class="pic">
                 <img src="images/2.jpg" alt="">
             </div>
         </div>
         <div class="box">
             <div class="pic">
                 <img src="images/3.jpg" alt="">
             </div>
         </div>
         <div class="box">
             <div class="pic">
                 <img src="images/4.jpg" alt="">
             </div>
         </div>
         <div class="box">
             <div class="pic">
                 <img src="images/2.jpg" alt="">
             </div>
         </div>
 </div>

CSS


 * {
     margin: 0;
     padding: 0;
 }
 #main {
     position: relative;
 }
 .box {
     padding:15px 0 0 15px;
     float:left;
 }
 .pic {
     padding: 10px;
     border: 1px solid #ccc;
     border-radius: 5px;
     box-shadow: 0px 0px 5px #ccc;
     img {
         width:165px;
         height:auto;
     }
 }

JavaScript


 $(window).on("load",function () {
     waterfall();
     var dataInt = { "data":[{"src":"7.jpg"},{"src":"8.jpg"},{"src":"9.jpg"},{"src":"6.jpg"}]}
     //Simulate json data; < br / >      $(window).on("scroll",function () {
         if(checkScrollSlide){
             $.each(dataInt.data,function (key,value) {
                 var oBox=$("<div>").addClass("box").appendTo($("#main"));
                 //JQuery supports continuous affix and implicit iteration. < br / >                  var oPic=$("<div>").addClass('pic').appendTo($(oBox));
                 $("<img>").attr("src","images/"+$(value).attr("src")).appendTo(oPic);
             });
             waterfall();
         }
     })
 });
 //Flow layout main function; < br / >  function waterfall () {
     var $boxs=$("#main>div");
     //Gets the immediate child element div.box under the #main element; < br / >      //Gets the width of each column; < br / >      var w=$boxs.eq(0).outerWidth();
     //OuterWidth () gets the width including padding and border; < br / >      //var w=$boxs.eq(0).width();
     //Width () can only get the width defined for an element; < br / >      var cols=Math.floor($(window).width()/w);
     //How many columns; < br / >      $("#main").width(w*cols).css("margin","0 auto");
     //Set the width and center style of the #main element. < br / >      var hArr=[];
     //The set of heights of each column; < br / >      $boxs.each(function (index,value) {
     //Each box element is traversed; < br / >      //To find the lowest point of all previous elements, then set this element below the lowest point; < br / >          var h=$boxs.eq(index).outerHeight();
         //The height of each box element is
         if (index<cols) {
             hArr[index]=h;
             //Determines the height of the first element in each column; < br / >          } else{
             var minH=Math.min.apply(null,hArr);
             //Gets the minimum height in the column height array; < br / >              var minHIndex=$.inArray(minH,hArr);
             //The $.inarray () method gets the index value of the element (minH) in the array (hArr). < br / >              //console.log(value);
             //The value is the DOM object of all the box elements after the first line! ; < br / >              $(value).css({
             //$(value): convert DOM object into jQuery object, then continue to use jQuery method; < br / >                  "position":"absolute",
                 "top":minH+"px",
                 "left":minHIndex*w+"px"
             });
             hArr[minHIndex]+=$boxs.eq(index).outerHeight();
             //The height of the lowest height element + the height of the element just added to the lowest height = the new column height; < br / >          };
     });
     // console.log(hArr);
 };
 function checkScrollSlide () {
     var $lastBox=$("#main>div").last();
     var lastBoxDis=$lastBox.offset().top+Math.floor($lastBox.outerHeight()/2);
     var scrollTop=$(window).scrollTop();
     var documentH=$(window).height();
     return (lastBoxDis<scrollTop+documentH)?true:false;
 }

Explain it in detail and refer to the notes carefully, so I won't pull it out on its own.


Related articles: