js拖动改变区域大小

先看效果:(拖动蓝色线可改变宽度或者高度)

CSS:

*{margin: 0;padding: 0;}
body,html{width: 100%; height: 100%;background-color: #d2d0d0;}
.line{position: absolute;background-color: rgba(0, 191, 255, 0.3);}
.line[data-direction = "y"]{
    width: 100%;
    height: 7px;
    top: calc(100% - 3px);
    right: 0;
    cursor: n-resize;}
.line[data-direction = "x"]{
    width: 7px;
    height: 100%;
    left: calc(100% - 3px);
    top: 0;
    cursor: e-resize;}
.container{
    width: 1200px;
    height: calc(100% - 2px);
    border: 1px solid #f8f8f8;
    margin: auto;}
.hearder{
    height: 60px;
    border-bottom: 1px solid #f8f8f8;
    position: relative;}
.main{display: flex;height: calc(100% - 60px);}
.left{
    width: 300px;
    height: 100%;
    border-right: 1px solid #f8f8f8;
    position: relative;}
.right{flex: 1;}

HTML结构:

<div class="container">
    <div class="hearder">
        <div class = "line" data-direction = "y" data-according = "header"></div>
    </div>
    <div class="main">
        <div class="left">
            <div class = "line" data-direction = "x" data-according = "left"></div>
        </div>
        <div class="right">
            <div class="hearder">
                <div class = "line" data-direction = "y" data-according = "header1"></div>
            </div>
            <div class="left">
                <div class = "line" data-direction = "x" data-according = "left1"></div>
            </div>
        </div>
    </div>
</div>

drag.js:

function Drag(option) {
    this.option =option,
    this.dir = function (i) {
        let _this = this;
        i.onmousedown = function (e) {
            e = e || window.event;
            let direction = this.getAttribute(_this.option.direct);
            let according = this.getAttribute(_this.option.limitValue);
            let dis = 0;
            if(direction == 'x'){
                dis = e.clientX - this.offsetLeft;
            }else if(direction == 'y'){
                dis = e.clientY - this.offsetTop;
            }
            let that = this;
            document.onmousemove = function (e) {
                e = e || window.event;
                if(direction == 'x'){
                    let dis_x = e.clientX - dis;
                    dis_x = dis_x > _this.option.height.max[according] ? _this.option.height.max[according]: (dis_x < _this.option.height.min[according] ? _this.option.height.min[according]: dis_x);
                    that.parentNode.style.width = dis_x + "px";
                }else if(direction == 'y'){
                    let dis_y = e.clientY - dis;
                    dis_y = dis_y > _this.option.width.max[according] ? _this.option.width.max[according]: (dis_y < _this.option.width.min[according] ? _this.option.width.min[according]: dis_y);
                    that.parentNode.style.height =  dis_y + "px";
                    if(that.parentNode.nextElementSibling){
                        that.parentNode.nextElementSibling.style.height = "calc(100% - " + dis_y + "px)";
                    }
                }
            }
            document.onmouseup = function () {
                document.onmousemove = document.onmousedown = null;
            }
        }
    }
}

调用以及配置:

let option = {
     /* 规定可拖动元素的自定义属性
     * direct:规定拖动方向的:x(调整宽度) || y(调整高度),
     * limitValue:每个可拖动元素类似id的存在,在width/height.max/min下以此属性值做名可设置最大/小 的 宽/高
     */
     direct:"data-direction",
     limitValue: "data-according",
     width:{
         max:{
             header:70,/* data-according属性值为header的最大宽度 */
             header1:140/* data-according属性值为header1的最大宽度 */
         },
         min:{
             header:50,/* data-according属性值为header的最小宽度 */
             header1:50/* data-according属性值为header1的最小宽度 */
         }
     },
     height:{
         max:{
             left:450,/* data-according属性值为left的最大高度 */
             left1:400/* data-according属性值为left1的最大高度 */
         },
         min:{
             left: 150,/* data-according属性值为left的最小高度 */
             left1:200/* data-according属性值为left1的最小高度 */
         }
     }
 }
 /*
  *   拖拽改变区域
  */
 let dragging = new Drag(option);
 let line = document.getElementsByClassName('line');
 for(let i =0;i<line.length;i++){
     dragging.dir(line[i]);
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Bootstrap Table 的扩展插件 `bootstrap-table-sticky-header` 来实现表头固定,使用 `bootstrap-table-resizable` 插件来实现列宽可拖动改变宽度大小。 首先,需要在页面中引入 `bootstrap-table-sticky-header.css` 和 `bootstrap-table-sticky-header.js` 插件文件以及 `bootstrap-table-resizable.js` 插件文件。例如: ```html <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table-sticky-header/dist/bootstrap-table-sticky-header.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap-table-sticky-header/dist/bootstrap-table-sticky-header.js"></script> <script src="https://rawgit.com/wenzhixin/bootstrap-table-resizable/master/src/bootstrap-table-resizable.js"></script> ``` 然后,在表格初始化时,需要将 `stickyHeader` 和 `resizable` 选项设置为 `true`,如下所示: ```javascript $('#table').bootstrapTable({ stickyHeader: true, // 开启表头固定 resizable: true, // 开启列宽可拖动改变 columns: [ // 列配置 ], data: [ // 数据 ] }); ``` 这样就可以在表格中实现表头固定和列宽可拖动改变的效果了。 ### 回答2: Bootstrap table 是一款基于Bootstrap框架的表格插件,可以用于展示数据和操作表格。要实现表头固定和列宽可拖动改变宽度大小,可以使用Bootstras Table的一些功能和插件。 首先,为了实现表头固定,可以使用Bootstrap Table的fixedHeader插件。这个插件可以将表格的表头固定在顶部,即使表格有滚动条也能保持表头可见。可以通过添加fixedHeader: true的属性来启用这个功能。 其次,为了实现列宽可拖动改变宽度大小,可以使用Resizable插件。这个插件可以让列的宽度可调整,用户可以拖动列的边界改变宽度大小。可以通过添加resizable: true的属性来启用这个功能。 示例代码如下: ```html <table id="myTable" data-resizable="true" data-fixed-header="true"> <thead> <tr> <th data-field="id">ID</th> <th data-field="name">Name</th> <th data-field="age">Age</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td>25</td> </tr> <tr> <td>2</td> <td>Jane</td> <td>30</td> </tr> </tbody> </table> <script> $(document).ready(function() { $('#myTable').bootstrapTable({ fixedHeader: true, resizable: true }); }); </script> ``` 上述示例代码中,我们创建了一个带有ID、Name和Age三列的表格,并启用了表头固定和列宽可调整功能。用户可以拖动列边界来改变列的宽度大小,而表头会一直保持在表格的顶部可见。 希望这个回答对您有帮助! ### 回答3: Bootstrap table是一种用于创建漂亮且响应式数据表格的开源前端框架。要实现表头固定和列宽可拖动改变宽度大小的功能,可以使用Bootstrap table的相关插件。以下是实现的步骤: 1. 表头固定:使用Bootstrap table的fixedHeader插件。这个插件可以使表格的表头在滚动时固定在页面顶部。只需要在表格初始化时启用该插件即可。 2. 列宽可拖动改变大小:使用Bootstrap table的resizable插件。这个插件可以让列的宽度可以通过拖动改变。首先,需要在表格初始化时启用该插件。然后,在表头中的每个列中添加一个可拖动区域,例如使用col-resizable类。最后,使用CSS样式指定这个可拖动区域的样式和大小。 综上所述,要实现Bootstrap table的表头固定和列宽可拖动改变大小的功能,可以使用fixedHeader和resizable两个插件。这些插件能够使表格更加灵活和易于使用,并提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值