当前位置:网站首页>Resize table columns
Resize table columns
2022-07-26 21:27:00 【Ziwei front end】
Suppose we want to adjust the size of any column in the following table :
<table id="resizeMe" class="table">
...
</table>Prepare the adjuster
div Elements , Indicates that related columns can be resized . The resizing element is absolutely inside the column . Their CSS The style is as follows :.table th {
position: relative;
}
.resizer {
/* Displayed at the right side of column */
position: absolute;
top: 0;
right: 0;
width: 5px;
cursor: col-resize;
user-select: none;
}want establish Resize And will They are attached to columns , We have to Inquire about and Traverse All columns :
// Query the table
const table = document.getElementById('resizeMe');
// Query all headers
const cols = table.querySelectorAll('th');
// Loop over them
[].forEach.call(cols, function (col) {
// Create a resizer element
const resizer = document.createElement('div');
resizer.classList.add('resizer');
// Set the height
resizer.style.height = `${table.offsetHeight}px`;
// Add a resizer element to the column
col.appendChild(resizer);
// Will be implemented in the next section
createResizableColumn(col, resizer);
});Handle resizing events
createResizableColumn, It takes two arguments :colIndicates the headerresizerRepresents the resizing element in the column
col, We have to deal with three events :mousedownOn the adjuster : Track the current position of the mousemousemoveondocument: Calculate how far the mouse has moved , And adjust the column widthmouseupondocument: Deleted event handlerdocument
const createResizableColumn = function (col, resizer) {
// Track the current position of mouse
let x = 0;
let w = 0;
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
// Calculate the current width of column
const styles = window.getComputedStyle(col);
w = parseInt(styles.width, 10);
// Attach listeners for document's events
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
};
const mouseMoveHandler = function (e) {
// Determine how far the mouse has been moved
const dx = e.clientX - x;
// Update the width of column
col.style.width = `${w + dx}px`;
};
// When user releases the mouse, remove the existing event listeners
const mouseUpHandler = function () {
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};
resizer.addEventListener('mousedown', mouseDownHandler);
};Highlight the adjuster
:hover The selector adds a solid border :.resizer:hover,
.resizing {
border-right: 2px solid blue;
} When resizing When the user clicks and drags the adjuster , This class is added to the adjuster :
const mouseDownHandler = function(e) {
...
resizer.classList.add('resizing');
};
const mouseUpHandler = function() {
...
resizer.classList.remove('resizing');
};边栏推荐
- Ros2 method of obtaining current system time
- [HCIA security] bidirectional nat
- Summary of 4 years of software testing experience and interviews with more than 20 companies after job hopping
- 【MySQL系列】-索引知多少
- 获取文本选择的方向
- Set the template of core configuration file in idea
- <button> 与 <input type=“button“ />
- MySQL -count: the difference between count (1), count (*), and count (column name)
- 1-《PyTorch深度学习实践》-线性模型
- Devsecops, speed and security
猜你喜欢

苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条

【HCIE安全】双机热备-主备备份
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

Why didn't Tencent create a game like "original God"

微服务化解决文库下载业务问题实践

Devsecops, speed and security

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

How to block the legendary GEE engine version? Close player account tutorial through script + engine
![[must read new] Keya valuation analysis of University of technology, heating energy-saving products](/img/e8/c3ac4e5f11db58031cb9249a4ba0f4.jpg)
[must read new] Keya valuation analysis of University of technology, heating energy-saving products

How to enter the specified user method body when debugging in idea?
随机推荐
Devops has been practiced for many years. What is the most painful thing?
一种用于实体关系抽取的统一标签空间
After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores
LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
Multivariable time series prediction using LSTM -- problem summary
TypeScript中的类型断言
Soft test --- database (1) database foundation
一些意想不到的bug记录
关于:获取当前客户端登录的域控
Serial port communication failure
调整表格列的大小
Error in render: “TypeError: data.slice is not a function“
4年软件测试工作经验,跳槽之后面试20余家公司的总结
使用 LSTM 进行多变量时间序列预测--问题汇总
在上传之前预览图像
DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!
Broadcast voice H5 speechsynthesisutterance
Why does it system need observability?
js点击图片打印图像
牛客刷题——Mysql系列