Connecting Tech Pros Worldwide Forums | Help | Site Map

pixelTop

Martin Payne
Guest
 
Posts: n/a
#1: Jul 23 '05
This is doing my head in. In my code below, I set the style of a div
so that its position is absolute and top = 50. When I view it in my
browser it is positioned correctly. But, if I use pixelTop (or top)
to change the position by adding one, my first mouse click is moving
the div to top = 1, instead of top = 51!

I've done some investigating using alert and it seems that height, top
and pixelTop all have an initial value of zero until I set them with
JS??!

What am I doing wrong?

Thanks
Martin

<html>
<head>
<style>
..myHead
{
width:100px;
height:50px;
background-color:blue;
position:absolute;
}
..myBody
{
width:100px;
height:50px;
background-color:red;
position:absolute;
top:50;
}
</style>
</head>
<body>
<script language="javascript">
function myMove()
{
myBody.style.pixelTop = myBody.style.pixelTop + 1;
}
</script>

<div id="myHead" class="myHead" />
<span id="myBody" class="myBody" onmousedown="myMove()"/>
</body>
</html>



mpayne@qualtech-int.com.au
Guest
 
Posts: n/a
#2: Jul 23 '05

re: pixelTop


Oh, BTW, I'm looking at it in IE6.

RobG
Guest
 
Posts: n/a
#3: Jul 23 '05

re: pixelTop


Martin Payne wrote:[color=blue]
> This is doing my head in. In my code below, I set the style of a div
> so that its position is absolute and top = 50. When I view it in my
> browser it is positioned correctly. But, if I use pixelTop (or top)
> to change the position by adding one, my first mouse click is moving
> the div to top = 1, instead of top = 51!
>
> I've done some investigating using alert and it seems that height, top
> and pixelTop all have an initial value of zero until I set them with
> JS??!
>
> What am I doing wrong?[/color]

using style.top will get the value if set inline, but not via CSS
style sheet rule.

Do a search in this group for getCurentStyle (which works for IE) or
getComputedStyle for other browsers, there are a number of examples
on how to use them.

'top' is set in units, % or auto. The units may be px or anything
else a browser understands.

To avoid using getCurrentStyle/getComputedStyle, set 'top' inline in
the element tag. The value for 'top' will include the units that you
set it with. If you've set it to 50px, the value is '50px', not '50'.
So strip off the 'px', add your increment, then put it back on.

See below.

[...][color=blue]
> {
> width:100px;
> height:50px;
> background-color:red;
> position:absolute;
> top:50;[/color]

top: 50px;
[color=blue]
> }
> </style>
> </head>
> <body>
> <script language="javascript">[/color]

language is depreciated, type is required

<script type="text/javascript">
[color=blue]
> function myMove()
> {
> myBody.style.pixelTop = myBody.style.pixelTop + 1;[/color]

Using an id as a global variable is a Microsoft-ism. Since the
script is initiated by a click on the element you want, why not pass
'this' to send a reference directly to the script (see below)?
[color=blue]
> }
> </script>
>
> <div id="myHead" class="myHead" />
> <span id="myBody" class="myBody" onmousedown="myMove()"/>[/color]

Rather than 'onmousedown', why not 'onclick'?

[...]

The following is just an example, it moves the element by 50 pixels
each time.

<script type="text/javascript">
function myMove(myBody) {
var x = myBody.style.top;
if ( x && (x = x.replace(/\D/g,'')) );
myBody.style.top = +x + 50 + 'px';
}
</script>

<div id="myHead" class="myHead">
<span id="myBody" class="myBody" style="top: 50px;"
onmousedown="myMove(this);">
</span>
</div>

--
Rob
Hallvord R. M. Steen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: pixelTop


On Sat, 30 Apr 2005 16:07:11 +0200, RobG <rgqld@iinet.net.auau> wrote:
[color=blue]
> if ( x && (x = x.replace(/\D/g,'')) );[/color]

I would suggest a parseInt() instead of that regexp. Any specific reason
why you used replace instead?

--
Hallvord R. M. Steen
http://www.opera.com/
RobG
Guest
 
Posts: n/a
#5: Jul 23 '05

re: pixelTop


Hallvord R. M. Steen wrote:[color=blue]
> On Sat, 30 Apr 2005 16:07:11 +0200, RobG <rgqld@iinet.net.auau> wrote:
>[color=green]
>> if ( x && (x = x.replace(/\D/g,'')) );[/color]
>
>
> I would suggest a parseInt() instead of that regexp. Any specific
> reason why you used replace instead?
>[/color]

parseInt is easy to misuse - though probably not in this case. top
is returned as a string, so I figured it'd modify it as a string. It
is possible that the units will be returned as a decimal number with
a unit(say 2.5em) in which case parseInt will not work correctly, but
a slight modification to the above will:

if ( x && (x = x.replace(/\D*$/g,'')) );

Probably best to use parseFloat, as that will deal with floats and
ints - use what suits.


--
Rob
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 23 '05

re: pixelTop


Martin Payne wrote:
[color=blue]
> This is doing my head in. In my code below, I set the style of a div
> so that its position is absolute and top = 50. When I view it in my
> browser it is positioned correctly. But, if I use pixelTop (or top)
> to change the position by adding one, my first mouse click is moving
> the div to top = 1, instead of top = 51!
> [invalid HTML/CSS code][/color]

<http://validator.w3.org/>
<http://jigsaw.w3.org/css-validator/>

Come back and complain about the UA when it's Valid.


PointedEars
--
In the First World War, 13 million people were killed. In the Second
World War, 40 million people were killed. I think that if a third war
takes place, nothing is going to be left on the face of earth.
-- Shakira, 2003-02-05 @ MTV.com
mpayne@qualtech-int.com.au
Guest
 
Posts: n/a
#7: Jul 23 '05

re: pixelTop


Can ANYONE see a complaint in my post??!!!

PointedEars, next time, unless you are actually going to respond to my
specific question and not make some unwanted wanker comment, don't
bother responding at all.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#8: Jul 23 '05

re: pixelTop


mpayne@qualtech-int.com.au wrote:
[color=blue]
> Can ANYONE see a complaint in my post??!!![/color]

Your `?' and `!' keys are borken. Regarding your "question", here you are:

| But, if I use pixelTop (or top) to change the position by adding one, my
| first mouse click is moving the div to top = 1, instead of top = 51!
|
| I've done some investigating using alert and it seems that height, top
| and pixelTop all have an initial value of zero until I set them with
| JS??!
[color=blue]
> PointedEars, next time, unless you are actually going to respond to my
> specific question and not make some unwanted wanker comment, don't bother
> responding at all.[/color]

Firstly, this is not a paid support forum, you don't have any right to
get any answer (to your question); read the FAQ. Secondly, a client-side
script executed on invalid markup is not supposed to work (among other
things, a missing DOCTYPE declaration enforces Quirks/Compatibility Mode).

But apparently you are too incompetent to recognize that. Go away.


PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
Closed Thread