当前位置:网站首页>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);
});边栏推荐
- 2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
- 拖放表格行
- Explain the 190 secondary compiler (decoding table) module of SMR laminated hard disk of Western data in detail
- About: get the domain controller of the current client login
- Computer professional interview topic summary, general navigation
- word-break: break-all VS word-wrap: break-word
- GOM login configuration free version generate graphic tutorial
- Is it reliable, reliable and safe to open an account with a low commission for funds in Galaxy Securities
- 关于:获取当前客户端登录的域控
- 使用 LSTM 进行多变量时间序列预测--问题汇总
猜你喜欢

Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management

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

Flash source code outline

基于Hough变换的直线检测(Matlab)

Practice of microservice in solving Library Download business problems

Redis hash和string的区别

如何在一个项目中使用多种不同的语言?

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

IT系统为什么需要可观测性?

Difference between redis hash and string
随机推荐
Summary of common interview questions of operating system, including answers
About: get the domain controller of the current client login
How to use multiple languages in a project?
一些意想不到的bug记录
立即报名:7 月 29 日推荐系统峰会 2022
拖放表格行
TCP的粘包拆包问题解决方案
ROS2节点通信实现零拷贝
Computer professional interview topic summary, general navigation
Object.getOwnPropertyNames() VS Object.keys()
【Oracle实训】-部署号称零停机迁移的OGG
Devsecops, speed and security
Some unexpected bug records
Ros2 method of obtaining current system time
GOM login configuration free version generate graphic tutorial
Props with type Object/Array must...
LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
Cfdiv1+2-pathwalks- (tree array + linear DP)
微服务化解决文库下载业务问题实践
和月薪3W的字节程序员聊过后,才知道自己一直在打杂...