Skip to content

Instantly share code, notes, and snippets.

@Thrilleratplay
Created September 21, 2015 04:15
Show Gist options
  • Save Thrilleratplay/45fc8acd5c5f9b2fc0ad to your computer and use it in GitHub Desktop.
Save Thrilleratplay/45fc8acd5c5f9b2fc0ad to your computer and use it in GitHub Desktop.
vanilla js HTML column resizing
(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;
});
})();
@samdeesh
Copy link

samdeesh commented Apr 9, 2017

Is there any live implementation for this?

@Jhonatangiraldo
Copy link

Hello @samdeesh here is an implementation of that http://jsfiddle.net/thrilleratplay/epcybL4v/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment