Json資料型別/分頁演算法/資料瀑布流載入
阿新 • • 發佈:2021-01-10
技術標籤:JavaScript
1.Json資料型別
簡單Json的使用
後臺給前端返回的資料格式 JSON
解析json 進行資料繫結
JSON 資料型別是 : 陣列 + 物件
建立(name上的引號可以不加)
var arr=[
{"name":"b1"},
{"name":"b2"},
{"name":"b3"},
{"name":"b4"}
];
呼叫
console.log(arr[1].name);
2.分頁演算法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style >
* {
padding: 0;
margin: 0;
}
.page li {
list-style: none;
float: left;
margin: 0 2px;
}
.box {
display: inline-block;
}
.btn_next,
.btn_top {
width : 50px;
height: 25px;
border: 1px solid #cbcbcb;
text-align: center;
line-height: 23px;
box-sizing: border-box;
}
.page_input {
width: 30px;
height: 23px;
border: 1px solid #cbcbcb;
outline: none;
text-align: center;
box-sizing: border-box;
}
.btn {
width: 50px;
height: 23px;
outline: none;
text-align: center;
box-sizing: border-box;
border: none;
}
.page_ul>li {
width: 30px;
height: 25px;
line-height: 23px;
text-align: center;
border: 1px solid #cbcbcb;
font-size: 13px;
box-sizing: border-box;
}
.ck {
background-color: #373ada;
color: #fff;
}
.page_list {
display: inline-block;
}
.btnList {
display: inline-block;
}
</style>
</head>
<body>
<div class="box">
<ul class="page">
<li class="btn_top">上一頁</li>
<li class="page_list">
<ul class="page_ul">
</ul>
</li>
<li class="btn_next">下一頁</li>
<li>
<input type="text" class="page_input">
<button class="btn">跳轉</button>
</li>
</ul>
</div>
<script>
var list = document.getElementsByClassName('page_ul')[0]
var btnList = document.getElementsByClassName('btnList')
var btn_next = document.getElementsByClassName('btn_next')[0]
var btn_top = document.getElementsByClassName('btn_top')[0]
var page_input = document.getElementsByClassName('page_input')[0]
var btn = document.getElementsByClassName('btn')[0]
var isTo = true
var page = {
tolPage: 20,
nowPage: 15,
//實現分頁顯示的
PageChange: function () {
var str = ''
for (var i = 1; i <= this.tolPage; i++) {
//第一種情況,後縮
if (this.nowPage < 6) {
if (i == 7) {
str += '<li>' + '...' + '</li>'
i = 19
}
else {
str += '<li class="btnList">' + i + '</li>'
}
}
//第二種情況,前縮
else if (this.nowPage > 15) {
if (i == 2) {
str += '<li>' + '...' + '</li>'
i = 14
}
else {
str += '<li class="btnList">' + i + '</li>'
}
}
//第三種情況,前後縮
else if (this.nowPage <= 15 && this.nowPage >= 6) {
//前縮
if (i == 2) {
str += '<li>' + '...' + '</li>'
i = this.nowPage - 2
}
//後縮
if (i == this.nowPage + 2) {
str += '<li>' + '...' + '</li>'
i = 19
}
//正常輸出
else {
str += '<li class="btnList">' + i + '</li>'
}
}
}
list.innerHTML = str
this.addBnt()
},
getBtnPage: function () {
for (var i = 0; i < btnList.length; i++) {
if (btnList[i].innerHTML == this.nowPage) {
btnList[i].classList.add('ck')
}
}
},
addBnt: function () {
that = this
for (var i = 0; i < btnList.length; i++) {
btnList[i].onclick = function () {
that.nowPage = parseInt(this.innerHTML)
that.PageChange()
that.getBtnPage()
}
}
},
}
window.onload = function () {
page.PageChange()
page.getBtnPage()
btn_next.onclick = function () {
if (page.nowPage <= page.tolPage) {
page.nowPage++
page.PageChange()
page.getBtnPage()
}
console.log(page.nowPage);
}
btn_top.onclick = function () {
if (page.nowPage > 1) {
page.nowPage--
page.PageChange()
page.getBtnPage()
}
console.log(page.nowPage);
}
btn.onclick = function () {
if (!isNaN(parseInt(page_input.value))) {
page.nowPage = page_input.value
page.PageChange()
page.getBtnPage()
page_input.value = ''
// isTo = false
}
}
}
</script>
</body>
</html>
3.資料瀑布流載入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 300px;
height: 250px;
}
</style>
</head>
<body>
<div class="conter">
</div>
<script>
var conter = document.getElementsByClassName('conter')[0]
var data = [
{ src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2005235653,1742582269&fm=26&gp=0.jpg' },
{ src: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1857771108,984160923&fm=26&gp=0.jpg' },
{ src: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2960427566,1786143065&fm=26&gp=0.jpg' },
{ src: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1731472669,4215674773&fm=26&gp=0.jpg' },
{ src: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1727914444,3729344997&fm=26&gp=0.jpg' },
{ src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2481321790,3127612340&fm=26&gp=0.jpg' },
{ src: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1262655631,4054846463&fm=26&gp=0.jpg' },
{ src: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=146893042,4244628931&fm=26&gp=0.jpg' },
{ src: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1522015854,2481348088&fm=26&gp=0.jpg' },
{ src: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3252747978,4060371130&fm=26&gp=0.jpg' },
]
console.log();
var dataLoad = {
//第一次載入圖片的數量
num: 4,
//控制載入圖片的數量
nowpage: 1,
//儲存載入圖片的圖片路徑
nowData: null,
//獲取圖片路徑到nowData
getData: function () {
this.nowData = data.slice((this.nowpage - 1) * this.num, this.nowpage * this.num)
this.load()
},
//更新頁面的方法
load: function () {
for (var i = 0; i < this.nowData.length; i++) {
var box = document.createElement('div')
var img = new Image()
img.src = this.nowData[i].src
box.appendChild(img)
conter.appendChild(box)
}
}
}
//剛載入頁面時的狀態
window.onload = function () {
dataLoad.getData()
console.log(dataLoad.nowData);
}
//頁面滑動時的變化載入更多圖片
document.body.onscroll = function () {
if (this.scrollY + this.innerHeight >= document.body.clientHeight - 1) {
dataLoad.nowpage++
dataLoad.getData()
}
}
</script>
</body>
</html>