Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:15:29+00:00 2026-06-15T11:15:29+00:00

I am doing a small JavaScript animation hoping that the little div can move

  • 0

I am doing a small JavaScript animation hoping that the little div can move along a sine wave, and for the moment the horizontal path works fine (just straight line). I am almost sure that my math formula for the Y axis is wrong. I have tried to correct it with some examples I found, but none of them worked for me. In all the possibilities I tried, the Y axis is ignored and the little box just moves in straight line horizontally.

How can I fix this, so the movement goes along a sine wave? I know that it’s possible to do it easier with jQuery or using html 5, but I just got wondering what is wrong in my original code… I would prefer to fix this if possible.

function animate() {
    xnow = item.style.left;
    item.style.left = parseInt(xnow)+1+'px';
    ynow = item.style.top;
    item.style.top = ynow + (Math.sin((2*Math.PI*xnow)/document.width))*document.heigth;
    setTimeout(animate,20); 
}

The complete code here:
JSFiddle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:15:30+00:00Added an answer on June 15, 2026 at 11:15 am

    I see several problems with your code:

    • xnow contains a string in this format: ###px You cannot multiply it, so use parseInt() in your Math.sin() call.
    • Same goes for your code to grab ynow, it needs parseInt().
    • Better is to use other (global) variables to store the x and y coordinates as numbers. And add px when you update coordinates of the div-element.
    • When you multiply 2*Math.PI with xnow (which contains only integer numbers), the sin() function will always return 0. So you won’t get a sine-like movement. You need to divide xnow by the number of x-steps you want to use to do a complete sine-like movement
    • Math.sin() returns a value between -1 and +1, so you need to multiply it by an amplitude to see a (more clear) effect.

    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):

    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);
    }
    

    As mentioned: it is much better to use some global variables containing the values (instead of using parseInt() all the time)

    See my updated JSFiddle example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a small JavaScript (framework) that can chain functions with all
I'm doing jquery small animation of fade in to form validation This is working
I need to build a small JavaScript library that would only be a couple
I'm making a small test framework that uses the JavaScript module pattern for UI
I have a client that I am doing a very small project for. His
I'm doing an application in Django that has a small map in the user
I'm trying to write a small piece of Javascript that will sort through a
I'm doing a small project in Django by using Python. In that, I am
I'm doing a small javascript method, which receive a list of point, and I've
I'm writing a small web that just makes some animation and shows some information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.