当前位置:网站首页>制作一个可调整大小的元素
制作一个可调整大小的元素
2022-07-26 20:31:00 【紫微前端】
假设我们要将以下元素变成可拖动元素:
<div id="resizeMe" class="resizable">Resize me</div>
首先,我们需要准备一些元素来表明该元素是可调整大小的。它们绝对放置在原始元素的四个侧面。为了简单的演示,我只准备了其中两个,分别放在右侧和底部:
<div id="resizeMe" class="resizable">
Resize me
<div class="resizer resizer-r"></div>
<div class="resizer resizer-b"></div>
</div>这是布局的基本样式:
.resizable {
position: relative;
}
.resizer {
/* All resizers are positioned absolutely inside the element */
position: absolute;
}
/* Placed at the right side */
.resizer-r {
cursor: col-resize;
height: 100%;
right: 0;
top: 0;
width: 5px;
}
/* Placed at the bottom side */
.resizer-b {
bottom: 0;
cursor: row-resize;
height: 5px;
left: 0;
width: 100%;
}mousedown在调整器上:跟踪鼠标的当前位置和原始元素的尺寸mousemoveondocument:计算鼠标移动了多远,并调整元素的尺寸mouseupondocument:删除的事件处理程序document
// Query the element
const ele = document.getElementById('resizeMe');
// The current position of mouse
let x = 0;
let y = 0;
// The dimension of the element
let w = 0;
let h = 0;
// Handle the mousedown event
// that's triggered when user drags the resizer
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
y = e.clientY;
// Calculate the dimension of element
const styles = window.getComputedStyle(ele);
w = parseInt(styles.width, 10);
h = parseInt(styles.height, 10);
// Attach the listeners to `document`
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
};
const mouseMoveHandler = function (e) {
// How far the mouse has been moved
const dx = e.clientX - x;
const dy = e.clientY - y;
// Adjust the dimension of element
ele.style.width = `${w + dx}px`;
ele.style.height = `${h + dy}px`;
};
const mouseUpHandler = function () {
// Remove the handlers of `mousemove` and `mouseup`
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};所有的事件处理程序都准备好了。最后,我们将mousedown事件处理程序附加到所有调整大小:
// Query all resizers
const resizers = ele.querySelectorAll('.resizer');
// Loop over them
[].forEach.call(resizers, function (resizer) {
resizer.addEventListener('mousedown', mouseDownHandler);
});边栏推荐
- Alkbh1
- golang版本管理gvm
- [JVM series] JVM tuning
- 【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
- What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!
- 我们从Crypto市场中学到的最艰难一课
- In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
- Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
- kubernetes之Deployment
- LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
猜你喜欢

GOM login configuration free version generate graphic tutorial

Sign up now: July 29 recommendation system summit 2022

串口通信失败

In addition to "adding machines", in fact, your micro service can be optimized like this

ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一

JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication
![[download materials of harmoniyos topics] HDD Hangzhou station · offline salon focuses on application innovation to show the ecological charm of Hongmeng](/img/62/9e2ff0dc2c8b049fd32ad56334a0c0.jpg)
[download materials of harmoniyos topics] HDD Hangzhou station · offline salon focuses on application innovation to show the ecological charm of Hongmeng

How to block the legendary GEE engine version? Close player account tutorial through script + engine

手机\固定电话座机呼叫转移设置方法
随机推荐
使用 LSTM 进行多变量时间序列预测--问题汇总
conda报错:json.decoder.JSONDecodeError:
JVM learning - memory structure - program counter & virtual machine stack & local method stack & heap & method area
kubernetes之Deployment
Set the template of core configuration file in idea
【HCIA安全】NAT网络地址转换
SPI configuration
Mysql -count :count(1)、count(*)、count(列名)的区别
Multivariable time series prediction using LSTM -- problem summary
DevSecOps,让速度和安全兼顾
牛客刷题——Mysql系列
[question] browser get request with token
ROS2获取当前系统时间的方法
IT系统为什么需要可观测性?
Is it reliable, reliable and safe to open an account with a low commission for funds in Galaxy Securities
【MySQL系列】-索引知多少
银河证券场内基金低佣金开户靠谱吗,可靠安全吗
TypeScript中的类型断言
关于:获取当前客户端登录的域控
Some unexpected bug records