class Index { constructor() { this.currentIndex = 0; this.allUserDo = [ { type: 1, title: '推荐', page: 1, content: [] }, { type: 2, title: '时令', page: 1, content: [] }, { type: 3, title: '食肉', page: 1, content: [] }, { type: 4, title: '素食', page: 1, content: [] }, { type: 5, title: '烘焙', page: 1, content: [] } ]; this.getBanner(); this.getType(); this.getthreeMeals(); this.getIndexClass(); } getIndexClass() { // 渲染大家都在做的标题和内容 this.allUserDo.forEach((v,i) => { $(".userdo").innerHTML += `
  • ${v.title}
  • `; $(".djdzz_father").innerHTML += `
    `; }); var a = $(".userdo li"), b = $(".djdzz_father .djdzz_list"); $("body").onscroll = () => { let scrollTop = document.documentElement.scrollTop || document.body.scrollTop; let clientHeight = document.documentElement.clientHeight || document.body.clientHeight; let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; if(scrollHeight > clientHeight && scrollTop + clientHeight === scrollHeight) { Array.from(b).filter((v,i)=>{ if (v.className.indexOf("active") > -1) { this.currentIndex = i; } }); this.allUserDo[this.currentIndex].page+=1; this.getdata("scroll"); console.log(this.currentIndex) } } // tabs 切换 a.forEach((v, i) => { v.onclick = async () => { this.currentIndex = i; a.forEach((item,index) => { item.className = ""; b[index].classList.remove("active") }); b[i].classList.add("active") v.className = "active"; await this.getdata(); } }); this.getdata(); } /** * 获取首页的分类数据 * @returns {Promise} */ async getdata(type = "click") { if (this.allUserDo[this.currentIndex].content.length == 0 || type == "scroll") { var res = await indexService.indexClass({ type: this.allUserDo[this.currentIndex].type, page: this.allUserDo[this.currentIndex].page }); this.allUserDo[this.currentIndex].content = [ ...this.allUserDo[this.currentIndex].content, ...res.items ] var currentF = $(".djdzz_father .djdzz_list")[this.currentIndex] // 把数据插入到对应的 djdzz_list 中 var a = this.allUserDo[this.currentIndex].content; currentF.innerHTML = ""; a.forEach((item,i) => { currentF.innerHTML += `
    ${ Array.isArray(item.img) ? `` : `` }
    ${item.title}
    ${ typeof item.label == "string" || item.label == null ? '' : `养
    ${item.label.desc}
    ` } ${ Array.isArray(item.author) ? '' : `
    ${item.author.nickname}
    ${item.viewed_amount}
    ` }
    ` }) } } /** * banner下面的九宫格数据 * @returns {Promise} */ async getType() { var res = await indexService.type(); res.forEach((v,i) => { $(".menu_type").innerHTML += `
  • ${v.title}

  • ` }) } /** * 每日三餐 * @returns {Promise} */ async getthreeMeals() { let res = await indexService.threeMeals(); // 渲染 每日三餐 标题 res.forEach((v,i) => { $(".meals_title").innerHTML += `
  • ${v.title}
  • ` }); res.forEach((v,i) => { $(".wrapper_list").innerHTML+=`
  • ${ (v.content.map((item, index) => { return `
    ${item.title}
    养
    ${item.des}
    ` })).toString().replace(/,/g,"") }
  • `; }); var titleLi = $(".three_title li"); var threeSwiper = new Swiper(".swiper_list",{ time: 1000, initialSlide: 0, loop: false, speed: 300, autoplay: false, grabCursor: true, swiperWrapper: '.wrapper_list', pagination: { el: '.pagination' }, on: { slideChange: function (index) { titleLi.forEach(v => v.className = ""); titleLi[index].className = "active" } }, }); titleLi.forEach((v,i) => { v.onclick = function () { threeSwiper.slideTo(i) } }) } /** * 获取banner数据 * @returns {Promise} */ async getBanner() { var res = await indexService.banner(); var bannerFather = $(".swiper-wrapper") res.forEach((v,i)=>{ bannerFather.innerHTML += `
  • ` }); // 初始banner轮播 new Swiper(".swiper",{ time: 1000, initialSlide: 0, loop: false, speed: 300, autoplay: false, grabCursor: true, }); } } new Index();