eg:

Video_2020-12-08_163906.wmv (3.48MB)

lib/http.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. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  8. <script src="lib/http.js"></script>
  9. <style>
  10. .item img {
  11. width: 150px;
  12. height: 150px;
  13. padding: 5px;
  14. }
  15. .item {
  16. border: 1px solid #eee;
  17. float: left;
  18. margin-right: 80px;
  19. margin-top: 50px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="app">
  25. </div>
  26. <script>
  27. /* 初始化数据 */
  28. loadData()
  29. jump();
  30. /* 下拉刷新加载的数据 */
  31. $(window).scroll(function () {
  32. if (isReachBottom()) {
  33. var offset = $(".item").length;
  34. loadData(offset)
  35. }
  36. })
  37. function jump() {
  38. setTimeout(() => {
  39. $(".item").click(function (event) {
  40. let aid = event.currentTarget.dataset.aid;
  41. location.href = `detail.html?id=${aid}`
  42. })
  43. }, 200)
  44. }
  45. function isReachBottom() {
  46. var scrollTop = $(window).scrollTop();
  47. var availHeight = $(window).height();
  48. var dh = $(document).height();
  49. return (scrollTop + availHeight == dh) ? true : false;
  50. }
  51. function loadData(offset) {
  52. http({
  53. offset,
  54. success: res => {
  55. var playlists = res.playlists;
  56. playlists.forEach(item => {
  57. var template = `
  58. <div class="item" data-aid=${item.id}>
  59. <img src=${item.coverImgUrl}>
  60. </div>
  61. `
  62. $("#app").append(template)
  63. })
  64. }
  65. })
  66. }
  67. </script>
  68. </body>
  69. </html>