WinFuture-Forum.de: Js: Object Anfassen Und Bewegen - WinFuture-Forum.de

Zum Inhalt wechseln

Nachrichten zum Thema: Entwicklung
Seite 1 von 1

Js: Object Anfassen Und Bewegen


#1 Mitglied ist offline   Iso 

  • Gruppe: aktive Mitglieder
  • Beiträge: 487
  • Beigetreten: 14. Mai 03
  • Reputation: 0
  • Wohnort:Hörstel
  • Interessen:Computer<br />Parties<br />Mädels...

  geschrieben 15. März 2007 - 22:03

Hi Leute,
ich bins schon wieder und war habe ich nun folgendes Problem:
Wir lernen ja im Moment DHTML aus einem Buch und dort befindet sich ein Quelltext, womit sich 2 Objekte hin und her bewegen lassen.
Wir sollen nun eine Seite schreiben wo man ein Rechteck hin und her bewegen kann.
Natürlich ist im Buch auch der Quelltext angegeben, sodass ich so abgeschrieben habe, dass ein Rechteck bewegt wird. So weit so gut, funktioniert ja auch alles, außer das in der Aufgabenstellung angegeben war, dass bei dem Code im Buch das zu bewegende Object, wenn es denn dann bewegt wird, sich nach der Maus ausrichtet, dies aber nun nicht mehr der Fall sein soll, also da wo ich das Object mit der Maus anfasse, soll die Maus auch stehen bleiben, sodass sich das Object nicht irgendwie nach der Maus ausrichtet:

Hier der Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
	<head>
		<title>Aufgabe 10</title>
		<script type="text/javascript">
		var dragElem=null;
		var dragging=false;
		var first=true;
		function setDrag(evt,elementID){
			dragElem=getObj(elementID);
			startDrag();
		}
		function startDrag() {
			dragging=true;
			document.onmousemove=dragObj;
			document.onmouseup=dropObj;
		}
		function dragObj(evt) {
			if (window.event){ evt = window.event; }
			if (evt){
				var x = evt.clientX;
				var y = evt.clientY;
				shiftTo(dragElem,x,y,1);
				var pos = "drag coordinates are: " + x + ", " + y + ", " + 1;
				window.status=pos;		
			} 
			return false;
		}
		
		function dropObj() {
			dragging=false;
			document.onmousemove=null;
			document.onmouseup=null;
			window.status="";
		}
		/*End Drag and Drop Script*/
		/*Utility functions, place in codelibrary.js for future use.*/
		/*Getting the width of an object*/
		function getWidth(obj){
			var theObj = getObj(obj);
			if (theObj.clientWidth){
				return parseInt(theObj.clientWidth);
			}
			if (theObj.offsetWidth){
				return parseInt(theObj.offsetWidth);
			}
		}
		/*Getting the Height of an object*/
		function getHeight(obj){
			var theObj = getObj(obj);
			if (theObj.clientHeight){
				return parseInt(theObj.clientHeight);
			}
			if (theObj.offsetHeight){
				return parseInt(theObj.offsetHeight);
			}
		}
		
		/* Convert object name string or object reference
		into a valid object reference */
		function getObj(elementID){
			if (typeof elementID == "string") {
				return document.getElementById(elementID);
			}else{
			return elementID;
			}
		}
		/* Object Motion and Position Scripts */
		/*This function places a positionable object (obj) in
		three dimensions (x,y, and z).*/
		function shiftTo(obj,x,y,z){
			var newObj = getObj(obj);
			newObj.style.left = x + "px";
			newObj.style.top = y + "px";
			newObj.style.zIndex = z;
		}
		</script>
		<style type="text/css">
			#thesquare { position:absolute; left:200px; top:200px;
			width:100px; height:100px; background:red; }
		</style>
	</head>
	<body>
		<div id="thesquare" onmousedown="setDrag(event,this);"></div>
	</body>
</html>



Die Zeilen, wo noch was geändert werden muss, werden wohl folgende sein:
var x = evt.clientX;
var y = evt.clientY;


Vorher stand dort folgendes, damit euch mein Problem klarer wird:
var x = evt.clientX - parseInt(getWidth(dragElem)/2);
var y = evt.clientY - parseInt(getHeight(dragElem)/2);

