html ul li 首页渲染多条数据 但只展示八条,其余的数据全部隐藏,通过icon图标 进行展示

<div style="float: left;" id="showMore"> 展开 </div>

<div style="float: left;"id="hideLess"> 收起 </div>

var data = document.querySelectorAll('.allbox .item h3 a');

const list = document.querySelectorAll('.all-sort-list .item h3 a');

data.forEach((item, index) => {

if (index === 7) {

document.querySelector("#showMore").style.display = "block"

}

});

var listItems = document.querySelectorAll('.all-sort-list .item');

if (listItems.length > 8) {

for (var i = 8; i < listItems.length; i++) {

listItems[i].style.display = 'none';

}

document.getElementById('showMore').style.display = 'block';

document.getElementById('hideLess').style.display = 'none';

}

// 展开

document.getElementById('showMore').addEventListener('click', function () {

var listItems = document.querySelectorAll('.allbox .item');

if (listItems.length > 8) {

for (var i = 8; i < listItems.length; i++) {

listItems[i].style.display = 'block';

listItems[i].className+= " wrap_item";

}

document.getElementById('showMore').style.display = 'none';

document.getElementById('hideLess').style.display = 'block';

}

});

// 收起

document.getElementById('hideLess').addEventListener('click', function () {

var listItems = document.querySelectorAll('.all-sort-list .item');

if (listItems.length > 8) {

for (var i = 8; i < listItems.length; i++) {

listItems[i].style.display = 'none';

}

document.getElementById('showMore').style.display = 'block';

document.getElementById('hideLess').style.display = 'none';

}

});

.wrap1 {

bottom: 0px;

left: 0px;

float: left;

position: relative;

z-index: 3;

width: 198px;

height: 100%;

background: #2a86dd;

}

.allbox {

position: relative;

width: 195px;

border: 1px solid #2a86dd;

border-top: none;

padding: 1px;

}

#showMore {

display: inline-block;

width: 20px;

height: 20px;

color: #fff;

display: none;

}

#hideLess{

display: inline-block;

width: 20px;

height: 20px;

color: #fff;

display: none;

}

.item:hover #showMore {

color: #f00;

}

.item:hover #hideLess {

color: #f00;

}

.wrap_item{

width: 200px;

height: 45px;

line-height: 45px;

background-color: #09f;

color: #fff;

}

数据结构

<div class="boxCur">

<div class="wrap1">

<div class="allbox">

<div class="item bo">

相关推荐
Justin3go4 分钟前
丢掉沉重的记忆:Codex、Claude Code 与 OpenCode 的上下文压缩术
前端·后端·架构
JokerLee...7 分钟前
大屏自适应方案
前端·vue.js·大屏端
dyb-dev25 分钟前
我是如何学习 NestJS 的
前端·nestjs·全栈
kyriewen40 分钟前
重排、重绘、合成:浏览器渲染的“三兄弟”,你惹不起也躲不过
前端·javascript·浏览器
NickJiangDev1 小时前
Elpis-Core 技术解析:从零构建一个基于 Koa 的企业级 Node.js 框架内核
前端
我要让全世界知道我很低调1 小时前
来聊聊 Codex 高效编程的正确姿势
前端·程序员
NickJiangDev1 小时前
Elpis Webpack 工程化实战:Vue 多页应用的构建体系搭建
前端
米饭同学i1 小时前
GitLab CI/CD + Vue 前端 完整方案
前端
yuki_uix1 小时前
遇到前端题目,我现在会先问自己这四个问题
前端·面试
Wect1 小时前
JS 手撕:对象创建、继承全解析
前端·javascript·面试