I have one div
that can only be dragged up and down, but I want it to have a minimum of 250px and a maximum of 650px , I have already searched but have not been able to find a solution yet.
Here you can test the code I have so far.
var offset = [0];
var divOverlay = document.querySelector(".panel__bottom-xs");
var isDown = false;
divOverlay.addEventListener('mousedown', function(e) {
isDown = true;
offset = [
divOverlay.offsetTop - e.clientY
];
}, true);
document.addEventListener('mouseup', function() {
isDown = false;
}, true);
document.addEventListener('mousemove', function(e) {
event.preventDefault();
if (isDown) {
divOverlay.style.top = (e.clientY + offset[0]) + 'px';
document.querySelector('span').innerHTML = divOverlay.style.top }
}, true);
.panel__bottom-xs {
position: fixed;
bottom: 0px;
background: red;
height: 50px;
width: 50px;
left: 150px;
}
<div class="panel__bottom-xs"></div>
<span></span>
Thank you very much in advance for your help!
Solving your question, you must include an if() inside the mousedown event that evaluates the vertical position of the mouse pointer when it is outside the range between 250px and a maximum of 650px, it does not update the top position of the div.
Set the top position of the div to 250px for tabulation purposes
Here I include the modified code.