Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found a simple example of a soccer ball that moves when you mouse drag it. (See code below). However, I'm attempting to turn off the horizontal movement and then catch the mouseup event, even if it's not over the ball and stop the ball from moving.

I put in a
var onTheMove = false;
and then set it to true when I click and drag the ball. It starts. However, when I put a check into onMouseUp and try to fire the ball.onmouseup event stuff, it doesn't work unless the mouse is over the ball. I'm trying to stop the ball from moving, even if the mouse isn't over it. I do this by commenting out the line:
//ball.style.left = pageX - shiftX + 'px';
This way the ball only moves up and down, not left to right, so the mouse drifts from the ball, which is ok, but I still want the ball to stop moving when I mouseup, or drop the ball.

Any thoughts or ideas here are greatly appreciated. I've done a lot of web development, but I seem to get stuck on these "events" things. I must not have something in the proper order/place. Thank you!

HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<script language="JavaScript" type="text/javascript">
<!-- 


// -->
</script>

<script language="JavaScript" type="text/javascript">
<!-- 
document._domino_target = "_self";
function _doClick(v, o, t) {
  var returnValue = false;
  var url="/xpagesext.nsf/testscrollbar?OpenPage&Click=" + v;
  if (o.href != null) {
    o.href = url;
    returnValue = true;
  } else {
    if (t == null)
      t = document._domino_target;
    window.open(url, t);
  }
  return returnValue;
}
// -->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF" onmouseup="if (onTheMove == true) {
	//stop the mousemove from happening, even if mouse up isn't over the ball.
	onTheMove = false
}">

<form action=""><body>
  <p>Drag the ball.</p>
  <img src="https://en.js.cx/clipart/soccer-gate.svg" id="gate" class="droppable">
  <img src="https://en.js.cx/clipart/ball.svg" id="ball">
<script>
	let currentDroppable = null;
	var onTheMove = false;
	ball.onmousedown = function(event) {
		onTheMove = true;
		let shiftX = event.clientX - ball.getBoundingClientRect().left;
		let shiftY = event.clientY - ball.getBoundingClientRect().top;
		ball.style.position = 'absolute';
		ball.style.zIndex = 1000;
		document.body.append(ball);
		moveAt(event.pageX, event.pageY);
		function moveAt(pageX, pageY) {
			ball.style.left = pageX - shiftX + 'px';
			ball.style.top = pageY - shiftY + 'px';
		}
		function onMouseMove(event) {
			moveAt(event.pageX, event.pageY);
			ball.hidden = true;
			let elemBelow = document.elementFromPoint(event.clientX, event.clientY);
			ball.hidden = false;
			if (!elemBelow) return;
		}
		document.addEventListener('mousemove', onMouseMove);
		ball.onmouseup = function() {
			document.removeEventListener('mousemove', onMouseMove);
			ball.onmouseup = null;
		};
	};
	ball.ondragstart = function() {
		return false;
	};
</script></form>
</body>
</html>


What I have tried:

calling "ball.mouseup" from the onMouseUp event.
I've tried just having onMouseUp just set the document.removeEventListener in ball.onmouseup.
Posted
Updated 2-Feb-20 21:31pm
v2
Comments
DerekT-P 30-Jan-20 17:47pm    
How does the ball move if the mouse pointer is NOT over it?? The code just drags the ball, if the pointer isn't over the ball on mouse down, the ball doesn't move. If it is, the ball moves with the mouse so the pointer is always over the ball.
ironclads 30-Jan-20 20:21pm    
Perhaps I wasn't clear. When I mousedown over the ball it moves, but with the left movement is turned off the mouse can stray from the ball, so when the mouseup event fires, it's not over the ball. I still want the ball to stop moving on any mouseup event if the ball has been fired to move on mousedown. Does this make sense?
DerekT-P 31-Jan-20 3:42am    
Not sure what you mean by "with the left movement is tuned off" and putting your code into JSFiddle I can't get the pointer off the ball once dragging... Anyway your only mouseup event is on the ball itself, so if the mouse isn't on the ball, the mouseup won't get called. You need a mouseup event on the window, which will stop dragging the ball. (In fact in that mouseup you don't care where the mouse is, whatever happens, stop dragging the ball. so just changing ball.mouseup=function... to window.mouseup=function... should do the job. And what is OnTheMove for? If there's code elsewhere that tests if the ball is being dragged, you'll need to set this back to false in your mouseup function too. I suspect there's more code / html elements than you're letting on! ;-)
ironclads 2-Feb-20 19:56pm    
That did it. Now I'm feeling stupid. Can't believe I didn't think to change that from "ball.onmouseup" to "window.onmouseup". Regardless, Thank you so much for your help. It's always the little things I don't see that are so obvious. Doesn't happen tons, but when it does I need a fresh pair of eyes to say, "eh, you can just do this." Then comes the "DUH!" moment in my mind. I say a very appreciative and sheepish "Thank you." and move on. Thank you!
DerekT-P 3-Feb-20 3:31am    
No problem - we've all been there! Am adding this as a solution so would you mind marking it accepted? Then the Question is flagged solved and disappears off the unanswered questions... cheers!

1 solution

change ball.mouseup=function... to window.mouseup=function... should do the job
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900