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 7511099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:26:50+00:00 2026-05-29T23:26:50+00:00

I have a Javascript file that I am using to try to animate a

  • 0

I have a Javascript file that I am using to try to animate a dropdown menu. I have the “Toggle” function in that file set to run when I click on a certain div. Here’s the script I’m using:

var up = true;
        function Toggle(x)
    {
        if (up)
        {
            for (var i = x.offsetTop; i <= 0; i++)
            {
                x.style.top = i;
                if (i == 0)
                {
                    up = false; 
                }
            }
        }

        else if (up == false)
        {
            for (var i = x.offsetTop; i >= -50; i--)
            {
                x.style.top = i;
                if (i == -50)
                {
                    up = true;
                }
            }
        }
    }

In the HTML div I want to animate, I have the “onclick” property set to “onclick=Toggle(this)”. The first for loop works as it should (it sets the div’s top offset to 0). However, the second for loop doesn’t set the offsetTop. I know that the for loop is activating because I’ve tested it and it gives me every integer between 0 and -50. Why isn’t it setting the offset position?

  • 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-05-29T23:26:51+00:00Added an answer on May 29, 2026 at 11:26 pm

    1) You must specify a unit to the top ie: x.style.top = i +"px"

    2) Your function won’t animate instead of you use a setInterval or a setTimeout

    Like you asked, an example. I wouldn’t do it like this for one of my project, but i kept your function to make you more familiar with the code.
    I Used setTimeout instead of setInterval because setInterval must be cleared when not needed and setTimeout is just launched one time :

    var Toggle = (function() { // use scope to define up/down
        var up = true; 
        return function(element) {
            var top = parseInt(element.style.top, 10); // element.offsetTop ?
            if ( !top ) {
                top = 0;
            }
    
            if (up) {
                if (element.offsetTop < 0) { // if we are not at 0 and want to get up
                    element.style.top = (top+1) + "px";
                    setTimeout(function() { Toggle(element); }, 10); // recall the function in 10 ms
                } else { // we change up value
                    up = !up;  
                }
            }
            else {
                if (element.offsetTop > -50) {
                    element.style.top = (top-1) + "px";
                    setTimeout(function() { Toggle(element); }, 10); // recall the function in 10 ms
                } else {
                    up=!up;
                }
            }
        }
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that loads an xml file using javascript: function getXmlDocument(sFile) {
I have some code in umbraco that is run and creates a javascript file
I have a javascript file that reads another file which may contain javascript fragments
I have a lengthy JavaScript file that passes JSLint except for used before it
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have some code in a javascript file that needs to send queries back
I have a .tag file that requires a JavaScript library (as in a .js
I have a piece of jQuery that requests a file (using .load() method) with
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have an AS2 Flash SWF that is calling another AS2 Flash File using

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.