1

editing this page for those who want to read this in the future :P

seems i'm asking too much here.. but thanks a lot for those who helps me with this problem ! actually i want to accept all of your answer, but it seems i only can accept one.. >.>

Question 1:
i want to move an image, but when i try to get the image position(using document.getElementById().style.left), it shows me a NaN value when i convert it to integer. i wonder how to fix this :/

here's my code:

<script type="text/javascript">

    function playit()
    {
        alert(parseInt(document.getElementById('ball').style.left)); <!-- shows a NaN value -->
    }
</script>

i put the image into table and call it using onclick function; <td id="frame" colspan="5" onclick="playit()"> to be exact and i declared the image like this: <img src="play/ball.png" id="ball" name="ball"/>

here's my CSS for the picture:

#ball
{
    position:absolute;
    top:500px;
    left:500px; 
}

SOLUTION(given by VIPIN JAIN): use .offsetLeft instead of element.style.left




Question 2:
EDIT: i fixed it with .offsetLeft and it works just fine. now when i'm trying to move the image, i got another error. It's said that i'm using too much recursion. I know it happens because i'm using a recursion, but i wonder, what's wrong with using recursion?

here's my code:

<script type="text/javascript">

    <script type="text/javascript">

    var lball = document.getElementById('ball').offsetLeft;
    var speed = 500;

    function moveleft()
    {
        if(document.getElementById('ball').offsetLeft >0)
        {
            document.getElementById('ball').offsetLeft-=1;

            setTimeout(moveleft(), speed);
        }
        else gameover();
    }
    function playit()
    {
        score = 0;
        setTimeout(moveleft(), speed);
    }
</script>

can someone help me with this? thanks :)

SOLUTION(given by Teemu): please refer to Teemu's comment below on 'Edit II' part.


