JS-Demo1: JavaScript implement table column dragging

Source: Internet
Author: User

You can drag the JS table column to the special effect:

[Html]
<! DOCTYPE html>
<Html>
<Head>
<Style type = "text/css">
Table {
Border-top: solid 1px black;
Border-left: solid 1px black;
Font-size: 12px;
Width: 100%;
}
Th {
White-space: nowrap;
}
Th, td {
Border-right-style: solid;
Border-right-width: 1px;
Border-bottom-style: solid;
Border-bottom-width: 1px;
Height: 30px;
}
. Dragable {
Width: 3px;
Height: 100%;
Background-color: white;
Float: right;/* This style has a certain relationship with the effect. Others do not matter */
Cursor: col-resize;
}
</Style>
<Script type = "text/javascript">
Var draging = false; // whether to drag
Var dragElement = null; // The th
Var offsetLeft = 0; // The th and absolute left coordinates of the current drag
Var offsetWidth = 0; // The absolute width of th currently dragged
Function mousedown (){
If (draging) return;
Draging = true;
DragElement = window. event. srcElement. parentNode;
OffsetLeft = dragElement. offsetLeft;
Document. body. style. cursor = "col-resize ";
Document. body. onselectstart = function () {return false ;}; // when dragging or dropping a word, it is not allowed to be selected when the mouse slides over the word (the effect of the selected word by default is known to everyone)
}
Function mouseup (){
Draging = false;
DragElement = null;
Document. body. style. cursor = "auto ";
Document. body. onselectstart = function () {return true };
}
Function mousemove (){
If (draging & dragElement ){
Var width = event. clientX-offsetLeft;
If (width> 0 ){
DragElement. style. width = width;
OffsetWidth = dragElement. offsetWidth;
} Else {
DragElement. style. width = offsetWidth;
}
}
}
</Script>
</Head>
<Body onmousemove = "mousemove ();" onmouseup = "mouseup ();">
<Table cellpadding = "0" cellspacing = "0">
<Thead>
<Tr>
<Th style = "width: 120px;"> NO. <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th style = "width: 120px;"> name <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th style = "width: 120px;"> age <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th> remarks </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> 1 </td>
<Td> Siuon </td>
<Td> 100 </td>
<Td> JavaEE engineer... </td>
</Tr>
</Tbody>
</Table>
</Body>
</Html>

Dynamically generate <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span>

[Html]
<! DOCTYPE html>
<Html>
<Head>
<Style type = "text/css">
Table {
Border-top: solid 1px black;
Border-left: solid 1px black;
Font-size: 12px;
Width: 100%;
}
Th {
White-space: nowrap;
}
Th, td {
Border-right-style: solid;
Border-right-width: 1px;
Border-bottom-style: solid;
Border-bottom-width: 1px;
Height: 30px;
}
. Dragable {
Width: 3px;
Height: 100%;
Background-color: white;
Float: right;/* This style has a certain relationship with the effect. Others do not matter */
Cursor: col-resize;
}
</Style>
<Script type = "text/javascript">
Var draging = false; // whether to drag
Var dragElement = null; // The th
Var offsetLeft = 0; // The th and absolute left coordinates of the current drag
Var offsetWidth = 0; // The absolute width of th currently dragged
Function mousedown (){
If (draging) return;
Draging = true;
DragElement = window. event. srcElement. parentNode;
OffsetLeft = dragElement. offsetLeft;
Document. body. style. cursor = "col-resize ";
Document. body. onselectstart = function () {return false ;}; // when dragging or dropping a word, it is not allowed to be selected when the mouse slides over the word (the effect of the selected word by default is known to everyone)
}
Function mouseup (){
Draging = false;
DragElement = null;
Document. body. style. cursor = "auto ";
Document. body. onselectstart = function () {return true };
}
Function mousemove (){
If (draging & dragElement ){
Var width = event. clientX-offsetLeft;
If (width> 0 ){
DragElement. style. width = width;
OffsetWidth = dragElement. offsetWidth;
} Else {
DragElement. style. width = offsetWidth;
}
}
}

// Create <span class = "dragable" onmousedown = "mousedown ();"> </span>
Function createSpan (){
Var span = document. createElement ("span ");
Span. className = "dragable ";
Span. attachEvent ("onmousedown", mousedown );
Span. innerHTML = "";
Return span;
}
Function init (){
// Add span in th
Var ths = document. getElementsByTagName ("th ");
For (var I = 0; I <ths. length; I ++ ){
Ths [I]. appendChild (createSpan ());
}
}
</Script>
</Head>
<Body onload = "init ()" onmousemove = "mousemove ();" onmouseup = "mouseup ();">
<Table id = "mytable" cellpadding = "0" cellspacing = "0">
<Thead>
<Tr>
<Th style = "width: 120px;"> NO. </th>
<Th style = "width: 120px;"> name </th>
<Th style = "width: 120px;"> age </th>
<Th> remarks </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> 1 </td>
<Td> Siuon </td>
<Td> 100 </td>
<Td> JavaEE engineer... </td>
</Tr>
</Tbody>
</Table>
</Body>
</Html>

 

 

From Siuon's Blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.