当前位置:Gxlcms > JavaScript > js自定义瀑布流布局插件

js自定义瀑布流布局插件

时间:2021-07-01 10:21:17 帮助过:26人阅读

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

瀑布流布局特点:

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

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

  1. (function ($) {
  2. $.fn.extend({
  3. myWaterfull: function () {
  4. var parentWidth = $(this).width(); //获取每行的宽度
  5. var childItems = $(this).children();
  6. var childWidth = childItems.width(); //获取每一列的列宽
  7. var column = 5; //定义每行有多少列
  8. //计算并设置列与列之间的间隙
  9. var space = (parentWidth - column * childWidth) / (column - 1);
  10. //声明一个数组,用来存放第一行中每一列的高度
  11. var arrHeight = [];
  12. //对子元素进行排列布局
  13. $.each(childItems, function (index, item) {
  14. if (index < column) { //对第一行的列进行排列布局
  15. $(item).css({
  16. top: 0,
  17. left: index * (childWidth + space)
  18. });
  19. arrHeight.push($(item).height() + space); //将第一行中的列的高度添加到数组中
  20. } else {
  21. //找寻列高最小的那一列
  22. var minIndex = 0;
  23. var minValue = arrHeight[minIndex];
  24. //循环遍历找出最小的列高值
  25. for (var i = 0; i < arrHeight.length; i++) {
  26. if (minValue > arrHeight[i]) {
  27. minValue = arrHeight[i];
  28. minIndex = i;
  29. }
  30. }
  31. //对余下的子元素挨个排列布局
  32. $(item).css({
  33. top: minValue + space,
  34. left: minIndex * (childWidth + space)
  35. });
  36. //更新最小列高
  37. arrHeight[minIndex] += $(item).height() + space;
  38. }
  39. });
  40. //由于这里是利用定位对子元素进行布局,所以要更新父元素的高度
  41. //当然也可以利用浮动对子元素进行布局
  42. var maxHeight = 0;
  43. for (var i = 0; i < arrHeight.length; i++) {
  44. if (maxHeight < arrHeight[i]) {
  45. maxHeight = arrHeight[i];
  46. }
  47. }
  48. //设置父元素的高度
  49. $(this).height(maxHeight);
  50. }
  51. });
  52. })(jQuery);

使用示例:

这里假设一个HTML结构:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>瀑布流案例原始</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body {
  12. font-family: Microsoft Yahei;
  13. background-color: #f5f5f5;
  14. }
  15. .container {
  16. width: 1200px;
  17. margin: 0 auto;
  18. padding-top: 50px;
  19. }
  20. .container > .items {
  21. position: relative;
  22. }
  23. .container > .items > .item {
  24. width: 232px;
  25. position: absolute;
  26. box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  27. overflow: hidden;
  28. }
  29. .container > .items > .item > img {
  30. width: 100%;
  31. display: block;
  32. height: 232px;
  33. }
  34. .container > .items > .item:nth-child(3n) > img {
  35. width: 100%;
  36. display: block;
  37. height: 350px;
  38. }
  39. .container > .items > .item > p {
  40. margin: 0;
  41. padding: 10px;
  42. background: #fff;
  43. }
  44. .container > .btn {
  45. width: 280px;
  46. height: 40px;
  47. text-align: center;
  48. line-height: 40px;
  49. background-color: #ccc;
  50. border-radius: 8px;
  51. font-size: 24px;
  52. cursor: pointer;
  53. }
  54. .container > .loading {
  55. background-color: transparent;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="container">
  61. <div class="items">
  62. </div>
  63. <div class="btn loading">正在加载...</div>
  64. </div>

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

  1. <script src="JS/jquery.min.js"></script>
  2. <script src="JS/jquery.myWaterfull.js"></script>
  3. <script src="JS/template.js"></script>
  4. //定义引擎模板
  5. <script id="template" type="text/html">
  6. {{ each items as item index}}
  7. <div class="item">
  8. <img src="{{item.path}}" alt="">
  9. <p>{{item.text}}</p>
  10. </div>
  11. {{/each}}
  12. </script>
  13. //书写脚本
  14. $(function () {
  15. //根据接口文档,向服务器请求数据
  16. var page = 1, pageSize = 20;
  17. //当DOM结构加载完毕,就调用一次render函数
  18. render();
  19. function render() {
  20. $.ajax({
  21. type: "get",
  22. url: "php/data.php",
  23. data: {
  24. page: page,
  25. pageSize: pageSize
  26. },
  27. beforeSend: function () { //在发送请求前改变按钮的内容
  28. $(".btn").html("正在加载...").addClass("loading");
  29. },
  30. success: function (data) {
  31. //2.借助模板引擎,渲染数据
  32. var tplStr = template("template", data);
  33. $(".items").append(tplStr);
  34. $(".items").myWaterfull();
  35. //当加载完成后,改变按钮内容和样式
  36. $(".btn").html("加载更多").removeClass("loading");
  37. //当后台数据展示完毕时,向用户提示
  38. if (data.items.length < pageSize) {
  39. $(".btn").html("没有更多内容了").addClass("loading");
  40. }
  41. //每次响应成功后,将从后台返回的page保存起来
  42. page = data.page;
  43. }
  44. });
  45. }
  46. //当点击按钮时,加载下一页数据
  47. $(".btn").on('click',function () {
  48. if($(".btn").hasClass("loading")) return false;
  49. render();
  50. });
  51. //当页面滚动到数据底部的时候加载数据
  52. $(window).on('scroll',function () {
  53. //判断是否滚动到底部
  54. var isBottom = ($('.items').height()-($(window).height()-$('.items').offset().top))-$(window).scrollTop();
  55. if (isBottom <= 200 && !$('.btn').hasClass("loading")) render();
  56. });
  57. });

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

人气教程排行