473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Function to move a DIV?

Hi,

Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?

Thanks, - Dave

Oct 25 '06 #1
10 3022
ASM
la***********@zipmail.com a écrit :
Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?
Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);
}

--
ASM
Oct 25 '06 #2

ASM wrote:
la***********@zipmail.com a écrit :
Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?

Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);
The last line is unnecessary unless the OP has a reason to remove the
element from its current position in the DOM tree and place it the last
child of the body element. I can't see any benefit in that, it has the
detraction of making it more difficult to return the element to its
original position (though that hasn't been stated as a requirement).
--
Rob

Oct 26 '06 #3
Rik
RobG wrote:
ASM wrote:
>la***********@zipmail.com a écrit :
>>Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?

Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);

The last line is unnecessary unless the OP has a reason to remove the
element from its current position in the DOM tree and place it the
last child of the body element. I can't see any benefit in that, it
has the detraction of making it more difficult to return the element
to its original position (though that hasn't been stated as a
requirement).
Well, the OP sais 'on the screen', and I think ASM correctly changed that
to 'in the window', as it is not really possible to leave the browser
window. There could be several parent elements for that div, that have
either position absolute or relative, which will affect the positioning for
the div itself. It will be relative to them, not to the window. By making
sure it's a direct child from the body it will be positioned on the
position the OP probably wants.
--
Rik Wasmus
Oct 26 '06 #4

Rik wrote:
RobG wrote:
ASM wrote:
la***********@zipmail.com a écrit :
Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?

Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);
The last line is unnecessary unless the OP has a reason to remove the
element from its current position in the DOM tree and place it the
last child of the body element. I can't see any benefit in that, it
has the detraction of making it more difficult to return the element
to its original position (though that hasn't been stated as a
requirement).

Well, the OP sais 'on the screen', and I think ASM correctly changed that
to 'in the window', as it is not really possible to leave the browser
window.
I have no problem with that.
There could be several parent elements for that div, that have
either position absolute or relative, which will affect the positioning for
the div itself. It will be relative to them, not to the window. By making
sure it's a direct child from the body it will be positioned on the
position the OP probably wants.
As I said: "unless the OP has a reason to remove the element from its
current position in the DOM tree". Further discussion regarding style
should be conducted in comp.infosystems.www.authoring.stylesheets.

--
Rob

Oct 26 '06 #5
ASM
RobG a écrit :
ASM wrote:
>la***********@zipmail.com a écrit :
>>Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?
Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);

The last line is unnecessary unless the OP has a reason to remove the
element from its current position in the DOM tree and place it the last
child of the body element.
The goal, it I did right understand, is to place the div in absolute in
*front view*.
The easiest way to get this feature is to place it in last position in
document, automatically that gives to it the highest z-index.

(I do not know if there is another absolute div at this position
declared farther in the html code)
detraction of making it more difficult to return the element to its
original position (though that hasn't been stated as a requirement).
Continuation(suite) in the next serial :-)

--
ASM
Oct 26 '06 #6
ASM
RobG a écrit :
As I said: "unless the OP has a reason to remove the element from its
current position in the DOM tree". Further discussion regarding style
should be conducted in comp.infosystems.www.authoring.stylesheets.
N'importe quoi !

You can't serial interactions this way :-(
To want to act on dom/html structure for resulting appearance at screen
without managing css means nothing.

