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

相关推荐
时光书签36 分钟前
通过http地址下载文件
服务器·前端·c#·asp.net
专注VB编程开发20年1 小时前
如何保存网站CSS和JS中的图片?网页另存为本地显示不正常
前端·javascript·css
丶重明1 小时前
【2024】前端学习笔记9-内部样式表-外部导入样式表-类选择器
前端·笔记·学习
又写了一天BUG2 小时前
关于在vue2中给el-input等输入框的placeholder加样式
前端·javascript·vue.js
秋月霜风2 小时前
Web爬虫应用功能及需求设计
前端·爬虫·python
蜡笔小新星2 小时前
前端框架对比和选择
前端·javascript·vue.js·经验分享·学习·前端框架
LE...2 小时前
HTML元素居中
css·html·css3
一直在学习的小白~2 小时前
中间添加一条可以拖拽的分界线,来动态调整两个模块的宽度
前端·javascript·react.js
&木头人&3 小时前
layui upload.render 设置文件名
前端·javascript·layui
ZZZCY20033 小时前
路由基础--路由引入
服务器·前端·tcp/ip