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">

相关推荐
华科云商xiao徐21 分钟前
用TypeScript和got库编写爬虫程序指南
前端
難釋懷26 分钟前
JavaScript基础-navigator 对象
前端·javascript
WebInfra27 分钟前
Midscene v0.14 - 新增即时操作接口和深度思考
前端·人工智能·测试
windyrain31 分钟前
用了Trae CN,原来实现从PDF提取页面如此简单
前端·react.js·trae
前端Hardy40 分钟前
HTML&CSS:音乐播放器卡片
javascript·css·html
前端Hardy43 分钟前
HTML&CSS:有个性的复选框
javascript·css·html
崽子爱码1 小时前
HTML静态网页成品作业(HTML+CSS)——阜阳剪纸介绍设计制作(1个页面)
前端·css·html
猿榜1 小时前
一文带你了解并发编程:线程、进程与协程
javascript·python
辛-夷1 小时前
正则表达式(一)
前端·javascript·正则表达式
12码力1 小时前
Vue 3 + Canvas 实现图形拖拽、缩放、旋转
前端