div自由拉伸

539 阅读1分钟

ezgif-5-da55a867ae.gif

<div class="container">
    <div class="left">
        <!-- 拉伸的工具 -->
        <div class="resize-bar"></div>
        <!-- 内容区域 -->
        <div class="content">content</div>
    </div>
    <div class="right">123</div>
</div>
.container{
    display: flex;
    height: 100vh;
    user-select: none;
    margin-left: 20px;
}

.right{
    background-color: #ff6b81;
    flex: 1;
}

.left{
    position: relative;
    height: inherit;
}

.resize-bar{
    width: 200px;
    min-width: 100px;//限制最小拉伸
    height: inherit;
    resize: horizontal;//宽度拉伸,要设置overflow
    cursor: w-resize;
    overflow: scroll;
    opacity: 0;
}

.resize-bar::-webkit-scrollbar{
    width: 10px;
    height: inherit;
}

.resize-bar:hover ~ .content{
    border-right: 1px dashed red;
}

.content{
    position: absolute;
    top: 0;
    right: 5px;//让resize-bar的scrollbar露出来
    width: 100%;
    height: inherit;
    background-color: #00cec9;
    padding: 10px;
    box-shadow: border-box;
    border-right: 1px solid #999;
}

用resize-bar元素撑开父元素,content定位到父元素身上盖住resize-bar,content设置right:5px让resize-bar的scrollbar露出来。把resize-bar的scrollbar高度设为100就能让原本一小块的拉伸区域变成整个右边5px拉伸区域