How can I stop dragging as soon as the drag
hits top and bottom of the .parent
?
As you can see the .button
is stops as soon as it hits the inner top and bottom of the .parent
but the event still continuing! . to test it try to drag the .button
to the bottom ot top and do not stop dragging when reaching the end, as you can see the new_top
still updating!
JavaScript
x
32
32
1
$(function() {
2
$('.button').mousedown(function(e) {
3
if(e.which===1) {
4
var button = $(this);
5
var parent_height = button.parent().innerHeight();
6
var top = parseInt(button.css('top')); //current top position
7
var original_ypos = button.css('top','').position().top; //original ypos (without top)
8
button.css({top:top+'px'}); //restore top pos
9
var drag_min_ypos = 0-original_ypos;
10
var drag_max_ypos = parent_height-original_ypos-button.outerHeight();
11
var drag_start_ypos = e.clientY;
12
$('#log1').text('mousedown top: '+top+', original_ypos: '+original_ypos);
13
$(window).on('mousemove',function(e) {
14
button.addClass('drag');
15
var new_top = top+(e.clientY-drag_start_ypos);
16
button.css({top:new_top+'px'});
17
if(new_top<drag_min_ypos) { button.css({top:drag_min_ypos+'px'}); }
18
if(new_top>drag_max_ypos) { button.css({top:drag_max_ypos+'px'}); }
19
$('#log2').text('mousemove min: '+drag_min_ypos+', max: '+drag_max_ypos+', new_top: '+new_top);
20
21
});
22
$(window).on('mouseup',function(e) {
23
if(e.which===1) {
24
$('.button').removeClass('drag');
25
$(window).off('mouseup mousemove');
26
$('#log1').text('mouseup');
27
$('#log2').text('');
28
}
29
});
30
}
31
});
32
});
JavaScript
1
18
18
1
.parent {
2
position: relative;
3
display: block;
4
padding: 0px;
5
margin: 20px;
6
width:45px;
7
height: 200px;
8
border: 1px solid black;
9
}
10
.button {
11
position: absolute;
12
top: 0;
13
display: table;
14
margin-bottom: 2px;
15
height: 25px;
16
clear: both;
17
}
18
.button.drag { z-index: 99; color: red; }
JavaScript
1
7
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="parent" class="parent">
3
<button id="button1" class="button">Drag</button>
4
5
</div>
6
<div id="log1"></div>
7
<div id="log2"></div>
Advertisement
Answer
You can add this line inside your check:
JavaScript
1
2
1
$(this).off('mousemove');
2
Which removes the mousemove
listener when you hit the max y position.
JavaScript
1
40
40
1
$(function() {
2
$('.button').mousedown(function(e) {
3
if(e.which===1) {
4
var button = $(this);
5
var parent_height = button.parent().innerHeight();
6
var top = parseInt(button.css('top')); //current top position
7
var original_ypos = button.css('top','').position().top; //original ypos (without top)
8
button.css({top:top+'px'}); //restore top pos
9
var drag_min_ypos = 0-original_ypos;
10
var drag_max_ypos = parent_height-original_ypos-button.outerHeight();
11
var drag_start_ypos = e.clientY;
12
$('#log1').text('mousedown top: '+top+', original_ypos: '+original_ypos);
13
$(window).on('mousemove',function(e) {
14
button.addClass('drag');
15
var new_top = top+(e.clientY-drag_start_ypos);
16
button.css({top:new_top+'px'});
17
18
if(new_top < drag_min_ypos) {
19
button.css({top:drag_min_ypos+'px'});
20
}
21
22
if (new_top > drag_max_ypos) {
23
button.css({top:drag_max_ypos+'px'});
24
$(this).off('mousemove'); /// <<< ------ HERE
25
}
26
$('#log2').text('mousemove min: '+drag_min_ypos+', max: '+drag_max_ypos+', new_top: '+new_top);
27
28
29
});
30
$(window).on('mouseup',function(e) {
31
if(e.which===1) {
32
$('.button').removeClass('drag');
33
$(window).off('mouseup mousemove');
34
$('#log1').text('mouseup');
35
$('#log2').text('');
36
}
37
});
38
}
39
});
40
});
JavaScript
1
18
18
1
.parent {
2
position: relative;
3
display: block;
4
padding: 0px;
5
margin: 20px;
6
width:45px;
7
height: 200px;
8
border: 1px solid black;
9
}
10
.button {
11
position: absolute;
12
top: 0;
13
display: table;
14
margin-bottom: 2px;
15
height: 25px;
16
clear: both;
17
}
18
.button.drag { z-index: 99; color: red; }
JavaScript
1
7
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="parent" class="parent">
3
<button id="button1" class="button">Drag</button>
4
5
</div>
6
<div id="log1"></div>
7
<div id="log2"></div>