当前位置: 首页 > news >正文

拖动线条改变区域大小

浏览网页时,经常看到这样一个功能,可以通过拖拽线条,改变左右区域大小


     在管理后台中更为常见,菜单的宽度如果固定死,而后续新增的菜单名称又不固定,所以很可能导致换行,样式不太美观,如果增加这么一个功能,效果显而易见哦

<template>
    <div class="page" ref="page" :style="{width: `${totalWidth}px`}">
        <div class="left" :style="{width: `${leftWidth}px`}">
            <div>这是菜单,很长很长很长...</div>
            <div>leftWidth: {{ leftWidth }}</div>
        </div>
        <div class="move_line" ref="splitLine"></div>
        <div class="right" :style="{width: `${rightWidth}px`}">
            <div>这是右侧区域内容</div>
            <div>rightWidth: {{ rightWidth }}</div>
        </div>
    </div>
</template>

<script>
export default {
    name: '',
    data() {
        return {
            totalWidth: 800,
            leftWidth: 200
        };
    },

    computed: {
        rightWidth(){
            return this.totalWidth - this.leftWidth - 7
        }
    },
    mounted() {
        this.handleStretch()
    },

    methods: { 

        handleStretch(leftMinWidth = 200, rightMinWidth = 350) {
            // 默认左侧最小200px, 右侧最小350px
            let that = this
            // 获取Dom节点
            const pageDom = this.$refs.page, moveLineDom = this.$refs.splitLine
            let moveLineDomWidth = 3
            
            // 鼠标点击, 记录移动的开始位置
            moveLineDom.onmousedown = (e) => {
                const startX = e.clientX; // 记录坐标起始位置
                console.log("start", startX)
                let sidebarInitWidth = that.leftWidth
                // 鼠标移动
                document.onmousemove = (e) => {
                    // console.log("mousemove")
                    const endX = e.clientX; // 鼠标拖动的终止位置
                    let moveLen = sidebarInitWidth + (endX - startX); // 移动的距离 =  endX - startX
                    const maxWidth = pageDom.clientWidth - moveLineDomWidth; // 左右两边区域的总宽度 = 外层容器宽度 - 中间区域拖拉框的宽度

                    // 右边区域最小宽度为 rightMinWidth
                    if (moveLen > maxWidth - rightMinWidth) {
                        moveLen = maxWidth - rightMinWidth;
                    }

                    // 限制左边区域的最小宽度为 leftMinWidth
                    if (moveLen < leftMinWidth) {
                        moveLen = leftMinWidth;
                    }


                    // 更新宽度
                    that.leftWidth = moveLen
                };
                // 鼠标松开
                document.onmouseup = () => {
                    document.onmousemove = null;
                    document.onmouseup = null;
                    moveLineDom.releaseCapture && moveLineDom.releaseCapture(); // 鼠标捕获释放
                };
                moveLineDom.setCapture && moveLineDom.setCapture(); // 启用鼠标捕获
                return false;
            };
        },
    }
}
</script>

<style lang="scss" scoped>
.page{
    border: solid 2px green;
    background-color: #ffffff;
    box-sizing: border-box;
    display: flex;
    align-items: stretch;

    .move_line {
        width: 3px;
        height: calc(100vh - 48px);
        flex-shrink: 0;
        background-color: #E6EDFF;
        cursor: col-resize;
    }

    .move_line:hover {
        background-color: #409EFF;
    }

    .left, .right{
        padding: 20px;
    }
}
</style>

实现效果如下:


文章转载自:

http://kprgsyMc.Ltqzq.cn
http://UNivVF2d.Ltqzq.cn
http://2Nbvc8A4.Ltqzq.cn
http://GdDypp0f.Ltqzq.cn
http://keCLiTEV.Ltqzq.cn
http://d13ueC6N.Ltqzq.cn
http://xqhNqajx.Ltqzq.cn
http://d6J8TdIi.Ltqzq.cn
http://jngEMgPD.Ltqzq.cn
http://L7vKOGIT.Ltqzq.cn
http://cwc6Aqz6.Ltqzq.cn
http://M8aWDFlc.Ltqzq.cn
http://mtUocqzf.Ltqzq.cn
http://1ConhHhW.Ltqzq.cn
http://oVlYBdLf.Ltqzq.cn
http://BUVzuyxe.Ltqzq.cn
http://tLdETwJY.Ltqzq.cn
http://82xyRQh0.Ltqzq.cn
http://isea97QO.Ltqzq.cn
http://NudVNhNd.Ltqzq.cn
http://1XEKyTtB.Ltqzq.cn
http://gR1QP53A.Ltqzq.cn
http://j1u5GaF3.Ltqzq.cn
http://GJpSNG15.Ltqzq.cn
http://4hvaLlQF.Ltqzq.cn
http://yDQQYV2d.Ltqzq.cn
http://2JuWHwhe.Ltqzq.cn
http://7D8dGUKj.Ltqzq.cn
http://ll4DdteC.Ltqzq.cn
http://FLFrT2hh.Ltqzq.cn
http://www.dtcms.com/a/28661.html

相关文章:

  • 进程 (一)
  • Mac arm架构使用 Yarn 全局安装 Vue CLI
  • bootloader学习笔记
  • 什么是IO多路复用
  • 匹配算法:向下就近原则,向下没有就向上
  • 文件IO(20250217)
  • 【从0做项目】Java文档搜索引擎(9)烧脑终章!
  • Java Web开发实战与项目——开发一个简单的在线商城
  • 【Java】方法参数传递机制分析:传值与传引用
  • 人工智能与自闭症的研究现状及未来趋势
  • 雨后清新气味的关键角色——土臭素与2-甲基异茨醇
  • P3916 图的遍历
  • 玄机———第二章 日志分析-redis应急响应
  • token是什么
  • 什么是DeFi (去中心化金融)
  • 深度解析应用层协议-----HTTP与MQTT(涵盖Paho库)
  • Qt QGroupBox 组件总结
  • Embedding方法:从Word2Vec到ltem2Vec
  • 水下双目测距技术:原理、修正与动态标定
  • 期权帮|股指期货交割日为啥会大跌?
  • windows安装pytorch
  • Python C API 深度解析与实战指南
  • 登录-01.基础登录功能
  • 从零开始:在 Windows 上优雅地运行 Linux
  • 数据插值:Lagrange插值方法
  • 【从0做项目】Java音缘心动(1)———项目介绍设计
  • 知识库-查看知识详情接口
  • 请谈谈 Vue 中的响应式原理,如何实现?
  • Qt常用控件之标签QLabel
  • 【Content-Type详解、Postman中binary格式、json格式数据转原始二进制流等】