当前位置:网站首页>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');
};边栏推荐
- 洛谷-线段覆盖-(区间排序问题总结)
- Number() VS parseInt()
- [HCIA security] bidirectional nat
- Custom annotation (I)
- What kind of security problems will the server encounter?
- How to enter the specified user method body when debugging in idea?
- 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
- MySQL -count: the difference between count (1), count (*), and count (column name)
- There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
猜你喜欢

How to use multiple languages in a project?

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

4年软件测试工作经验,跳槽之后面试20余家公司的总结

2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览

Redis hash和string的区别

JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区
HTTP cache browser cache that rabbits can understand

MySQL的JDBC操作及入门案例

2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
![[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
随机推荐
25张炫酷交互图表,一文入门Plotly
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
如何在一个项目中使用多种不同的语言?
Props with type Object/Array must...
测试用例千万不能随便,记录由一个测试用例异常引起的思考
Niuke brush questions - MySQL series
Test cases should never be used casually, recording the thinking caused by the exception of a test case
Soft test --- database (1) database foundation
[HCIA security] NAT network address translation
[hcie security] dual computer hot standby - primary and standby backup
MySQL的JDBC操作及入门案例
Deployment of kubernetes
力扣每日一题-第43天-168. Excel表列名称
How to configure the legendary SF lander to automatically read the list without a network
服务器的防护会遇到什么样的安全问题呢?
Flutter Performance Optimization Practice - UI chapter
Sign up now: July 29 recommendation system summit 2022
基于Hough变换的直线检测(Matlab)
Introduction of JDBC
How to block the legendary GEE engine version? Close player account tutorial through script + engine