pixelTop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Payne

    #1

    pixelTop

    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:absolu te;
    }
    ..myBody
    {
    width:100px;
    height:50px;
    background-color:red;
    position:absolu te;
    top:50;
    }
    </style>
    </head>
    <body>
    <script language="javas cript">
    function myMove()
    {
    myBody.style.pi xelTop = myBody.style.pi xelTop + 1;
    }
    </script>

    <div id="myHead" class="myHead" />
    <span id="myBody" class="myBody" onmousedown="my Move()"/>
    </body>
    </html>
  • mpayne@qualtech-int.com.au

    #2
    Re: pixelTop

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

    Comment

    • RobG

      #3
      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
      getComputedStyl e 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/getComputedStyl e, 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:absolu te;
      > top:50;[/color]

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

      language is depreciated, type is required

      <script type="text/javascript">
      [color=blue]
      > function myMove()
      > {
      > myBody.style.pi xelTop = myBody.style.pi xelTop + 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="my Move()"/>[/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.to p;
      if ( x && (x = x.replace(/\D/g,'')) );
      myBody.style.to p = +x + 50 + 'px';
      }
      </script>

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

      --
      Rob

      Comment

      • Hallvord R. M. Steen

        #4
        Re: pixelTop

        On Sat, 30 Apr 2005 16:07:11 +0200, RobG <rgqld@iinet.ne t.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
        Faster, safer and smarter than default browsers. Fully-featured for privacy, security, and so much more. Get the faster, better Opera browser for free.

        Comment

        • RobG

          #5
          Re: pixelTop

          Hallvord R. M. Steen wrote:[color=blue]
          > On Sat, 30 Apr 2005 16:07:11 +0200, RobG <rgqld@iinet.ne t.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

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            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.or g/>
            <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

            Comment

            • mpayne@qualtech-int.com.au

              #7
              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.

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                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

                Comment

                Working...