实现豆瓣图书
//jsfunction ajax({url,method,success}){var xhr = new XMLHttpRequest()xhr.open(method,url,true)xhr.send()xhr.onreadystatechange = function(){if(xhr.readyState == 4 && xhr.status == 200){var result = JSON.parse(xhr.responseText);success(result);}}}
<div id="app"><div class="left"></div></div><script>var listUrl="http://192.168.4.18:8000/";ajax({url:listUrl,method:"get",success:res=>{var arr = res.result;arr.forEach((item,index)=>{var {pic,title,raiting,slogo,evaluate,labels} = item;var sum = labels.join("/")var htmlItem = `<div class="item"><span id="span">${index+1}</span><img src=${pic} alt=""><div class="monddle"><p id="title">${title}</p><p class="on">${sum}</p><p class="on"><span>${raiting}</span> <span>${evaluate}人评价</span></p><p class="on">${slogo}</p></div></div>`$(".left").append(htmlItem);})}})</script>
实现下拉刷新
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
style
.item {float: left;border: 1px solid #eee;}.item img {width: 240px;vertical-align: bottom;padding: 5px;}
内容
<div id="app"></div>
js
http();$(window).scroll(function(){if(isReachBottom()){http();}})function http() {var url = "http://192.168.4.18:8000/more"$.ajax({url,success: res => {res.forEach(item => {var template = `<div class ="item"><img src = ${item.imageUrl}</div>`$("#app").append(template)})setTimeout(sortBox, 100)}})}function sortBox() {/* 1.获取一排能放几个盒子 */var ww = $(window).width();var box = $(".item").width();var num = Math.floor(ww / box);var arr = [];/* 2.将第一排的高度放到一个数组中 */$(".item").each((index, item) => {if (index < num) {var height = $(item).outerHeight();arr.push(height)} else {/* 3.从最小高度的地方添加图片 */var minHeight = Math.min(...arr);var index = arr.indexOf(minHeight);var offsetLeft = $(".item").eq(index).offset().left;$(item).css({position:"absolute",top:minHeight,left:offsetLeft});arr[index] = minHeight + $(item).outerHeight();}})}function isReachBottom(){var scrollTop = $(window).scrollTop();var availHeight = $(window).height();var dh = $(document).height();return (scrollTop+availHeight>dh-200)?true:false;}
实现网页云下拉刷新
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script><script src="lib/http.js"></script>
http.js
function http({offset=0,success}){var url =`http://192.168.4.18:3000/top/playlist?cat=华语&limit=20&offset=${offset}`$.ajax({url,success:res=>{success(res)}})}
<style>.item img {width: 150px;height: 150px;padding: 5px;}.item {border: 1px solid #eee;float: left;margin-right: 80px;margin-top: 50px;}</style></head><body><div id="app"></div><script>/* 第一http请求 */loadData();jump();$(window).scroll(function(){if(isReachBottom()){var offset = $(".item").length;loadData(offset)}})function jump(){setTimeout(()=>{$(".item").click(function(event){let aid = event.currentTarget.dataset.aid;location.href = `datail.html?id=${aid}`})},200)}function isReachBottom() {var scrollTop = $(window).scrollTop();var availHeight = $(window).height();var dh = $(document).height();console.log(scrollTop + availHeight)return (scrollTop + availHeight == dh) ? true : false;}function loadData(offset){http({offset,success: res => {var playlists = res.playlists;playlists.forEach(item => {var template = `<div class="item" data-aid=${item.id}><img src=${item.coverImgUrl}></div>`$("#app").append(template)})}})}</script>
实现下拉刷新的瀑布流
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script><style>.item {float: left;border: 1px solid #eee;}.item img {width: 240px;vertical-align: bottom;padding: 5px;}</style></head><body><div id="app"></div><script>http();$(window).scroll(function(){if(isReachBottom()){http();}})function http() {var url = "http://192.168.4.18:8000/more"$.ajax({url,success: res => {res.forEach(item => {var template = `<div class ="item"><img src = ${item.imageUrl}</div>`$("#app").append(template)})setTimeout(sortBox, 100)}})}function sortBox() {/* 1.获取一排能放几个盒子 */var ww = $(window).width();var box = $(".item").width();var num = Math.floor(ww / box);var arr = [];/* 2.将第一排的高度放到一个数组中 */$(".item").each((index, item) => {if (index < num) {var height = $(item).outerHeight();arr.push(height)} else {/* 3.从最小高度的地方添加图片 */var minHeight = Math.min(...arr);var index = arr.indexOf(minHeight);var offsetLeft = $(".item").eq(index).offset().left;$(item).css({position:"absolute",top:minHeight,left:offsetLeft});arr[index] = minHeight + $(item).outerHeight();}})}function isReachBottom(){var scrollTop = $(window).scrollTop();var availHeight = $(window).height();var dh = $(document).height();return (scrollTop+availHeight>dh-200)?true:false;}</script></body></html>
