html中为div添加展开与收起功能(div折叠)

1、添加样式

html 复制代码
<style type="text/css">

    .mask {
        position: absolute;
        bottom: -5px;
        color: #4b83f0;
        font-weight: 700;
        font-size: 14px;
        text-align: center;
        height: 80px;
        left: 0;
        right: 0;
        background-image: -webkit-gradient(linear, left top, left bottom, from(hsla(0, 0%, 100%, 0)), to(#fff));
        background-image: linear-gradient(hsla(0, 0%, 100%, 0), #fff);
    }

        .mask.expand {
            background-image: none;
        }

        .mask span {
            position: absolute;
            bottom: 5px;
            left: 50%;
            margin-left: -25px;
            cursor: pointer;
        }

    .mask-fold {
        height: 300px;
    }

</style>

2、添加展开收起按钮div (i为图标,可自行修改)

html 复制代码
@* 为div添加maskDiv *@
<template id="template_div-mask">
    <div class="mask">
        <span>
            <i class="layui-icon layui-icon-down layui-font-12"></i> 展开
        </span>
    </div>
</template>

<template id="template_div-mask2">
    <div class="mask expand">
        <span>
            <i class="layui-icon layui-icon-up layui-font-12"></i> 收起
        </span>
    </div>
</template>

3、添加js代码响应逻辑(此处设置div高度超过300px时,显示收起展开)

javascript 复制代码
    /** 添加展开或折叠按钮 */
    function AppendMask() {

        // 为content-image自动判定,并添加mask
        $.each($(".content-image"), function (index, contentImage) {
            appendMaskItem(index, contentImage);
        });
    }

    /** 为contentImage添加mask的展开或折叠按钮 */
    function appendMaskItem(index, contentImage) {

        var newMakItem = $("#template_div-mask").html();

        var divHeight = contentImage.getClientRects()[0].height;
        if (divHeight > 300) {

            //debugger;
            $(contentImage.parentNode).append(newMakItem);
            $(contentImage).addClass("mask-fold");
        }
    }

    /** 点击mask的展开或折叠按钮 */
    $(document).on("click", ".mask>span", function () {

        //debugger;

        var maskDiv = $(this).parents(".mask");
        var isExpand = $(maskDiv).hasClass("expand");

        var newMakItem = $(isExpand ? "#template_div-mask" : "#template_div-mask2").html();

        var parentDiv = maskDiv[0].parentNode;
        $(parentDiv).append(newMakItem);
        $(maskDiv[0]).remove();

        var firstDiv = parentDiv.children[0];
        if (isExpand) {
            $(firstDiv).addClass("mask-fold");
        }
        else {
            $(firstDiv).removeClass("mask-fold");
        }
    });

4、在界面中的div元素显示完成后调用:

setTimeout(AppendMask, 5000);

相关推荐
夜郎king13 分钟前
HTML5 SVG 实现日出日落动画与实时天气可视化
前端·html5·svg 日出日落
夏幻灵1 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_1 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝2 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions2 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发2 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_2 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、2 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao2 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架