class Index {
constructor() {
footer()
this.classList = [
{ title: '推荐', type: 1, page: 1, total_page: 0, content: [] },
{ title: '时令', type: 2, page: 1, total_page: 0, content: [] },
{ title: '食肉', type: 3, page: 1, total_page: 0, content: [] },
{ title: '素食', type: 4, page: 1, total_page: 0, content: [] },
{ title: '烘焙', type: 5, page: 1, total_page: 0, content: [] },
];
this.currentIndex = 0;
this.isLoadMore = false; // false 不滚动加载,true 滚动加载
this.getThreeMeals();
this.getBanner();
this.renderTitle();
// 滚动加载更多数据
$("body").onscroll = () => {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
scrollTop = Math.ceil(scrollTop)
let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
if (scrollHeight > clientHeight && scrollTop + clientHeight == scrollHeight) {
console.log("滚动到底部了: ", this.isLoadMore)
if (this.isLoadMore) {
this.classList[this.currentIndex].page += 1;
console.log(this.classList[this.currentIndex])
this.getIndexType("more")
}
}
}
}
initSwiper() {
new swiper('.swiper-container',{
})
}
async getBanner() {
let res = await indexServe.banner()
res.forEach(v => {
$(".swiper-wrapper").innerHTML += `
`
});
this.initSwiper()
}
async getThreeMeals() {
let res = await indexServe.threeMeals()
res.forEach((v,i) => {
$(".three .title_li").innerHTML += `${v.title}`;
$(".wrapper").innerHTML += `
${
v.content.map(item => {
return `
-
${item.title}
`
}).toString().replaceAll(",","")
}
`
})
var liArr = _(".three .title_li li")
var sw = new swiper('.ddddddd',{
wrapper: '.wrapper',
on: {
slideChangeTransitionStart(index) {
liArr.forEach(v => v.classList.remove("active"))
liArr[index].classList.add("active")
}
}
});
liArr.forEach((v,i) => {
v.onclick = function () {
liArr.forEach(v => v.classList.remove("active"))
this.classList.add("active")
sw.slideTo(i)
}
})
}
// type click 点击加载数据,需要做判断
// type more 滚动加载,不需要
async getIndexType(type = "click") {
var getData = async () => {
console.log("======== type: ", type)
let res = await indexServe.type({
page: this.classList[this.currentIndex].page,
type: this.classList[this.currentIndex].type
});
this.classList[this.currentIndex].total_page = res.total_page;
this.classList[this.currentIndex].content = [
...this.classList[this.currentIndex].content,
...res.items
]
if (type == "click") {
this.isLoadMore = true
}
var ul = _(".alldo_father ul")
ul[this.currentIndex].innerHTML = ""
this.classList[this.currentIndex].content.forEach((v, i) => {
ul[this.currentIndex].innerHTML += `
${v.title}
养
${
v.label == null
? ''
: v.label.length == 0
? ''
: `
${v.label.desc}
`
}
${
v.author.length == undefined
? `
${v.author.nickname}
${v.total_amount}
`
: ''
}
`
})
}
if (type == "click") {
if (this.classList[this.currentIndex].content.length == 0) {
getData()
}
} else {
getData()
}
}
async renderTitle() {
this.classList.forEach((v,i) => {
$(".alldo .title_li").innerHTML += `${v.title}`;
$(".alldo_father").innerHTML += `
`
});
this.getIndexType()
var ul = _(".alldo_father ul")
var li = _(".alldo .title_li li")
li.forEach((v,i) => {
v.onclick = () => {
li.forEach((item,index) => {
item.classList.remove("active")
ul[index].classList.remove("f_active")
})
ul[i].classList.add("f_active")
v.classList.add("active")
this.isLoadMore = false
console.log("你点击了:",this.isLoadMore)
this.currentIndex = i
this.getIndexType()
}
})
}
}
new Index();