Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see several problems with your code:</p> <ul> <li><code>xnow</code> contains a string in this format: <code>###px</code> You cannot multiply it, so use <code>parseInt()</code> in your <code>Math.sin()</code> call.</li> <li>Same goes for your code to grab <code>ynow</code>, it needs <code>parseInt()</code>.</li> <li>Better is to use other (global) variables to store the x and y coordinates as numbers. And add <code>px</code> when you update coordinates of the div-element.</li> <li>When you multiply <code>2*Math.PI</code> with <code>xnow</code> (which contains only integer numbers), the <code>sin()</code> function will always return <code>0</code>. So you won't get a sine-like movement. You need to divide <code>xnow</code> by the number of x-steps you want to use to do a complete sine-like movement</li> <li><code>Math.sin()</code> returns a value between -1 and +1, so you need to multiply it by an amplitude to see a (more clear) effect.</li> </ul> <p>To keep it as much as you designed it, it would become something like this (takes 50 x-movement steps to do a complete sine and uses an amplitude of 10 pixels):</p> <pre><code>function animate() { xnow = parseInt(item.style.left); item.style.left = (xnow+1)+'px'; ynow = parseInt(item.style.top); item.style.top = (ynow+Math.sin(2*Math.PI*(xnow/50))*10) + "px"; setTimeout(animate,20); } </code></pre> <p>As mentioned: it is much better to use some global variables containing the values (instead of using <code>parseInt()</code> all the time)</p> <p>See my <a href="http://jsfiddle.net/twNcy/5/" rel="nofollow">updated JSFiddle example</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload