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

相关推荐
zwjapple20 分钟前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
AA-代码批发V哥3 小时前
HTML之语义化标签与多媒体标签
html
像风一样自由20203 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem3 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊3 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术3 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
GISer_Jing3 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止4 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall4 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴4 小时前
简单入门Python装饰器
前端·python