原生JS实现聊天窗口的输入框点击边框实现上下拖动改变输入框和聊天记录列表大小高度

见图
在这里插入图片描述
在这里插入图片描述
上代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            *{
                margin:0;
                padding:0;
                box-sizing: border-box;
            }
            .out-box{
                width:500px;
                height:450px;
                background:#fff;
                position:absolute;
                top:100px;
                left:100px;
                border:1px solid #000000;
            }
            .out-box>div{
                width:100%;
            }
            .top-box{
                height:calc(100% - 200px);
                overflow-y:scroll;
                background:#fff;
            }
            ul,ul li{
            }
            .move-line{
                width:100%;
                height:2px;
                position:absolute;
                bottom:200px;
                border:0;
                background:#f0f;
                cursor: ns-resize;
            }
            .bottom-box{
                background:#fff;
                height:200px;
                text-align:center;
                color:#ffffff;
            }
            .input-box{
                height:80%;
            }
            .btn-box{
                height:20%;
                display:flex;
                justify-content: flex-end;
                align-items: center;
                border-top:1px solid #000000;
            }
            .input-box #input{
                width:100%;
                height:100%;
                outline:none;
                border:none;
                padding:5px;
            }
            .btn-box .send-btn{
                outline: none;
                border:none;
                background: gold;
                padding:5px 15px;
                border-radius:5px;
                margin-right:15px;
            }
    </style>
    </head>
    <body>
        <div class="out-box" id="outBox">
            <div class="top-box" id="topBox">
                <ul>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                    <li>阿斯蒂芬解开了士大夫</li>
                </ul>
                <li>阿斯蒂芬解开了士大夫</li>
            </div>
            <hr id="line" class="move-line">
            <div class="bottom-box" id="botBox">
                <div class="input-box">
                    <textarea name="" id="input" cols="30" rows="10" placeholder="请输入内容"></textarea>
                </div>
                <div class="btn-box">
                    <button class="send-btn">发送</button>
                </div>
            </div>
        </div>
        <script>
            let moveFlag = false,
            lineObj = document.getElementById('line'),
            outObj = document.getElementById('outBox'),
            topObj = document.getElementById('topBox'),
            botBox = document.getElementById('botBox');
            let boxH = parseInt(getStyle(outObj,'height'));
            let maxH = 250,minH = 150;
            outObj.addEventListener("mousedown",function(e){
                if(e.target.id === 'line'){
                    moveFlag = true;
                }
            })
            outObj.addEventListener("mousemove",function(e){
                if(moveFlag){
                    let outY = outObj.getBoundingClientRect().y;
                    let y = e.clientY;
                    let bot = outY + boxH - y;
                    if(bot >minH && bot <maxH){
                        lineObj.style.bottom = bot+'px';
                        botBox.style.height = bot+'px';
                        topObj.style.height = `calc(100% - ${bot}px)`;
                    }
                }
            })
            document.addEventListener('mouseup',function(){
                moveFlag = false;
            })
            function getStyle(obj, attr) {
                if (obj.currentStyle) {
                    return obj.currentStyle[attr];
                } else {
                    return window.getComputedStyle(obj)[attr];
                }
            }
        </script>
    </body>
</html>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值