Könnt den Code in der Codebox ja mal so umändern und selber mal ausprobieren.
Asus F3JP-Z53 Notebook:
Intel Core 2 Duo T7200 @ 2 Ghz
Mobile Radeon X1700 256 MB
2048 MB DDR2-Ram
160 GB Hitachi HDD
Ubuntu 9.04 / Windows XP SP 3
0

Anzeige



#2 Mitglied ist offline   Iso 

  • Gruppe: aktive Mitglieder
  • Beiträge: 487
  • Beigetreten: 14. Mai 03
  • Reputation: 0
  • Wohnort:Hörstel
  • Interessen:Computer<br />Parties<br />Mädels...

geschrieben 16. März 2007 - 07:57

Ok habs doch noch selber hinbekommen,
da der Griffpunkt der Maus am Object ja beim bewegen immer der selbe ist, habe ich ihn nur einmal am Anfang initialisiert und nicht jedes mal, sodass es nun funktioniert :whistling:

Hier der code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
	<head>
		<title>Opdracht 10</title>
		<script type="text/javascript">
		var dragElem=null;
		var dragging=false;
		var intX;
		var intY;
		function setDrag(evt,elementID){
			dragElem=getObj(elementID);
			intX = 200;
			intY = 200;
			startDrag();
		}
		function startDrag() {
			dragging=true;
			document.onmousemove=dragObj;
			document.onmouseup=dropObj;
		}
		function dragObj(evt) {
			if (window.event){ evt = window.event; }
			if (evt){
				if(intX==200 && intY==200){
					intX = (evt.clientX-parseInt(getObj(dragElem).offsetLeft));
					intY = (evt.clientY-parseInt(getObj(dragElem).offsetTop));
				}
				var x = evt.clientX - intX;
				var y = evt.clientY  - intY;
				shiftTo(dragElem,x,y,1);
				var pos = "drag coordinates are: " + x + ", " + y + ", " + 1;
				window.status=pos;		
			} 
			return false;
		}
		
		function dropObj() {
			dragging=false;
			document.onmousemove=null;
			document.onmouseup=null;
			window.status="";
		}
		/*End Drag and Drop Script*/
		/*Utility functions, place in codelibrary.js for future use.*/
		/*Getting the width of an object*/
		function getWidth(obj){
			var theObj = getObj(obj);
			if (theObj.clientWidth){
				return parseInt(theObj.clientWidth);
			}
			if (theObj.offsetWidth){
				return parseInt(theObj.offsetWidth);
			}
		}
		/*Getting the Height of an object*/
		function getHeight(obj){
			var theObj = getObj(obj);
			if (theObj.clientHeight){
				return parseInt(theObj.clientHeight);
			}
			if (theObj.offsetHeight){
				return parseInt(theObj.offsetHeight);
			}
		}
		
		/* Convert object name string or object reference
		into a valid object reference */
		function getObj(elementID){
			if (typeof elementID == "string") {
				return document.getElementById(elementID);
			}else{
			return elementID;
			}
		}
		/* Object Motion and Position Scripts */
		/*This function places a positionable object (obj) in
		three dimensions (x,y, and z).*/
		function shiftTo(obj,x,y,z){
			var newObj = getObj(obj);
			newObj.style.left = x + "px";
			newObj.style.top = y + "px";
			newObj.style.zIndex = z;
		}
		</script>
		<style type="text/css">
			#thesquare { position:absolute; left:200px; top:200px;
			width:100px; height:100px; background:red; }
		</style>
	</head>
	<body>
		<div id="thesquare" onmousedown="setDrag(event,this);"></div>
	</body>
</html>


Asus F3JP-Z53 Notebook:
Intel Core 2 Duo T7200 @ 2 Ghz
Mobile Radeon X1700 256 MB
2048 MB DDR2-Ram
160 GB Hitachi HDD
Ubuntu 9.04 / Windows XP SP 3
0

Thema verteilen:


Seite 1 von 1

1 Besucher lesen dieses Thema
Mitglieder: 0, Gäste: 1, unsichtbare Mitglieder: 0