当前位置:网站首页>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);
});边栏推荐
- Arm TZ hardware support
- 留存收益率计算公式
- 平滑滚动到元素
- Alkbh1
- <button> 与 <input type=“button“ />
- js点击图片打印图像
- Redis interview questions
- HTTP cache browser cache that rabbits can understand
- 2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
- Cfdiv1+2-pathwalks- (tree array + linear DP)
猜你喜欢

串口通信失败

2022-7-26 第七组 抽象和接口

What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!

About: get the domain controller of the current client login

TCP的粘包拆包问题解决方案

Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation

Test cases should never be used casually, recording the thinking caused by the exception of a test case
![[hero planet July training leetcode problem solving daily] 26th and check the collection](/img/f1/e63b1f35b883274ca077cbd2ab4c24.png)
[hero planet July training leetcode problem solving daily] 26th and check the collection

Error in render: “TypeError: data.slice is not a function“

Pointpillars: fast encoders for object detection from point clouds reading notes
随机推荐
Cfdiv1+2-pathwalks- (tree array + linear DP)
Flutter Performance Optimization Practice - UI chapter
The hardest lesson we learned from the crypto Market
In addition to "adding machines", in fact, your micro service can be optimized like this
ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一
Devsecops, speed and security
Niuke brush questions - MySQL series
Devops has been practiced for many years. What is the most painful thing?
In depth study of efcore migrations
Calculation formula of retained earnings rate
Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
加载 iframe 时显示加载指示器
Ros2 node communication realizes zero copy
[HCIA security] user authentication
JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区
How to implement Devops with automation tools | including low code and Devops application practice
【打新必读】工大科雅估值分析,供热节能产品
About: get the domain controller of the current client login
Flutter性能优化实践 —— UI篇
js中join方法