Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the X Y coords of a object 3

Status
Not open for further replies.

yandso

Programmer
Jul 20, 2001
278
0
0
US
I need to find the x and y coordinates of a flash movie on my page. I perform different actions when the mouse is not over my movie. However I need to know what the x and y are all the time. For example if they have a different resolution or if the page is not maximised.
 
What do you need to find the X and Y coordinates for ...the mouse or the movie
 
OK heres what you need to find the coordinates ...just put it in the head tag of your page.
Code:
<style>
<!--
.drag{position:relative;cursor:hand}
-->
</style>
<script language=&quot;JavaScript&quot;>
<!--

var dragapproved=false
var z,x,y
function move(){
if (event.button==1&&dragapproved){
z.style.pixelLeft=temp1+event.clientX-x
z.style.pixelTop=temp2+event.clientY-y
return false
}
}
function drags(){
if (!document.all)
return
if (event.srcElement.className==&quot;drag&quot;){
dragapproved=true
z=event.srcElement
temp1=z.style.pixelLeft
temp2=z.style.pixelTop
x=event.clientX
y=event.clientY
document.onmousemove=move
}
}
document.onmousedown=drags
document.onmouseup=new Function(&quot;dragapproved=false&quot;)
//-->
</script>
</head>


<script language=&quot;javascript&quot;>


var where = &quot;&quot;; // which link
function checkwhere(e) {
        if (document.layers){
        xCoord = e.x;
        yCoord = e.y;
}
        else if (document.all){
        xCoord = event.clientX;
        yCoord = event.clientY;
}
        else if (document.getElementById){
        xCoord = e.clientX;
        yCoord = e.clientY;
}


}
//the x coordinate varible is xCoord
//the y coordinate varible is yCoord        


document.onmousemove = checkwhere;
if(document.captureEvents) {document.captureEvents(Event.MOUSEMOVE);}

</script>
put this in the the tag that calls the flash movie
Code:
onLoad=&quot;window.status='X Coordinates: '+ xCoord + '  Y Coordinates: ' + yCoord + '&quot;

I dont know if your going to use an if statement or what to find where the corrdinates are, but to call each thing the x coordinate varible is xCoord and the y coordinate varible is yCoord. if you need any further help such as writing the if statement or where to put the actual calling of the script just post back here.if this isn't what your looking for then post back here cuz im not too sure about flash movies an this might not work.
jammer1221
 
Why so complicated? :/

var t;
var l;
var cNode = document.getElementById('movie');
while(cNode.tagName!='BODY'){
l+=cNode.offsetLeft;
t+=cNode.offsetTop;
cNode=cNode.offsetParent;
}
alert('position, left:'+l+' and top:'+t);
}

----------
I'm willing to trade custom scripts for... [see profile]
 
stormbind
Wow that is much easier...Mine was an old script that was used to find the mouse coordinates in a game...would yoru script be able to find the coordinates for a mouse or would you have to have the mouse have an ID...in order to call it using the getElementbyID function
yandso
use stormbind's script is is much easier to understand is is suited more for what your looking for.

sorry mine didnt help ya
jammer1221
 
Thanks for the vote of confidence jammer :)

I frequently use that snippet for positioning DHTML widgets, like expanding menus or whatever.

----------
I'm willing to trade custom scripts for... [see profile]
 
Thanks stormbind the offsetLeft and offsetTop were exactly what I was looking for. What is the offsetParent for though?
 
offsetLeft and offsetTop only measure the distance from the previous element.

offsetParent is that previous element.

By measuring the offsetLeft & offsetParent of every element between the movie and the BODY tag, you get the total left & top positions.

----------
I'm willing to trade custom scripts for... [see profile]
 
I've got a kind of standardised function that I use for this, but I never quite got it to work properly when there's a left/top margin set on the page.

Anybody wanna help? (Please.) [smile]
 
One more question stormbind. When I run that script it tells me that the left and top are NaN? Any ideas
 
Try setting them to 0 at the start.

var l=0;
var t=0;

Sorry, typed it up from memory.

----------
I'm willing to trade custom scripts for... [see profile]
 
That did the trick. Thanks I owe you one
 
theboyhope,

Can you set the initial value to the BODY.leftMargin and BODY.topMargin? :/

----------
I'm willing to trade custom scripts for... [see profile]
 
Actually, I've just dug out the old function again and it seems to be reporting the correct values.

It's been a while since I thought about it. Maybe it was just a problem on Macs or something. :eek:/

Anybody got a Mac to test this on?
Code:
function getCoordinates( obj ) {

	var x = 0; var y = 0;

	if( typeof obj == &quot;string&quot; ) {

		obj = document.getElementById( obj );
 	}

	if( obj != null ) {

		while( obj.tagName != &quot;BODY&quot; ) {

			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}

		return new Array( x, y );

	} else {

		alert( &quot;Object not found.&quot; );
		return false;
	}
}
 

Ah, well. :)

I've got a Mac at work nowadays, so I'll try to remember to test it after the Xmas holidays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top