In HTML, write a container that contains a range input. The range should be disabled. However, to change its value, you should drag the container from the left/right sides (make sure you give the sides enough space to be dragged from) to the top/bottom to move the range with gravity.
Assistant
html
<!DOCTYPE html>
<html>
<head>
<title>Range Slider with Gravity</title>
<style>
#container {
position: relative;
width: 300px;
height: 50px;
margin: 50px auto;
background-color: #f0f0f0;
user-select: none; /* Prevent text selection while dragging */
}
#left-side, #right-side {
position: absolute;
width: 50px;
height: 100%;
top: 0;
background-color: rgba(0, 0, 255, 0.1);
cursor: ns-resize; /* Cursor indicates vertical drag */
}
#left-side {
left: 0;
}
#right-side {
right: 0;
}
#myRange {
position: absolute;
left: 50px;
right: 50px;
top: 50%;
transform: translateY(-50%);
pointer-events: none; /* Disable pointer events */
}
</style>
</head>
<body>
<div id="container">
<div id="left-side"></div>
<div id="right-side"></div>
<input type="range" id="myRange" disabled min="0" max="100" value="50">
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
let isDragging = false;
let startY = 0;
let currentValue = parseInt(document.getElementById('myRange').value);
const rangeInput = document.getElementById('myRange');
const min = parseInt(rangeInput.min);
const max = parseInt(rangeInput.max);
function onMouseDown(event) {
isDragging = true;
startY = event.clientY;
currentValue = parseInt(rangeInput.value);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
// Prevent default behavior to avoid text selection
event.preventDefault();
}
function onMouseMove(event) {
if (!isDragging) return;
let deltaY = event.clientY - startY;
// Sensitivity factor to control how fast the value changes
let sensitivity = (max - min) / 200; // Adjust this value as needed
// Simulate gravity-like acceleration
let deltaValue = deltaY * Math.abs(deltaY) * sensitivity / 100;
let newValue = currentValue + deltaValue;
// Clamp the newValue within min and max
newValue = Math.min(Math.max(newValue, min), max);
rangeInput.value = newValue;
}
function onMouseUp(event) {
isDragging = false;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
document.getElementById('left-side').addEventListener('mousedown', onMouseDown);
document.getElementById('right-side').addEventListener('mousedown', onMouseDown);
});
</script>
</body>
</html>
Explanation:
HTML Structure:
A container <div id="container"> holds the range input and the draggable left and right sides.
The range input <input type="range" id="myRange" disabled> is disabled to prevent direct interaction.
Left and right sides <div id="left-side"> and <div id="right-side"> serve as draggable areas.
CSS Styling:
The container is centered and given a background color for visibility.
The left and right sides are positioned absolutely within the container, with enough width to be easily grabbed.
The range input is positioned between the sides and vertically centered.
user-select: none; is set to prevent text selection during dragging.
pointer-events: none; on the range input ensures it doesn't intercept mouse events.
JavaScript Functionality:
Event listeners are added to detect when the user starts dragging (mousedown), moves the mouse (mousemove), and releases the mouse button (mouseup).
On mousedown, we start tracking the mouse movement.
On mousemove, we calculate the vertical distance moved (deltaY) and adjust the range input's value accordingly.
A sensitivity factor controls how quickly the value changes with movement.
By multiplying deltaY by Math.abs(deltaY), we simulate acceleration (gravity effect), as the rate of change increases with the distance dragged.
On mouseup, we stop tracking the mouse movement.
Now, when you run this code in a browser, you will see the range input's value change as you drag the left or right side up and down, simulating the effect of moving the range with gravity.