http.js

    1. var url ="https://douban-api.uieee.com/v2/movie/top250";
    2. function http({type="get",callback,data={}}){
    3. $.ajax({
    4. url,
    5. data,
    6. type,
    7. dataType:"jsonp",
    8. success: function(res) {
    9. callback(res)
    10. }
    11. })
    12. }
    13. function onReachBottom(){
    14. var dh = $(document).height();
    15. var sh = $(window).scrollTop();
    16. var wh = $(window).height();
    17. return (Math.ceil(sh + wh) == dh) ? true : false;
    18. }
    1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    2. <script src="lib/http.js"></script>
    3. <style>
    4. #app {
    5. display: flex;
    6. flex-wrap: wrap;
    7. justify-content: space-between;
    8. }
    9. </style>
    1. <div id="app">
    2. </div>
    3. <script>
    4. var start = 0;
    5. http({
    6. data: {
    7. start
    8. },
    9. callback: handleData
    10. })
    11. function handleData(res) {
    12. var subjects = res.subjects;
    13. var movies = [];
    14. for (var i = 0; i < subjects.length; i++) {
    15. var obj = {}
    16. obj.title = subjects[i].title;
    17. obj.imageUrl = subjects[i].images.small;
    18. movies.push(obj)
    19. }
    20. for (var j = 0; j < movies.length; j++) {
    21. var item = $(`<div class="item"><img src="${movies[j].imageUrl}"/><p>${movies[j].title}</p></div>`)
    22. $("#app").append(item)
    23. }
    24. $(window).scroll(function(){
    25. if(onReachBottom()){
    26. start+=20;
    27. http({
    28. data:{
    29. start
    30. },
    31. callback:handleData
    32. })
    33. }
    34. })
    35. }
    36. </script>