1.实现一个瀑布流

  1. <style>
  2. *{margin:0;padding:0}
  3. .item img{
  4. width:240px;
  5. padding:10px;
  6. }
  7. .item{
  8. border:1px solid #eee;
  9. float: left;
  10. font-size: 0;
  11. }
  12. </style>
  1. <div id="app">
  2. <div class="item"><img src="images/01.jpg" alt=""></div>
  3. <div class="item"><img src="images/02.jpg" alt=""></div>
  4. <div class="item"><img src="images/03.jpg" alt=""></div>
  5. <div class="item"><img src="images/04.jpg" alt=""></div>
  6. <div class="item"><img src="images/05.jpg" alt=""></div>
  7. <div class="item"><img src="images/06.jpg" alt=""></div>
  8. <div class="item"><img src="images/07.jpg" alt=""></div>
  9. <div class="item"><img src="images/08.jpg" alt=""></div>
  10. <div class="item"><img src="images/09.jpg" alt=""></div>
  11. <div class="item"><img src="images/10.jpg" alt=""></div>
  12. <div class="item"><img src="images/11.jpg" alt=""></div>
  13. <div class="item"><img src="images/12.jpg" alt=""></div>
  14. </div>
  15. <script>
  16. /* 1.得到一排能放置几个元素 */
  17. var ww = $(window).width();
  18. var box = $(".item").outerWidth();
  19. var num = Math.floor(ww/box);
  20. /* 2.定义一个数组获取第一排元素的height */
  21. var arr = [];
  22. $(".item").each((index,value)=>{
  23. if(index<num){
  24. var height = $(value).outerHeight();
  25. arr.push(height)
  26. }else{
  27. /* 3.将元素放置在最小高度的位置 同时将数组重置 */
  28. var minHeight = Math.min(...arr);
  29. var index = arr.indexOf(minHeight);
  30. var offsetLeft = $(".item").eq(index).offset().left;
  31. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  32. arr[index] = minHeight+$(value).outerHeight();
  33. }
  34. })
  35. </script>

2.瀑布流下拉刷新

  1. <script>
  2. http();
  3. $(window).scroll(function(){
  4. if(isReachbottom()){
  5. http();
  6. }
  7. })
  8. function http(){
  9. var url = "http://192.168.4.18:8000/more";
  10. $.ajax({
  11. url,
  12. method: "get",
  13. success: res => {
  14. res.forEach(item => {
  15. var { imageUrl } = item;
  16. var html = `
  17. <div class="item"><img src=${imageUrl} alt=""></div>
  18. `
  19. $("#app").append(html);
  20. })
  21. setTimeout(sortBox,100)
  22. }
  23. })
  24. }
  25. function sortBox(){
  26. //1.得到一排能放置几个元素
  27. var ww=$(window).width();//获取浏览器窗口宽度
  28. var box=$(".item").outerWidth();//获取盒子的宽度,outerWidth包含盒子内容区,填充,边框
  29. var num=Math.floor(ww/box);//一排能放几个图片
  30. console.log(num);
  31. //2.定义一个数组获取第一排元素的height
  32. var arr=[];
  33. $(".item").each((index,value)=>{
  34. if(index<num){
  35. console.log(value)
  36. var height=$(value).outerHeight();
  37. console.log(height);
  38. arr.push(height)
  39. }else{
  40. //将元素放置在最小高度
  41. var minHeight=Math.min(...arr);
  42. var index=arr.indexOf(minHeight);
  43. var offsetLeft=$(".item").eq(index).offset().left;
  44. $(value).css({position:"absolute",left:offsetLeft,top:minHeight});
  45. arr[index]=minHeight+$(value).outerHeight();
  46. }
  47. })
  48. }
  49. function isReachbottom() {
  50. var scrollTop = $(window).scrollTop();
  51. var availHeight = $(window).height();
  52. var dw = $(document).height();
  53. return (scrollTop + availHeight) > dw - 100 ? true : false;
  54. }
  55. </script>

3.实现网易云下拉刷新

封装的js

  1. function http({
  2. offset=0,
  3. success
  4. }){
  5. var url =`http://192.168.4.18:3000/top/playlist?cat=华语&limit=20&offset=${offset}`
  6. $.ajax({
  7. url,
  8. success:res=>{
  9. success(res)
  10. }
  11. })
  12. }

html代码

  1. <style>
  2. .item img{
  3. width: 150px;
  4. height: 150px;
  5. padding: 5px;
  6. }
  7. .item{
  8. border:1px solid #eee;
  9. float:left;
  10. margin-right: 80px;
  11. margin-top: 50px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. </div>
  18. <script>
  19. loadData()
  20. jump();
  21. $(window).scroll(function(){
  22. if(isReachBottom()){
  23. var offset=$(".item").length;
  24. loadData(offset)
  25. }
  26. })
  27. function jump() {
  28. setTimeout(() => {
  29. $(".item").click(function (event) {
  30. let aid = event.currentTarget.dataset.aid;
  31. location.href = `2.detail.html?id=${aid}`
  32. })
  33. }, 200)
  34. }
  35. function isReachBottom(){
  36. var scrollTop=$(window).scrollTop();//获取滚动条距离顶部的高度
  37. var availHeight=$(window).height();//获取可视区的高度
  38. var dh=$(document).height();//获取当前文档的高度
  39. return (scrollTop+availHeight==dh)?true:false;
  40. }
  41. function loadData(offset){
  42. http({
  43. offset,
  44. success:res=>{
  45. var playlists=res. playlists;
  46. playlists.forEach(item=>{
  47. // console.log(playlists);
  48. var html=`
  49. <div class="item" data-aid=${item.id}>
  50. <img src=${item.coverImgUrl}>
  51. </div>
  52. `
  53. $("#app").append(html)
  54. })
  55. }
  56. })
  57. }
  58. </script>