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

  • Home
  • SEARCH
  • 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 6163769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:52:50+00:00 2026-05-23T21:52:50+00:00

While I was animating two boxes with jQuery at same pace with setting marginTop,

  • 0

While I was animating two boxes with jQuery at same pace with setting marginTop, the second box will move faster than the first box.

<div id="box1" style="width:500px;height:80px;background-color:blue;">box 1</div>
<div id="box2" style="width:500px;height:80px;background-color:green;margin-left:510px;">box 2</div>


<script type="text/javascript">
    jQuery("#box1").animate({marginTop:"-=80px"}, {duration: 1500});            
    jQuery("#box2").animate({marginTop:"-=80px"}, {duration: 1500});
</script>

However, when I apply the same setting Top, both box move at same pace.

<div id="box1" style="width:500px;height:80px;background-color:blue;position:relative;">box 1</div>
<div id="box2" style="width:500px;height:80px;background-color:green;margin-left:510px;position:relative;">box 2</div>


<script type="text/javascript">
    jQuery("#box1").animate({top:"-=80px"}, {duration: 1500});          
    jQuery("#box2").animate({top:"-=80px"}, {duration: 1500});
</script>

Any idea why this happening this way?

  • 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-23T21:52:50+00:00Added an answer on May 23, 2026 at 9:52 pm

    You can see the effect more clearly in this fiddle

    The reason why it is happening is because your second div is below the first div. And the margin property is relative to the elements surrounding it. The top div will move slower because the element that is above (in this case the body) is not moving. But your div on the bottom has to move faster in order to maintain that margin property relative to the element above it. In other words, it wants to MAINTAIN that property once the animation is finished.

    In my fiddle, each div starts with margin-top: 100px. Now when they are animated, they want to marginTop: "-=80px", which means that they need to end up at margin-top: 20px. In the top elements case, it only needs to move 80px upward, because body isn’t moving. But the bottom div needs to move 80px PLUS the amount that the top div moved, which is another 80px, for a total of 160px. The div needs to move 2x the amount of pixels in the same amount of time, so it will move faster.

    We can further illustrate that the margin property is a relative property from this fiddle. Even though I’m only animating the top div, the bottom div is moving too because it needs to keep that margin value consistent.

    The reason why your second piece of code allows the divs to move at the same speed is because the top property is not a relative property of any element besides it’s container (the right container that is, probable that it’s not it’s direct container), so they will both move at the same speed.

    Not sure if this is clear.

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

Sidebar

Related Questions

I want to animate two jquery OBJECTS at the same time (using the jquery
While setting up CruiseControl, I added a buildpublisher block to the publisher tasks: <buildpublisher>
I am trying to swap two LinearLayouts by setting their visibility properties to VISIBLE
I need to play an MP3 in the background while generating animation. The audio
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
While going through university and from following the development of SO, I've heard a
While I've seen rare cases where private inheritance was needed, I've never encountered a
While working in a Java app, I recently needed to assemble a comma-delimited list
While in the final throws of upgrading MS-SQL Server 2005 Express Edition to MS-SQL
While Ctrl X works fine in vim under windows, Ctrl A selects all (duh).

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.