前言

本例是在React中实现,不过改一改通过原生js也很好实现,另外兼容性也做到了IE9。(IE8讲道理也是可以的)。

首先看一下需要实现的需求:

要拖动图中的白色横条调整绿色和蓝色区域的高度,要拖动白色竖条调整左边区域和红色区域的宽度。

一两年前曾经遇到过这个需求,当时直接在网上搜了个解决方案贴上去了,不过那个解决方案很挫。

这次的项目又遇到这个需求,而且是三个块的拖动。不仅需要左右拖动还需要上下拖动。

在这里特地记录下解决方案,也希望可以得到一些反馈与优化。

方案的思路

横条拖动和竖条拖动原理差不多,那就先来实现竖条左右拖动调整宽度。

水平方向的布局是通过以下方式实现:

.left{
width: 500px;
height: 100%;
float: left;
position: relative;
} .v-resize{
height: 100%;
width: 4px;
position: absolute;
background: #fff;
left: 500px;
z-index: 2;
cursor: col-resize;
user-select: none;
} .right{
margin-left: 504px;
background-color: lightsalmon;
height: 100%;
}

通过样式我们可以了解到,只要同时改变left块的宽度,白色竖条v-resize的left定位和right块的定位(这个数相差不大,我们将这个数定义为vNum),即可实现水平拖动的效果。

通过鼠标按下白色竖条,开启水平拖动,监控鼠标位置,计算vNum,在鼠标放开或者鼠标移出拖动区域时停止水平拖动。

计算vNum的本质实际上就是通过鼠标位置减去拖动区域离浏览器左侧位置,从而得到vNum。

因为每块区域都有最大和最小宽度的限制,这里仅仅加了个vNumLimit来进行限制,即竖条最少要离最左侧和最右侧的距离。

这里还要考虑到每次窗口resize时,各个参数可能都会有所调整,所以加了窗口resize的处理,当然也加了防抖。

本来想要分步讲解,但是冬寒人乏,懒病发作。

方案的实现

jsx部分

