waterfall.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. window.onload = function () {
  2. var waterfall = document.getElementById('waterfall');
  3. var aList = waterfall.getElementsByTagName('li');
  4. ajax({
  5. url: 'data/imagesData.json',
  6. success: function (response, xml) {
  7. renderDOM(response);
  8. }
  9. });
  10. // 窗口事件
  11. window.onscroll = function () {
  12. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  13. var waterfall_h = waterfall.offsetHeight;
  14. var window_h = window.innerHeight;
  15. if (waterfall_h - window_h < scrollTop) {
  16. ajax({
  17. url: 'data/1.json',
  18. success: function (response, xml) {
  19. renderDOM(response);
  20. }
  21. });
  22. }
  23. };
  24. window.onresize = function () {
  25. setPosition();
  26. };
  27. function renderDOM(response) {
  28. var data = JSON.parse(response);
  29. for (var i = 0; i < data.length; i++) {
  30. var create_li = document.createElement('li');
  31. var create_a = document.createElement('a');
  32. var create_title = document.createElement('p');
  33. create_a.innerHTML = '<img src='+data[i].picture+' alt="#">';
  34. create_a.className = 'bounceIn animated';
  35. create_a.href = data[i].target;
  36. create_a.style.height = data[i].height + 'px';
  37. create_title.innerHTML = data[i].title;
  38. create_li.appendChild(create_a);
  39. create_li.appendChild(create_title);
  40. waterfall.appendChild(create_li);
  41. }
  42. setPosition();
  43. };
  44. /*
  45. // 图片预加载
  46. function renderDOM(response) {
  47. var data = JSON.parse(response);
  48. for (var i = 0; i < data.length; i++) {
  49. prepareImage(data[i].picture, function () {
  50. var create_li = document.createElement('li');
  51. var create_img = document.createElement('img');
  52. create_img.src = this.src;
  53. create_img.style.height = this.height + 'px';
  54. create_li.appendChild(create_img);
  55. waterfall.appendChild(create_li);
  56. setPosition();
  57. });
  58. }
  59. };*/
  60. function setPosition() {
  61. var cols = getCols();
  62. var heightArr = [];
  63. var i = 0;
  64. var waterfall_h = 0;
  65. var item_width = aList[0].offsetWidth + 10;
  66. for (i = 0; i < aList.length; i++) {
  67. if (i < cols) {
  68. heightArr.push(aList[i].offsetHeight);
  69. aList[i].style.top = 0;
  70. aList[i].style.left = i * item_width + 'px';
  71. } else {
  72. var _index = getShort(heightArr);
  73. var min = Math.min.apply(null, heightArr);
  74. aList[i].style.left = aList[_index].offsetLeft + 'px';
  75. aList[i].style.top = min + 20 + 'px';
  76. heightArr[_index] += aList[i].offsetHeight + 20;
  77. }
  78. aList[i].style.position = 'absolute';
  79. aList[i].style.margin = 0;
  80. }
  81. waterfall_h = aList[aList.length - 1].offsetTop + aList[aList.length - 1].offsetHeight;
  82. waterfall.style.width = cols * item_width + 'px';
  83. waterfall.style.height = waterfall_h + 'px';
  84. };
  85. function getShort(heightArr) {
  86. var index = 0;
  87. var min = heightArr[0];
  88. for (var i = 0; i < heightArr.length; i++) {
  89. if (min > heightArr[i]) {
  90. min = heightArr[i];
  91. index = i;
  92. }
  93. }
  94. return index;
  95. };
  96. function getCols() {
  97. var body_w = document.documentElement.clientWidth;
  98. var item_w = aList[0].offsetWidth;
  99. var result = Math.floor(body_w / item_w);
  100. return result >= 5 ? 5 : result;
  101. };
  102. function prepareImage(imageUrl, callBack) {
  103. var oImg = new Image();
  104. oImg.src = imageUrl;
  105. oImg.onload = function () {
  106. callBack.call(oImg);
  107. };
  108. };
  109. };