实现豆瓣图书

  1. //js
  2. function ajax({
  3. url,
  4. method,
  5. success
  6. }){
  7. var xhr = new XMLHttpRequest()
  8. xhr.open(method,url,true)
  9. xhr.send()
  10. xhr.onreadystatechange = function(){
  11. if(xhr.readyState == 4 && xhr.status == 200){
  12. var result = JSON.parse(xhr.responseText);
  13. success(result);
  14. }
  15. }
  16. }
  1. <div id="app">
  2. <div class="left">
  3. </div>
  4. </div>
  5. <script>
  6. var listUrl="http://192.168.4.18:8000/";
  7. ajax({
  8. url:listUrl,
  9. method:"get",
  10. success:res=>{
  11. var arr = res.result;
  12. arr.forEach((item,index)=>{
  13. var {pic,title,raiting,slogo,evaluate,labels} = item;
  14. var sum = labels.join("/")
  15. var htmlItem = `
  16. <div class="item">
  17. <span id="span">${index+1}</span>
  18. <img src=${pic} alt="">
  19. <div class="monddle">
  20. <p id="title">${title}</p>
  21. <p class="on">${sum}</p>
  22. <p class="on"><span>${raiting}</span>&nbsp;&nbsp;<span>${evaluate}人评价</span></p>
  23. <p class="on">${slogo}</p>
  24. </div>
  25. </div>
  26. `
  27. $(".left").append(htmlItem);
  28. })
  29. }
  30. })
  31. </script>

实现下拉刷新

  1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>

style

  1. .item {
  2. float: left;
  3. border: 1px solid #eee;
  4. }
  5. .item img {
  6. width: 240px;
  7. vertical-align: bottom;
  8. padding: 5px;
  9. }

内容

  1. <div id="app">
  2. </div>

js

  1. http();
  2. $(window).scroll(function(){
  3. if(isReachBottom()){
  4. http();
  5. }
  6. })
  7. function http() {
  8. var url = "http://192.168.4.18:8000/more"
  9. $.ajax({
  10. url,
  11. success: res => {
  12. res.forEach(item => {
  13. var template = `
  14. <div class ="item">
  15. <img src = ${item.imageUrl}
  16. </div>
  17. `
  18. $("#app").append(template)
  19. })
  20. setTimeout(sortBox, 100)
  21. }
  22. })
  23. }
  24. function sortBox() {
  25. /* 1.获取一排能放几个盒子 */
  26. var ww = $(window).width();
  27. var box = $(".item").width();
  28. var num = Math.floor(ww / box);
  29. var arr = [];
  30. /* 2.将第一排的高度放到一个数组中 */
  31. $(".item").each((index, item) => {
  32. if (index < num) {
  33. var height = $(item).outerHeight();
  34. arr.push(height)
  35. } else {
  36. /* 3.从最小高度的地方添加图片 */
  37. var minHeight = Math.min(...arr);
  38. var index = arr.indexOf(minHeight);
  39. var offsetLeft = $(".item").eq(index).offset().left;
  40. $(item).css({position:"absolute",top:minHeight,left:offsetLeft});
  41. arr[index] = minHeight + $(item).outerHeight();
  42. }
  43. })
  44. }
  45. function isReachBottom(){
  46. var scrollTop = $(window).scrollTop();
  47. var availHeight = $(window).height();
  48. var dh = $(document).height();
  49. return (scrollTop+availHeight>dh-200)?true:false;
  50. }

实现网页云下拉刷新

  1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  2. <script src="lib/http.js"></script>

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. }
  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. /* 第一http请求 */
  20. loadData();
  21. jump();
  22. $(window).scroll(function(){
  23. if(isReachBottom()){
  24. var offset = $(".item").length;
  25. loadData(offset)
  26. }
  27. })
  28. function jump(){
  29. setTimeout(()=>{
  30. $(".item").click(function(event){
  31. let aid = event.currentTarget.dataset.aid;
  32. location.href = `datail.html?id=${aid}`
  33. })
  34. },200)
  35. }
  36. function isReachBottom() {
  37. var scrollTop = $(window).scrollTop();
  38. var availHeight = $(window).height();
  39. var dh = $(document).height();
  40. console.log(scrollTop + availHeight)
  41. return (scrollTop + availHeight == dh) ? true : false;
  42. }
  43. function loadData(offset){
  44. http({
  45. offset,
  46. success: res => {
  47. var playlists = res.playlists;
  48. playlists.forEach(item => {
  49. var template = `
  50. <div class="item" data-aid=${item.id}>
  51. <img src=${item.coverImgUrl}>
  52. </div>
  53. `
  54. $("#app").append(template)
  55. })
  56. }
  57. })
  58. }
  59. </script>

实现下拉刷新的瀑布流

  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. <style>
  9. .item {
  10. float: left;
  11. border: 1px solid #eee;
  12. }
  13. .item img {
  14. width: 240px;
  15. vertical-align: bottom;
  16. padding: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="app">
  22. </div>
  23. <script>
  24. http();
  25. $(window).scroll(function(){
  26. if(isReachBottom()){
  27. http();
  28. }
  29. })
  30. function http() {
  31. var url = "http://192.168.4.18:8000/more"
  32. $.ajax({
  33. url,
  34. success: res => {
  35. res.forEach(item => {
  36. var template = `
  37. <div class ="item">
  38. <img src = ${item.imageUrl}
  39. </div>
  40. `
  41. $("#app").append(template)
  42. })
  43. setTimeout(sortBox, 100)
  44. }
  45. })
  46. }
  47. function sortBox() {
  48. /* 1.获取一排能放几个盒子 */
  49. var ww = $(window).width();
  50. var box = $(".item").width();
  51. var num = Math.floor(ww / box);
  52. var arr = [];
  53. /* 2.将第一排的高度放到一个数组中 */
  54. $(".item").each((index, item) => {
  55. if (index < num) {
  56. var height = $(item).outerHeight();
  57. arr.push(height)
  58. } else {
  59. /* 3.从最小高度的地方添加图片 */
  60. var minHeight = Math.min(...arr);
  61. var index = arr.indexOf(minHeight);
  62. var offsetLeft = $(".item").eq(index).offset().left;
  63. $(item).css({position:"absolute",top:minHeight,left:offsetLeft});
  64. arr[index] = minHeight + $(item).outerHeight();
  65. }
  66. })
  67. }
  68. function isReachBottom(){
  69. var scrollTop = $(window).scrollTop();
  70. var availHeight = $(window).height();
  71. var dh = $(document).height();
  72. return (scrollTop+availHeight>dh-200)?true:false;
  73. }
  74. </script>
  75. </body>
  76. </html>