import React, { Component } from 'react'
import _ from 'underscore'
import styles from './index.css' // 可调整宽高的Div
export default class ResizeDiv extends Component {
state = {
isHResize: false,
isVResize: false,
hNum: 100,
vNum: 500,
hNumLimit: 30,
vNumLimit: 30
} resizeOffsetInfo = {
clientTop: 0,
clientLeft: 0
} leftHeight = 0 containerWidth = 0 componentDidMount() {
this.initResizeInfo()
const throttled = _.throttle(() => {
this.initResizeInfo()
}, 200) window.onresize = throttled
}
componentWillUnmount() {
window.onresize = null
} /**
* 初始化resize信息
*/
initResizeInfo = () => {
const hEle = document.getElementById('h_resize_container')
this.resizeOffsetInfo = this.getEleOffset(hEle)
this.leftHeight = hEle.offsetHeight
this.containerWidth = document.getElementById('v_resize_container').offsetWidth
} /**
* 获取元素的偏移信息
*/
getEleOffset(ele) {
var clientTop = ele.offsetTop
var clientLeft = ele.offsetLeft
let current = ele.offsetParent
while (current !== null) {
clientTop += current.offsetTop
clientLeft += current.offsetLeft
current = current.offsetParent
}
return {
clientTop,
clientLeft,
height: ele.offsetHeight,
width: ele.offsetWidth
}
} /**
* 开始拖动水平调整块
*/
hResizeDown = () => {
this.setState({
isHResize: true
})
} /**
* 拖动水平调整块
*/
hResizeOver = (e) => {
const { isHResize, hNum, hNumLimit } = this.state
if (isHResize && hNum >= hNumLimit && (this.resizeOffsetInfo.height - hNum >= hNumLimit)) {
let newValue = this.resizeOffsetInfo.clientTop + this.resizeOffsetInfo.height - e.clientY
if (newValue < hNumLimit) {
newValue = hNumLimit
}
if (newValue > this.resizeOffsetInfo.height - hNumLimit) {
newValue = this.resizeOffsetInfo.height - hNumLimit
}
this.setState({
hNum: newValue
})
}
} /**
* 开始拖动垂直调整块
*/
vResizeDown = () => {
this.setState({
isVResize: true
})
} /**
* 拖动垂直调整块
*/
vResizeOver = (e) => {
const { isVResize, vNum, vNumLimit } = this.state
if (isVResize && vNum >= vNumLimit && (this.containerWidth - vNum >= vNumLimit)) {
let newValue = e.clientX - this.resizeOffsetInfo.clientLeft
if (newValue < vNumLimit) {
newValue = vNumLimit
}
if (newValue > this.containerWidth - vNumLimit) {
newValue = this.containerWidth - vNumLimit
}
this.setState({
vNum: newValue
})
}
} /**
* 只要鼠标松开或者离开区域,那么就停止resize
*/
stopResize = () => {
this.setState({
isHResize: false,
isVResize: false
})
} render() {
const hCursor = this.state.isHResize ? 'row-resize' : 'default'
const hColor = this.state.isHResize ? '#ddd' : '#fff'
const vCursor = this.state.isVResize ? 'col-resize' : 'default'
const vColor = this.state.isVResize ? '#ddd' : '#fff' return (
<div className={styles['container']} onMouseUp={this.stopResize} onMouseLeave={this.stopResize}>
<div id='v_resize_container' className={styles['content']} onMouseMove={this.vResizeOver}>
<div id='h_resize_container' style={{ width: this.state.vNum, cursor: vCursor }} className={styles['left']}
onMouseMove={this.hResizeOver}>
<div style={{ bottom: this.state.hNum, cursor: hCursor }} className={styles['left-top']}>aasd</div>
<div style={{ bottom: this.state.hNum, backgroundColor: hColor }} draggable={false} onMouseDown={this.hResizeDown} className={styles['h-resize']} />
<div style={{ height: this.state.hNum + 4, cursor: hCursor }} className={styles['left-bottom']}>asd</div>
</div>
<div style={{ left: this.state.vNum, backgroundColor: vColor }} draggable={false} onMouseDown={this.vResizeDown} className={styles['v-resize']} />
<div style={{ marginLeft: this.state.vNum + 4, cursor: vCursor }} className={styles['right']}>
asdas
</div>
</div>
</div>
)
}
}

css部分

.container{
margin: 30px;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
} .content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
min-height: 300px;
} .left{
width: 500px;
height: 100%;
float: left;
position: relative;
} .left-top{
position: absolute;
top: 0;
bottom: 104px;
width: 100%;
background-color: lightblue;
} .h-resize{
height: 4px;
width: 100%;
background: #fff;
position: absolute;
bottom: 100px;
z-index: 1;
cursor: row-resize;
user-select: none;
} .left-bottom{
position: absolute;
bottom: 0;
width: 100%;
height: 100px;
background-color: lightgreen;
} .v-resize{
height: 100%;
width: 4px;
position: absolute;
background: #fff;
left: 500px;
z-index: 2;
cursor: col-resize;
user-select: none;
} .right{
margin-left: 504px;
background-color: lightsalmon;
height: 100%;
}

总结

技术上其实还是比较简单的,不过丝般润滑的左右移动还是挺有成就感的。

如果有更好的玩法还望不吝赐教。

这是这个demo的地址:demo地址

