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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:00:05+00:00 2026-05-26T16:00:05+00:00

I wanted to write a javascript code that will slide a div in specific

  • 0

I wanted to write a javascript code that will slide a div in specific direction, distance and in some given time. I wrote this small script. but doesn’t work at all. Instead browser gets slow. No change in position is visible.
Can someone tell me how to achieve the result ? I know there are many ready made libraries that can do this easily. But I just wanted to give it a try.

<script type="text/javascript" language="javascript">
    var element = '';
    var slidePerMS = '';
    function slideIt(ele, direction, distance, slideDuration){
        element = ele;
        var i=0;
        slidePerMS = distance / (slideDuration*1000);
        for(i=0; i<3000; i++){
            setTimeout("changePosition()",1);
    }
}
function changePosition(){              
    var currElement = document.getElementById(element);
    currElement.style.left = "'"+slidePerMS+"px'";
}
</script>
  • 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-26T16:00:06+00:00Added an answer on May 26, 2026 at 4:00 pm

    SOO many things wrong with that code it’s not even funny… Let’s see…

    1. You are trying to render a 1,000 FPS animation. This is simply impossible for a browser.
    2. You are passing a string as parameter to setTimeout, which is as evil as eval.
    3. You set slidePerMS once but never change it after, resulting in the div being moved to the exact same spot over and over.
    4. You are setting the style with extra quotes inside – do you put quotes in a CSS file?

    That’s to name but a few. Try this instead:

    <script type="text/javascript" language="javascript"> 
    function slideIt(elem, direction, distance, slideDuration){ 
        var elmt = document.getElementById(elem),
            i=0, step = distance / (slideDuration*20),
            stepper = setInterval(function() {
                i = Math.min(distance,i+step);
                elmt.style.left = i+'px';
                if( i == distance) clearInterval(stepper);
            },50);
    }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote some code, looked it over, and wanted to cry. Lines that
I wanted to write a function that would take an object and convert it
Just wanted to write some recursion but can't check if the child is in
I wanted to write a method with an argument that defaults to a member
I wanted to write a Custom Control that would show a family tree... so
I wanted to write a program that test if two files are duplicates (have
Say I wanted to write a program that would export calendar data so that
I am working on implementing a JavaScript web bug that will be inserted into
Sorry for the semi-rant here. I am hooked on javascript and want some direction
I was fed up with the limited javascript Array functions and wanted to write

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.