当前位置:网站首页>制作一个可调整大小的元素
制作一个可调整大小的元素
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);
});边栏推荐
- 【Oracle实训】-部署号称零停机迁移的OGG
- 牛客多校-Journey-(建图distra+卡常优化)
- IT系统为什么需要可观测性?
- How to configure the legendary SF lander to automatically read the list without a network
- Mysql -count :count(1)、count(*)、count(列名)的区别
- The hardest lesson we learned from the crypto Market
- 留存收益率计算公式
- Go+mysql+redis+vue3 simple chat room, the sixth bullet: use vue3 and element plus to call the interface
- [download materials of harmoniyos topics] HDD Hangzhou station · offline salon focuses on application innovation to show the ecological charm of Hongmeng
- 修改excel默认编码
猜你喜欢
![[Oracle training] - deploy Ogg known as zero downtime migration](/img/bc/4ee0493129d5abab931ca50dbdce6f.png)
[Oracle training] - deploy Ogg known as zero downtime migration

Difference between redis hash and string

除了「加机器」,其实你的微服务还能这样优化

After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores

Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift

Explain the 190 secondary compiler (decoding table) module of SMR laminated hard disk of Western data in detail

详解西部数据SMR叠瓦式硬盘的190二级编译器(译码表)模块

js中join方法
![[HCIA security] NAT network address translation](/img/10/3b4d011963e00229d73a7435ce8c4b.png)
[HCIA security] NAT network address translation

关于:获取当前客户端登录的域控
随机推荐
TypeScript中的类型断言
CFdiv1+2-Pathwalks-(树状数组+线性dp)
4年软件测试工作经验,跳槽之后面试20余家公司的总结
Detailed illustration of B-tree and its implementation in C language
串口通信失败
Summary of common interview questions on computer network, including answers
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
【HCIA安全】用户认证
修改excel默认编码
基于Hough变换的直线检测(Matlab)
About: get the domain controller of the current client login
洛谷-线段覆盖-(区间排序问题总结)
我们被一个 kong 的性能 bug 折腾了一个通宵
详解西部数据SMR叠瓦式硬盘的190二级编译器(译码表)模块
Is it safe for Huishang futures to open an account? What should Huishang futures pay attention to when opening an account?
Mysql -count :count(1)、count(*)、count(列名)的区别
JDBC connection
[OBS] solve the problem of OBS pushing two RTMP streams + timestamp
Devops has been practiced for many years. What is the most painful thing?
Alkbh1