当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js自定义瀑布流布局插件

js自定义瀑布流布局插件

2018年05月01日  | 移动技术网IT编程  | 我要评论
瀑布流布局是网页中经常采用的一种布局方式,其布局有如下特点: 瀑布流布局特点: (1)图文元素按列排放 (2)列宽一致,但高度不等 (3)布局过程中将优先

瀑布流布局是网页中经常采用的一种布局方式,其布局有如下特点:

瀑布流布局特点:

(1)图文元素按列排放
(2)列宽一致,但高度不等
(3)布局过程中将优先向高度最小的列补充数据

以下是自定义的一个jquery瀑布流插件:jquery.mywaterfull.js

(function ($) {
 $.fn.extend({
  mywaterfull: function () {

   var parentwidth = $(this).width(); //获取每行的宽度
   var childitems = $(this).children();
   var childwidth = childitems.width(); //获取每一列的列宽
   var column = 5; //定义每行有多少列
   //计算并设置列与列之间的间隙
   var space = (parentwidth - column * childwidth) / (column - 1);
   //声明一个数组,用来存放第一行中每一列的高度
   var arrheight = [];

   //对子元素进行排列布局
   $.each(childitems, function (index, item) {
    if (index < column) { //对第一行的列进行排列布局
     $(item).css({
      top: 0,
      left: index * (childwidth + space)
     });
     arrheight.push($(item).height() + space); //将第一行中的列的高度添加到数组中
    } else {
     //找寻列高最小的那一列
     var minindex = 0;
     var minvalue = arrheight[minindex];
     //循环遍历找出最小的列高值
     for (var i = 0; i < arrheight.length; i++) {
      if (minvalue > arrheight[i]) {
       minvalue = arrheight[i];
       minindex = i;
      }
     }

     //对余下的子元素挨个排列布局
     $(item).css({
      top: minvalue + space,
      left: minindex * (childwidth + space)
     });

     //更新最小列高
     arrheight[minindex] += $(item).height() + space;
    }
   });

   //由于这里是利用定位对子元素进行布局,所以要更新父元素的高度
   //当然也可以利用浮动对子元素进行布局
   var maxheight = 0;
   for (var i = 0; i < arrheight.length; i++) {
    if (maxheight < arrheight[i]) {
     maxheight = arrheight[i];
    }
   }

   //设置父元素的高度
   $(this).height(maxheight);

  }
 });
})(jquery);

使用示例:

这里假设一个html结构:

<!doctype html>
<html lang="zh-cn">
<head>
 <meta charset="utf-8">
 <title>瀑布流案例原始</title>
 <style>
* {
 margin: 0;
 padding: 0;
}

body {
 font-family: microsoft yahei;
 background-color: #f5f5f5;
}

.container {
 width: 1200px;
 margin: 0 auto;
 padding-top: 50px;
}

.container > .items {
 position: relative;
}

.container > .items > .item {
 width: 232px;
 position: absolute;
 box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
 overflow: hidden;
}

.container > .items > .item > img {
 width: 100%;
 display: block;
 height: 232px;
}

.container > .items > .item:nth-child(3n) > img {
 width: 100%;
 display: block;
 height: 350px;
}

.container > .items > .item > p {
 margin: 0;
 padding: 10px;
 background: #fff;
}

.container > .btn {
 width: 280px;
 height: 40px;
 text-align: center;
 line-height: 40px;
 background-color: #ccc;
 border-radius: 8px;
 font-size: 24px;
 cursor: pointer;
}

.container > .loading {
 background-color: transparent;
}
</style>
</head>
<body>
<div class="container">
 <div class="items">

 </div>
 <div class="btn loading">正在加载...</div>
</div>

书写脚本文件,这里假设从后台获取子元素的数据,并用arttemplate模板引擎将数据渲染到页面:

<script src="js/jquery.min.js"></script>
<script src="js/jquery.mywaterfull.js"></script>
<script src="js/template.js"></script>

//定义引擎模板
<script id="template" type="text/html">
 {{ each items as item index}}
 <div class="item">
  <img src="{{item.path}}" alt="">
  <p>{{item.text}}</p>
 </div>
 {{/each}}
</script>

//书写脚本
$(function () {
 //根据接口文档,向服务器请求数据
 var page = 1, pagesize = 20;
 //当dom结构加载完毕,就调用一次render函数
 render();
 function render() {
  $.ajax({
   type: "get",
   url: "php/data.php",
   data: {
    page: page,
    pagesize: pagesize
   },
   beforesend: function () { //在发送请求前改变按钮的内容
    $(".btn").html("正在加载...").addclass("loading");
   },
   success: function (data) {
    //2.借助模板引擎,渲染数据
    var tplstr = template("template", data);
    $(".items").append(tplstr);
    $(".items").mywaterfull();
    //当加载完成后,改变按钮内容和样式
    $(".btn").html("加载更多").removeclass("loading");
    //当后台数据展示完毕时,向用户提示
    if (data.items.length < pagesize) {
     $(".btn").html("没有更多内容了").addclass("loading");
    }
    //每次响应成功后,将从后台返回的page保存起来
    page = data.page;
   }
  });
 }

 //当点击按钮时,加载下一页数据
 $(".btn").on('click',function () {
  if($(".btn").hasclass("loading")) return false;
  render();
 });

 //当页面滚动到数据底部的时候加载数据
 $(window).on('scroll',function () {
  //判断是否滚动到底部
  var isbottom = ($('.items').height()-($(window).height()-$('.items').offset().top))-$(window).scrolltop();
  if (isbottom <= 200 && !$('.btn').hasclass("loading")) render();
 });


});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网