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);

相关推荐
之歆9 分钟前
DAY13_CSS3进阶完全指南 —— 背景、边框、文本、渐变、滤镜与 Web 字体(下)
前端·css·css3
剑神一笑26 分钟前
CSS 阴影生成器:从单层到多层叠加的艺术
前端·css·css3
lljss20201 小时前
1. NameServer 域名服务器---NS
linux·服务器·前端
anOnion1 小时前
构建无障碍组件之Tooltip Pattern
前端·html·交互设计
陈随易1 小时前
为什么今天还会有新语言?MoonBit 想解决什么问题?
前端·后端·程序员
西洼工作室1 小时前
unipp+vue3+python h5+app极验验证码集成全流程解析
前端·uni-app·全栈·极验
ZC跨境爬虫1 小时前
跟着 MDN 学 HTML day_15:(媒体缓冲、拖动与时间范围控制)
前端·笔记·ui·html·edge浏览器·媒体
李白的天不白2 小时前
webpack 与 webpack-cli 版本匹配问题
前端·webpack·node.js
tool2 小时前
Hermes Agent 从安装到生产:我的完整踩坑记录
前端