--
ASM
Oct 26 '06 #7
Hi,
Just for the heck of it, I tried out this code, and it does not work for my
situation on IE6. Works OK on FF and Opera.
Here's the scenario:
3 absolutely positioned DIV's that simulate a framed page:
1: A nonscrolling #header
2. A scrolling #middle with overflow:auto
3. A nonscrolling #footer
When this type of page is viewed as a "popup" from another page, I only want
the middle to be visible.
So, in the onload code, I sense if parent!=self and set
header.style.display='none' and footer.style.display='none' .
Now this is ALL I have to do in FF and Opera, i.e., the middle is reflowed
so that it appears at the top and occupies the entire popup.
With IE, the top and bottom DIV's are not displayed, but their whitespace is
still viewable (acts as if visibility='hidden' vs. display='none')
So I attempted to use your moveDiv function to translate the middle DIV to
the top-left corner of the popup.
Didn't work
The only way I can get the desired effect is by using an alternate style
sheet for the popup situation (uses 'document.write's for the conditional
links to the different CSS's in the <headsection). In that CSS the hidden
DIV's have display: none.
Is there something going on with IE that I don't understand?
Mike
"ASM" <st*********************@wanadoo.fr.invalidwrote in message
news:45***********************@news.orange.fr...
RobG a écrit :
>ASM wrote:
>>la***********@zipmail.com a écrit :
Does anyone have a cross-browser function that given an id of a DIV
element, can move the DIV to an absolute x,y position on the screen?
Not on the screen, but in the window :

function moveDiv(id,x,y) {
id = document.getElementById(id);
id.style.position = 'absolute';
id.style.top = y+'px';
id.style.left = x+'px';
document.body.appendChild(id);

The last line is unnecessary unless the OP has a reason to remove the
element from its current position in the DOM tree and place it the last
child of the body element.

The goal, it I did right understand, is to place the div in absolute in
*front view*.
The easiest way to get this feature is to place it in last position in
document, automatically that gives to it the highest z-index.

(I do not know if there is another absolute div at this position declared
farther in the html code)
>detraction of making it more difficult to return the element to its
original position (though that hasn't been stated as a requirement).

Continuation(suite) in the next serial :-)

--
ASM

Oct 26 '06 #8
ASM
Mike a écrit :
Hi,
Just for the heck of it, I tried out this code, and it does not work for my
situation on IE6. Works OK on FF and Opera.
so for your popup you just need :

truc = false;
function pop() {
if(!truc || truc.closed)
truc= window.open('','','width=700,height=500,resizable= 1');
truc.onload = function() {
truc.document.getElementById('header').style.visib ility='hidden';
truc.document.getElementById('footer').style.displ ay='none';
truc.document.getElementById('middle').style.posit ion='relative';
truc.document.getElementById('middle').style.top=0 ;
truc.document.getElementById('middle').style.left= 0;
truc.document.getElementById('middle').style.margi nTop=0;
truc.document.getElementById('middle').style.margi nLeft=0;
}
truc.location = self.location;
truc.focus();
}
So I attempted to use your moveDiv function to translate the middle DIV to
the top-left corner of the popup.
Didn't work
You also can try to *delete* div 'header'

var H = truc.document.getElementById('header');
while(H.firstChild) H.remove(H.firstChild);

then to position 'middle' in relative with margin to 0

and, of course delete 'footer'

--
ASM

Oct 26 '06 #9
Thanks for your help.
The "problem" was that IE was retaining initial style values (borderTop and
borderBottom) while the other browsers apparently didn't or reset them.
Soon as set those to zero, the other settings took effect.
Almost seems "cleaner" to use an alternate style sheet than twiddling around
with properties.
Mike

"ASM" <st*********************@wanadoo.fr.invalidwrote in message
news:45**********************@news.orange.fr...
Mike a écrit :
>Hi,
Just for the heck of it, I tried out this code, and it does not work for
my situation on IE6. Works OK on FF and Opera.

so for your popup you just need :

truc = false;
function pop() {
if(!truc || truc.closed)
truc= window.open('','','width=700,height=500,resizable= 1');
truc.onload = function() {
truc.document.getElementById('header').style.visib ility='hidden';
truc.document.getElementById('footer').style.displ ay='none';
truc.document.getElementById('middle').style.posit ion='relative';
truc.document.getElementById('middle').style.top=0 ;
truc.document.getElementById('middle').style.left= 0;
truc.document.getElementById('middle').style.margi nTop=0;
truc.document.getElementById('middle').style.margi nLeft=0;
}
truc.location = self.location;
truc.focus();
}
>So I attempted to use your moveDiv function to translate the middle DIV
to the top-left corner of the popup.
Didn't work

You also can try to *delete* div 'header'

var H = truc.document.getElementById('header');
while(H.firstChild) H.remove(H.firstChild);

then to position 'middle' in relative with margin to 0

and, of course delete 'footer'

--
ASM

Oct 27 '06 #10
ASM
Mike a écrit :
Thanks for your help.
The "problem"
The problem is : never enough infos
It's always better to have a page in line to see exactly all what you have.
From this point it is possible to give a real lighted help.
Almost seems "cleaner" to use an alternate style sheet than twiddling around
with properties.
or a class to

Oct 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
3
by: Dooza | last post by:
Hi there, I was wondering if anyone had come across some javascript that would allow me to have a chart of say 20 music tracks, and be able to move each track up and down the chart using up/down...
3
by: dibblm | last post by:
Below is current code used. I can only list one directory then move to next. I want to search one more directory further and can't seem to find how to get one deeper. What I want to accomplish is...
4
by: VBTricks.de.vu Webmaster | last post by:
Hello everybody, I've been programming using VB6 for some years now and now I want to learn VB.net. One of the first things I'm missing most is the Move-function for controls. Is there no...
6
by: hharry | last post by:
Hello All, Is it possible to force a function argument to fall with a range of values ? For example: I have a function which searches a database table and accepts a query type argument...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
2
by: jasonchan | last post by:
I am trying to have a function start with a timer and then switch to another function. However, it is not working at all. Anyone know whats goin on? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
2
by: ELINTPimp | last post by:
Hello all, Have a really interesting problem (at least to me) with my upload_file() function. I have it working now, with a bit of a work around, but would like to know what everyone thinks in...
5
by: john_woo | last post by:
Hi, If what I want from the on_mouse_move function is, trigger another function in two case: 1. when the mouse holds at same position for 1 second; 2. when the mouse fully stop; in other...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.