Display same image at different places in a window

Y

Yves PEYSSON

Hi, I want to display the same image (from the same file) at different
positions in a window. Up to now I used DIV command in the body part of the
text, and document.getElementById in order to place it at the right place.
But when I do that several times, only the last position is displayed. How
to show all positions ?
Thanks
Yves
 
J

Janwillem Borleffs

Yves PEYSSON said:
Hi, I want to display the same image (from the same file) at different
positions in a window. Up to now I used DIV command in the body part of the
text, and document.getElementById in order to place it at the right place.
But when I do that several times, only the last position is displayed. How
to show all positions ?

Perhaps the following is helpful:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
window.onload = function () {
var clone = document.getElementById('logo').cloneNode(true);
document.getElementsByTagName('BODY')[0].appendChild(clone);
clone.style.position = 'absolute';
clone.style.left = '10px';
clone.style.top = '200px';
}
</script>
</head>

<body>
<img id="logo" src="http://www.google.nl/intl/nl_nl/images/logo.gif" />
</body>
</html>


JW
 
M

Michael Winter

Yves PEYSSON wrote on 13 Dec 2003 at Sat, 13 Dec 2003 21:17:27
GMT:
Hi, I want to display the same image (from the same file) at
different positions in a window. Up to now I used DIV command in
the body part of the text, and document.getElementById in order
to place it at the right place. But when I do that several
times, only the last position is displayed. How to show all
positions?

A single DIV can only be in one place at one time. Create multiple
DIVs with different IDs and place them wherever it is you want.

Mike
 
T

Thomas 'PointedEars' Lahn

Yves said:
Hi, I want to display the same image (from the same file) at different
positions in a window. Up to now I used DIV command in the body part of the
text,

<prayer_wheel>
<acronym title="HyperText Markup Language">HTML</acronym>
is not a programming language. There are no HTML commands.
</> [psf 7.9]

You mean a `div' _element_.
and document.getElementById

That is why the method is named that way.
in order to place it at the right place.

document.getElementById(...) only retrieves a reference to
the HTML element object, if there is any. Do not use

document.getElementById("foobar").bla = foo;
document.getElementById("foobar").blubb = bar;
...

but use

if (document.getElementById) // make sure it is supported
{
var o = document.getElementById("foobar");
if (o)
{
o.bla = foo;
o.blubb = bar;
}
}

instead.
But when I do that several times, only the last position is displayed.
How to show all positions ?

Since you do not show *any* snippet of your code(!), I can only *guess*
that you confront a common processing problem with animations. If you
try to accomplish such like

function Position(x, y)
{
this.x = x;
this.y = y;
}

var a = [new Position(1, 2), ...];

for (var i = 0; i < a.length; i++)
{
referenceToDivElement.style.left = a.x + "px";
referenceToDivElement.style.top = a.y + "px";
}

it seems like the `div' element is placed only at the last position.
That is because the CPU is far to fast for the graphics card and the
host, or preceding assignments are removed by internal optimization.

If you do

function setPos(x, y)
{
referenceToDivElement.style.left = x + "px";
referenceToDivElement.style.top = y + "px";
}

for (var i = 0; i < a.length; i++)
{
setTimeout("setPos(", a.x + ", " + a.y + "), 42);
}

it may also seem like the `div' element is placed only at the last
position. That is because you plan to call setPos(...) with different
values but *at* *the* *same* *time*. For there cannot be a "same time"
in non-quantum computing, the calls are planned that the first, using
a[0].x and a[0].y, is called after 42 milliseconds and the following
the next instant (small number of CPU cycles) which is either too fast
or again removed by internal optimization.

Note:
setTimeout(...) *does* *not* cause the script host to wait for an
expression to be evaluated and neither does setInterval(...).

To accomplish animations with ECMAScript implementations, you need to
plan the calls at multiples of the frame length instead:

for (var i = 0; i < a.length; i++)
{
setTimeout("setPos(", a.x + ", " + a.y + "), i * 42);
}

If you have an animation with variable frame length, you need to use
the offset to the first call for successive calls. A function that
calculates the offsets helps you to stay with your current data
structure *and* to provide useful values for setTimeout(...), e.g.:

function getOffsets(aLenghts)
{
var a = [0];
var nOffset = 0;

for (var i = 0; i < a.length; i++)
{
nOffset += a;
a[a.length] = nOffset;
}

return a;
}

var aFrameLengths = [11, 23, 42, 64, 10, 15];

// calculate array elements can be used
// as argument for setTimeout(...)
var aOffsets = getOffsets(aFrameLengths);

Note:
I used a frame length of 42 milliseconds only for entertainment.
Frame lengths below 50 milliseconds can significantly and thus
noticeably slow down the host and are not recommended in Web
applications. The acceptable minimum varies, depending on the
timed action(s) taken and on the host where the script is running
on.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Lasse said:
@SM said:
Thomas 'PointedEars' Lahn a ecrit :
</> [psf 7.9]

Sorry ?
What that means ?

It is a reference to his page of standard comments, which is pretty
hard to guess when he doesn't give a link

There is no need to guess anything. First, it is merely used as a kind
of trademark *following* my standard _phrases_. Second, you can (and
should, because it is off-topic!) ask me via e-mail about it.
(preferably to a page not in German).

Since I am now more active in the Big8, I have almost finished the
translation into English. Those who want to be informed about the
publication, just drop me a note (both From and Reply-To are of
course always valid, Reply-To is preferred.)

This is a resource redirected to, use

<http://pointedears.de.vu/psf/#net>

instead (as written in the document), for the university account
is volatile and the first URL is likely to become invalid.


F'up2 poster

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas 'Ingrid' Lahn wrote:

function getOffsets(aLenghts)
{
var a = [0];
var nOffset = 0;
- for (var i = 0; i < a.length; i++)
+ for (var i = 0; i < aLengths.length; i++)
{
- nOffset += a;
+ nOffset += aLengths;
a[a.length] = nOffset;
}

return a;
}


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
Thomas 'Ingrid' Lahn wrote:

function getOffsets(aLenghts)
{
var a = [0];
var nOffset = 0;
- for (var i = 0; i < a.length; i++)
+ for (var i = 0; i < aLengths.length; i++)
{
- nOffset += a;
+ nOffset += aLengths;
a[a.length] = nOffset;
}

return a;
}


The code is still, it seems, not tested code.

Code should be inserted in a News article by copy'n'paste from a tested
version, except where trivial.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top