[Edit]
+
0
-
0

JavaScript - simple vertical splitter component in Vanilla JS

9600
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<!doctype html> <html> <head> <style> div.splitter { border: 1px solid #e4e4e4; width: 300px; height: 200px; display: flex; flex-direction: column; } div.panel { flex: auto; max-height: calc(100% - 6px); overflow: auto; } div.resizer { position: relative; border: 1px #e4e4e4; border-style: solid none; background: rgb(228, 228, 228, 20%) url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2230.267%22%20height%3D%226.267%22%20viewBox%3D%220%200%208.006%201.658%22%3E%3Cpath%20fill%3D%22%23cfcfcf%22%20d%3D%22M8.008%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM6.95%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM5.892%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM4.832%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM3.776%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM2.717%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM1.657%200v.6h-.6V0Zm0%201.058v.6h-.6v-.6zM.6%200v.6H0V0Zm0%201.058v.6H0v-.6z%22%2F%3E%3C%2Fsvg%3E') no-repeat center / 18px; flex: 0 0 6px; cursor: row-resize; /* OR: n-resize */ /* the below styles disable selection for resizer element */ -webkit-touch-callout: none; /* iOS Safari */ -webkit-user-select: none; /* Safari */ -khtml-user-select: none; /* Konqueror HTML */ -moz-user-select: none; /* Firefox in the past (old versions) */ -ms-user-select: none; /* Internet Explorer (>=10) / Edge */ user-select: none; /* Currently supported: */ /* Chrome, Opera and Firefox */ } div.resizer:hover::after { position: absolute; top: -6px; right: 0; bottom: -6px; left: 0; background: rgb(228, 228, 228, 30%); display: block; content: '' } </style> <script type="text/javascript"> function computeMargins(element) { var style = element.currentStyle || window.getComputedStyle(element); return { marginTop: parseInt(style.marginTop), marginBottom: parseInt(style.marginBottom) }; } function prepareResizer(master, resizer) { var removed = false; var dragging = false; var position; var style = master.style; var handleMouseDown = function(event) { var margins = computeMargins(master); position = margins.marginTop + margins.marginBottom + master.offsetHeight - event.clientY; dragging = true; }; var handleMouseUp = function(event) { dragging = false; }; var handleMouseMove = function(event) { if (dragging) { var y = position + event.clientY; style.flex = `0 0 ${y}px`; } }; var handleTouchStart = function(event) { var touches = event.touches; if (touches.length === 1) { event.preventDefault(); var touch = touches[0]; handleMouseDown({ clientX: touch.clientX, clientY: touch.clientY }); } }; var handleTouchEnd = function(event) { var touches = event.touches; if (touches.length === 0) { handleMouseUp({}); } }; var handleTouchMove = function(event) { if (dragging) { event.preventDefault(); var touches = event.touches; if (touches.length === 1) { var touch = touches[0]; handleMouseMove({ clientX: touch.clientX, clientY: touch.clientY }); } } }; resizer.addEventListener('mousedown', handleMouseDown); resizer.addEventListener('touchstart', handleTouchStart); window.addEventListener('mouseup', handleMouseUp); window.addEventListener('touchend', handleTouchEnd); window.addEventListener('mousemove', handleMouseMove); window.addEventListener('touchmove', handleTouchMove); return function() { if (removed) { return; } resizer.removeEventListener('mousedown', handleMouseDown); resizer.removeEventListener('touchstart', handleTouchStart); window.removeEventListener('mouseup', handleMouseUp); window.removeEventListener('touchmove', handleTouchEnd); window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('touchend', handleTouchMove); removed = true; }; } </script> </head> <body> <div class="splitter"> <div id="master" class="panel">Panel 1</div> <div id="resizer" class="resizer"></div> <div class="panel">Panel 2</div> </div> <script> var master = document.querySelector('#master'); var resizer = document.querySelector('#resizer'); prepareResizer(master, resizer); </script> </body> </html>
Reset