当前位置:Gxlcms > JavaScript > js实现瀑布流效果(自动生成新的内容)

js实现瀑布流效果(自动生成新的内容)

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

当滚动条接近底部会自动生成新的内容(色块)

效果图:

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. *{list-style: none;}
  8. div{overflow: hidden;}
  9. ul{float: left;}
  10. li{width:300px; margin-bottom:10px;}
  11. </style>
  12. <script>
  13. function rnd(n,m){
  14. return parseInt(Math.random()*(m-n))+n;
  15. }
  16. function cl(){
  17. var li = document.createElement('li');
  18. li.style.height=rnd(100,500)+'px';
  19. li.style.background='rgb('+rnd(0,255)+','+rnd(0,255)+','+rnd(0,255)+')';
  20. return li;
  21. }
  22. window.onload=function(){
  23. var aUl = document.getElementsByTagName('ul');
  24. //alert(aUl.length);
  25. function c20(){
  26. for(var i =0;i<20;i++){
  27. var arr =[];
  28. for(var j=0;j<aUl.length;j++){
  29. arr.push(aUl[j])
  30. }
  31. arr.sort(function(n,m){
  32. return n.offsetHeight- m.offsetHeight
  33. });
  34. arr[0].appendChild(cl());
  35. }
  36. }
  37. c20();
  38. window.onscroll=function(){
  39. var arr = [];
  40. for (var j = 0; j < aUl.length; j++) {
  41. arr.push(aUl[j])
  42. }
  43. arr.sort(function (n, m) {
  44. return n.offsetHeight - m.offsetHeight
  45. });
  46. var n = (document.body.scrollTop || document.documentElement.scrollTop) + document.documentElement.clientHeight;
  47. if (n > arr[0].offsetHeight) {
  48. c20()
  49. }
  50. }
  51. }
  52. </script>
  53. </head>
  54. <body>
  55. <div>
  56. <ul></ul>
  57. <ul></ul>
  58. <ul></ul>
  59. </div>
  60. </body>
  61. </html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

人气教程排行