当前位置:网站首页>Make a resizable element
Make a resizable element
2022-07-26 21:26:00 【Ziwei front end】
Suppose we want to turn the following elements into draggable elements :
<div id="resizeMe" class="resizable">Resize me</div>
First , We need to prepare some elements to show that the element is resizable . They are absolutely placed on the four sides of the original element . For a simple demonstration , I only prepared two of them , Put them on the right and bottom respectively :
<div id="resizeMe" class="resizable">
Resize me
<div class="resizer resizer-r"></div>
<div class="resizer resizer-b"></div>
</div>This is the basic style of layout :
.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%;
}mousedownOn the adjuster : Track the current position of the mouse and the size of the original elementmousemoveondocument: Calculate how far the mouse has moved , And adjust the size of the elementmouseupondocument: Deleted event handlerdocument
// 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);
}; All event handlers are ready . Last , We will mousedown Event handlers are attached to all resizing :
// Query all resizers
const resizers = ele.querySelectorAll('.resizer');
// Loop over them
[].forEach.call(resizers, function (resizer) {
resizer.addEventListener('mousedown', mouseDownHandler);
});边栏推荐
- Niuke multi school -journey- (map building distra+ card often optimization)
- Devsecops, speed and security
- 制作一个可调整大小的元素
- Serial port communication failure
- 加载 iframe 时显示加载指示器
- Go+mysql+redis+vue3 simple chat room, the sixth bullet: use vue3 and element plus to call the interface
- Arm TZ hardware support
- LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
- In depth study of efcore migrations
- Introduction to the billing function of wechat payment
猜你喜欢

Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复

Registration conditions for information system project managers in the second half of 2022 (soft examination advanced)

牛客刷题——Mysql系列

JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区

About: get the domain controller of the current client login

功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY

Summary of 4 years of software testing experience and interviews with more than 20 companies after job hopping

DevSecOps,让速度和安全兼顾
随机推荐
Is it reliable, reliable and safe to open an account with a low commission for funds in Galaxy Securities
Niuke multi school -journey- (map building distra+ card often optimization)
Flutter Performance Optimization Practice - UI chapter
Custom annotation (I)
Modify excel default code
修改excel默认编码
Serial port communication failure
About: get the domain controller of the current client login
Redis hash和string的区别
What kind of security problems will the server encounter?
Introduction to the billing function of wechat payment
一些意想不到的bug记录
Test cases should never be used casually, recording the thinking caused by the exception of a test case
获取文本选择的方向
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
记一次invalid bound statement xxxxxx 问题解决思路
除了「加机器」,其实你的微服务还能这样优化
Devsecops, speed and security
Basic use of livedatade
word-break: break-all VS word-wrap: break-word