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

相关推荐
跟橙姐学代码2 小时前
Python异常处理:告别程序崩溃,让代码更优雅!
前端·python·ipython
niuhuahua2 小时前
大屏拖拽功能,配合ai组件使用,配合各个组件都可使用
前端
得物技术2 小时前
前端日志回捞系统的性能优化实践|得物技术
前端·javascript·性能优化
ZKshun2 小时前
[ 前端JavaScript的事件流机制 ] - 事件捕获、冒泡及委托原理
javascript
陶甜也2 小时前
threeJS 实现开花的效果
前端·vue·blender·threejs
用户7678797737322 小时前
后端转全栈之Next.js 路由系统App Router
前端·next.js
OEC小胖胖2 小时前
Next.js数据获取入门:`getStaticProps` 与 `getServerSideProps`
前端·前端框架·web·next.js
薛定谔的算法2 小时前
JavaScript栈的实现与应用:从基础到实战
前端·javascript·算法
深圳外环高速2 小时前
React 受控组件如何模拟用户输入
前端·react.js