Question 3(actually i'm asking it on the comments :P)
when i tried to set the image position using document.getElementById('ball').offsetLeft-=1;, it shows an undefined value.

SOLUTION(given by Kemal Fadillah): "That's because you can't set the offsetLeft value of an element. You have to set the new position value to .style.left. Something like this: element.style.left = new_value + "px";"


Question 4(again, i'm asking it on the comments :P)
it seems that the image won't move smoothly(it looks like it is suddenly "warps" to another place). i have raised the speed value but it still not working. am i missing something with it? i wonder.. :/

SOLUTION(given by Kemal Fadillah): "Set speed to 10 or 20. That should make the animation smooth enough. To make the image moves faster, instead of decreasing the left offset by 1, try to decrease it by 3."

Community
  • 1
  • 1
aquatorrent
  • 801
  • 2
  • 8
  • 11
  • 1
    If the CSS property `left` is not set, `document.getElementById('ball').style.left` will return an empty string. And if you pass an empty string to `parseInt()` it will give you `NaN`. – Kemal Fadillah Apr 16 '12 at 11:16
  • @KemalFadillah i have using it on the CSS. here's my CSS for the ball: #ball { position:absolute; top:500px; left:500px; } – aquatorrent Apr 16 '12 at 11:22
  • @KemalFadillah This might be a cross-browser issue. IE humbly gives me all style values defined in external stylesheets too. – Teemu Apr 16 '12 at 12:07

4 Answers4

1

You should use jQuery, it's much easier. Try something like

$(function(){
$("img#ball").offset({top: 10px, left: 500px});
});

More information can be found here:

http://api.jquery.com/offset/

information about jQuery in general can be found here:

http://jquery.com/

Rob Angelier
  • 2,335
  • 16
  • 29
1

This requires offsetLeft and offsetTop values please refer to the link

This post

Community
  • 1
  • 1
Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
1

document.getElementById('ball').style.left returns '' (or undefined), if you haven't set the property before retrieving it. That's why you get NaN from parseInt().

Edit:

Seems you're style definition is OK (after you edited your post). You can try to use the radix in the parseInt():

alert(parseInt(document.getElementById('ball').style.left,10));

This is documentated as not oblique, but using it sometimes helps.

Edit II

This happens, because you're executing all setTimeouts twice, and you're not clearing them. Try it this way:

var delay;

function moveLeft(){
  :
  if(delay){ // Add this if before all of your setTimeout() calls and in gameover() too
    clearTimeout(delay);
  }
  delay=setTimeout(moveLeft, speed);
  :
}

By setting timeout like this setTimeout(doFunction(),delaytime); executes doFunction rightaway because of (), and again after delaytime, that really is too much of recursion...

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • i have tried it and it's not working either; it still shows a NaN value. i wonder what radix means anyway :/ – aquatorrent Apr 16 '12 at 11:45
  • @aquatorrent I'm not sure if I can explain this in english, but... When you use `16` as a radix, you'll get a hex-value, if you'll use `2` as a radix you'll get a binary value etc. For example: `parseInt('100',2)` returns `4`, as `parseInt('100',10)` returns `100`. – Teemu Apr 16 '12 at 11:54
  • oh, so it's like converting the integer to another type of integer? – aquatorrent Apr 16 '12 at 12:18
  • @aquatorrent Yes, that's the purpose. Did you got your code to work? If you didn't, please use `offsetLeft` instead of `style.left` like VIPIN JAN has proposed. – Teemu Apr 16 '12 at 12:23
  • oh, i didn't notice the link before XD i have checked it and i confused about the `el` . what type is it? is it the same type as `document.getElementById` ? – aquatorrent Apr 16 '12 at 12:50
  • @aquatorrent Hard to say without the code, there is no `XD` or `el` in the code you posted. Please add the whole (relevant) code to your question. At least the `head`-part of the HTML, and all script&HTML dealing with your problem. This seems to be more like issue of timing or scope. – Teemu Apr 16 '12 at 13:01
  • i have edited it and it works! :D but i got another error with recursion.. i have updated my code though :D – aquatorrent Apr 16 '12 at 13:18
  • thanks for the code, it's working! ^^ now i know that i have to use cleartimeout first before setting another timeout :) – aquatorrent Apr 16 '12 at 16:28
1

You can't get the CSS styling by using element.style if it's declared inside an external stylesheet. From MDN Documentation:

However, it is not useful for learning about the element's style in general, since it represents only the CSS declarations set in the element's inline style attribute, not those that come from style rules elsewhere, such as style rules in the section, or external style sheets.

https://developer.mozilla.org/en/DOM/element.style

Instead, you'll have to use window.getComputedStyle() to get the properties.

window.getComputedStyle(document.getElementById('ball')).left; // returns 500px
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • thanks, it works! now i'm trying to set the value to a variable. but it doesn't work. can't i use parseInt() to parse it? here's how i do it: var ball = parseInt(window.getComputedStyle(document.getElementById('ball'))); – aquatorrent Apr 16 '12 at 12:00
  • you forgot `.left` at the end of the `window.getComputedStyle()` call. – Kemal Fadillah Apr 16 '12 at 12:04
  • i add `.left` as you said earlier, but it gives me an undefined value. is it like this? `var lball = parseInt(window.getComputedStyle(document.getElementById('ball')).left);` – aquatorrent Apr 16 '12 at 12:11
  • Is the page hosted on a live server so I can see it? Or perhaps you can post a JsFiddle instead? – Kemal Fadillah Apr 16 '12 at 12:27
  • it's not hosted on a live server.. and what is JsFiddle? i just learned to code javascript some day ago and have no clue about lots of things.. sorry >.> – aquatorrent Apr 16 '12 at 12:46
  • @aquatorrent Nevermind then. Can you tell which part is returning undefined? To my knowledge, `parseInt()` only returns either an integer or `NaN` and not `undefined`. – Kemal Fadillah Apr 16 '12 at 12:57
  • first, i declare this: `var lball = parseInt(window.getComputedStyle(document.getElementById('ball')).left);` then in `playit` function i'm using this: `alert(lball);` – aquatorrent Apr 16 '12 at 13:03
  • Can you update the question and add the updated code? There might be some issue with scoping. – Kemal Fadillah Apr 16 '12 at 13:05
  • i just change it with `.offsetleft` and it works! but it seems i got another error.. and i have updated the code :) – aquatorrent Apr 16 '12 at 13:19
  • That's because you can't set the `offsetLeft` value of an element. You have to set the new position value to `.style.left`. Something like this: `element.style.left = new_value + "px";`. – Kemal Fadillah Apr 16 '12 at 13:28
  • oh i see. i have changed it but i got another problem. it seems that it won't move smoothly(it looks like the picture is suddenly "Warps" to another place). i have raised the speed value but it still not working. am i missing something with it? i wonder.. :/ – aquatorrent Apr 16 '12 at 13:56
  • Set `speed` to 10 or 20. That should make the animation smooth enough. To make the image moves faster, instead of decreasing the left offset by 1, try to decrease it by 3. – Kemal Fadillah Apr 16 '12 at 14:22