I am currently working on implementing a number input feature that allows users to adjust values by clicking and holding the mouse button while moving the cursor left and right, similar to Unity's editor number adjuster: https://youtu.be/uY9PAcNMu8s?t=907
The challenge I am facing now is making it function properly when there are multiple elements with this behavior. I have been attempting to differentiate between the elements being adjusted by utilizing unique id
s and class
es. Below is the source code snippet (copy and paste into an empty HTML file, then open in a browser):
<input type="number" id="Testing1" value="0"><span class="ClickHoldChangeNumber" id="MouseDragSpace1"><table><tr><td style="width:1000; height:100; border-width:1px; border: 1px solid black;"></td></tr></table></span><br><br>
<input type="number" id="Testing2" value="0"gt;<span class="ClickHoldChangeNumber" id="MouseDragSpace2"><table><tr><td style="width:1000; height:100; border-width:1px; border: 1px solid black;"></td></tr></table></span><br><br>
<span id="MousePos"></span>
<span id="DeltaDrag"></span>
<span id="IncrementDecrement"></span>
<script>
//Initializing variables
DragOriginX = 0;
DragOriginY = 0;
WhichElement = "";
ClickHoldChangeNumberClasses = document.getElementsByClassName("ClickHoldChangeNumber");
window.onload=function(){
SourcePosition = 0;
MouseState = "";
let thing = "";
/* Loop through all classes and add event listeners */
let Index = 0;
while (Index < ClickHoldChangeNumberClasses.length) {
thing = ClickHoldChangeNumberClasses[Index].id;
ClickHoldChangeNumberClasses[Index].addEventListener('mousedown', function(){PlaceDragOriginPos(ClickHoldChangeNumberClasses[Index].id)});
Index++;
}
document.addEventListener('mouseup', ExitPlaceDragOriginPos);
document.addEventListener('mousemove', logKey);
}
function logKey(e) {
if (MouseState != "PressedDown") {
DragOriginX = e.clientX;
DragOriginY = e.clientY;
}
document.getElementById("MousePos").innerHTML = "Mouse position: (" + DragOriginX.toString(10) + ", " + DragOriginY.toString(10) + ")<br>";
document.getElementById("DeltaDrag").innerHTML = "Delta from mouse position: (" + (e.clientX - DragOriginX).toString(10) + ", " + (e.clientY - DragOriginY).toString(10) + ")<br>";
if (MouseState == "PressedDown") {
SubFunctionVariable = [e.clientX, e.clientY];
MouseMoveIncreaseDecrease();
}
}
function MouseMoveIncreaseDecrease() {
document.getElementById("Testing1").value = (SourcePosition + (SubFunctionVariable[0] - DragOriginX)).toString(10);
}
function PlaceDragOriginPos(ElementID) {
MouseState = "PressedDown";
}
function ExitPlaceDragOriginPos() {
SourcePosition = parseInt(document.getElementById("Testing1").value);
MouseState = "Released";
}
</script>
Although the page loads correctly, I encounter an error (
Cannot read property 'id' of undefined
) when trying to click-hold and adjust the values in the rectangular boxes, even though I defined them globally. The reason for them being undefined despite setting them during page load remains unclear to me.
On a related note, does anyone know the technical term used to describe this UI functionality?