Created
September 21, 2015 04:15
-
-
Save Thrilleratplay/45fc8acd5c5f9b2fc0ad to your computer and use it in GitHub Desktop.
vanilla js HTML column resizing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var thElm; | |
var startOffset; | |
// Create 'grip' elements near the right border and bind mouse down event to | |
// resize | |
Array.prototype.forEach.call( | |
document.querySelectorAll("table th"), | |
function (th) { | |
th.style.position = 'relative'; | |
var grip = document.createElement('div'); | |
grip.innerHTML = " "; | |
grip.style.top = 0; | |
grip.style.right = 0; | |
grip.style.bottom = 0; | |
grip.style.width = '5px'; | |
grip.style.position = 'absolute'; | |
grip.style.cursor = 'col-resize'; | |
grip.addEventListener('mousedown', function (e) { | |
thElm = th; | |
startOffset = th.offsetWidth - e.pageX; | |
}); | |
th.appendChild(grip); | |
}); | |
// On mouse move event | |
document.addEventListener('mousemove', function (e) { | |
if (thElm) { | |
thElm.style.width = startOffset + e.pageX + 'px'; | |
} | |
}); | |
// On mouse up event | |
document.addEventListener('mouseup', function () { | |
thElm = undefined; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @samdeesh here is an implementation of that http://jsfiddle.net/thrilleratplay/epcybL4v/