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!

"lock" cursor over element / create "splitter" control

Status
Not open for further replies.

NBartomeli

Programmer
Jul 1, 2003
111
0
0
US
I have a table with a single row and 3 cells, first and last cell contain iframes, the middle cell is 5px wide, and when clicked allows the user to drag left/right and change the width of each frame.

My problem is that when the user moves the cursor too quickly, it moves off of the element (<td> tag) that they are dragging and over one of the iframes. Since the iframes have child pages loaded, the "resize" event is not there and therefore the size of the element doesnt change.

is there any way to "lock" the cursor over the element where the mousedown event occured? or is there some other javascript "splitter" control out somewhere I can look at the code for or use?

Thanks in advance, here is a quick-n-dirty sample page:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>FrameSize</title>

<script>

var frame;

function StartSize(event){
		
	frame = document.getElementById('frame1');
			
	document.attachEvent("onmousemove", SizeGo);
	document.attachEvent("onmouseup",   SizeStop);
	window.event.cancelBubble = true;
	window.event.returnValue = false;

}

function SizeGo(event) {

	var x, y;
	x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
	y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;

	frame.style.width =  x - 25;
	
	window.event.cancelBubble = true;
	window.event.returnValue = false;
}

function SizeStop(event) {
	document.detachEvent("onmousemove", SizeGo);
	document.detachEvent("onmouseup",   SizeStop);
}


</script>

</head>
<body>

<table border="1">
	<tr>
		<td>
			<iframe id="frame1" style="width:500px; height:200px;" scrolling="no" frameborder="0" src="planningtool/loading.htm"></iframe>
		</td>
		<td onmousedown="StartSize(event);" style="background:black; width:5px; cursor:hand;">&nbsp;</td>
		<td>
			<iframe id="frame2" style="width:500px; height:200px;" scrolling="no" frameborder="0" src="planningtool/loading.htm"></iframe>
		</td>
	</tr>
</table>

</body>
</html>
 
I do not have a solution but here are some ideas.
The resizing obviously takes some time to execute and it cannot resize faster than the mouse can be moved so there is no good way to prevent that from happening. However the primary issue is that the events are restricted to the element they were started from (the center TD) so as soon as you move off that TD the events stop firing. You might be able to make the drag events applicable to the entire document rather than just the TD then use the onmousedown onmouseup events to enable, disable the drag events. This way the drag would not stop until the mouse button was released. They could still drag real fast and release and have it not catch up but it would be better.

Another approach might be to not immediately resize the TD's but to display a ghosted image of the drag bar (center TD) that you can move left/right and once you let go of it you hide the ghosted version and use those coordinates to set the size of the table cells all at once.



Paranoid? ME?? Who wants to know????
 
Can you give me an example of how to do the "ghosted" image?

I have seen this done before but was unable to find a source code example
 
Essentially you create a div on the page that is the same height and width of your center TD. Probably you can just set it up as a table with only the one cell set to the same properties as the main one and set some soft colors as the borders to give it that ghosted effect.
You would set the div style to hidden and set it's positioning absolute so it would effectively float right over top of the other table.

You toggle the display property for that div visible/hidden as needed and move it fairly similarly to how you were resizing your table cells but you just change the style properties style.left, style.top or style.pixelLeft, style.pixelTop (depending on which browser you are using) to reposition the div.

If I get some time I will see if I can work up a sample, I have done this before but do not know it well enough to pound something out off the top of my head.


Paranoid? ME?? Who wants to know????
 
I did some testing and I cannot get any drag code to work well when iframes are included in the table. Remove the iframes from the table and things work smoothly. I am not sure why they introduce so much delay in the code execution but it screws up my code for just dragging a small object across the page as well.



Paranoid? ME?? Who wants to know????
 
The problem is that if (or when) the cursor moves over the iframe, it is then over a different document than the one where the event occured. Since the events dont populate down the document hierarchy it doesn't think there is a mousemove on the page.

I solved this problem by playing a transparent div 100px wide under the cursor in the onmousedown handler, which seems to give more than enough room when dragging, and since the transparent div resides on the parent document, the cursor is always over the div and never actually enters one of the child documents, just have to make sure you move the div along w/ the cursor in the mousemove event

This isn't a perfect solution by any means, but it gets the job done for me

Thanks for you input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top