实现可调整宽高的DIV(左右拖动和上下拖动)的更多相关文章

  1. 不定宽高的DIV,垂直水平居中

    1.怎么让一个不定宽高的DIV,垂直水平居中? 答:1)使用CSS方法. 父盒子设置: display:table-cell; text-align:center; vertical-align:mi ...

  2. 固定宽高的DIV绝对居中示例

    看了一些代码,然后自己试验了一番,分享如下示例: 实现点: 如果元素的宽高固定,那么,css指定样式为top:50%;left:50%; 而margin-top和 margin-left 指定为负数, ...

  3. css实现不定宽高的div水平、垂直居中

    一共有三个方案: 1,第一种方案主要使用了css3中transform进行元素偏移,效果非常好 这方法功能很强大,也比较灵活,不仅仅局限在实现居中显示.  兼容方面也一样拿IE来做比较,第二种方法IE ...

  4. 已知宽高和未知宽高的div块的水平垂直居中

    //已知宽高的情况 .div1_container{     border:1px solid #00ee00;     height:300px;     position:relative; } ...

  5. 怎么让一个不定宽高的div垂直水平居中?

    方法一:使用CSS3 transform 父盒子设置:position:relative; div设置:position:absolute;transform:translate(-50%,-50%) ...

  6. handsontable组件和jqwidgets(jqxdragdrop组件)在一个页面产生调整宽高bug

    修改handsontable.full.js handsontable绑定的"mouseup"事件,默认是window区域太大.引起冲突.

  7. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

  8. css实现div不定宽高垂直水平居中解决方案

    在项目中我们经常能碰见然图片垂直水平居中,不定宽高的div垂直水平居中,等等~~ 现在我将介绍我所知道的几种用css来解决的几种方案. 1.父元素text-align:center;display:t ...

  9. iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;

    最终效果图: 一.轮播图中图片自适应宽高:  <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...

随机推荐

  1. php 接口与前端数据交互实现

    最近在做前后端数据交互的尝试,也跳了很多坑,使用的是php+bootstrap-table+js,把一些收获记录在这里,方便查询. 这个小项目,仅有3个文件,分别为: crud.html data.p ...

  2. 复杂的字符串数组解析:{"setting":"简单:10:5,一般:5:10,困难:2:20"},使用split多次截取

    "[0,{"id":563,"name":"测试题1","dscr":null,"picId&quo ...

  3. 使用Visual Studio Team Services敏捷规划和项目组合管理(六)——VSTS仪表盘的使用

    使用Visual Studio Team Services敏捷规划和项目组合管理(六)--VSTS仪表盘的使用 仪表盘使团队能够看到项目的状态和监控项目的进展.简单来说,不必深入到团队项目站点的其他部 ...

  4. 2017 Pig-0.16.0安装

    前提:已经装好hadoop2.7.3 单机版本: export PIG_HOME=/usr/local/pig    export PATH=$PATH:$PIG_HOME/bin 运行:pig -x ...

  5. 洗礼灵魂,修炼python(86)--全栈项目实战篇(12)—— 利用socket实现文件传输/并发式聊天

    由于本篇博文的项目都很简单,所以本次开个特例,本次解析两个项目,但是都很简单的 项目一:用socket实现文件传输 本项目很简单,作为小项目的预热的,前面刚学完socket,这里马上又利用socket ...

  6. xtrabackup部署以及使用

    简介 备份mysql数据库一直是一个比较恶心的工作,主要就是备份的数据库比较大实在是慢.最近开始使用xtrabackup来备份数据库,速度上快了很多,尤其还原速度要快的多.下面我将从安装开始简要介绍一 ...

  7. Scrapy(爬虫框架)中,Spider类中parse()方法的工作机制

    parse(self,response):当请求url返回网页没有指定回调函数,默认的Request对象的回调函数,用来处理网页返回的response,和生成的Item或者Request对象 以下分析 ...

  8. Eric Chen Mock Interview

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  9. 我的第一个SolidWorks图

    1. 学习到的知识点 2. 完成的工程图 3. 感受 学习是一种快乐,学到新的知识要学会分享,只要坚持,就有那么一点点的成就. 4. 参考 SolidWorks帮助文档

  10. beta版本合集

    beta版本合集 [<p><span style="text-align: center; padding-bottom: 6px; background-color: # ...