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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:38:36+00:00 2026-06-03T01:38:36+00:00

I wrote a program to animate a div content. This is working according to

  • 0

I wrote a program to animate a div content. This is working according to the mouse position. On mousemove event, if mouse position is more than 1/3 of window height content will animate to up (content will appears). Otherwise it will animate to downside (disappears). The program is giving proper result but it is taking time. I think the animate function is calling multiple times.

How can I improve the code?

Please check the code:

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">            
            var ww;
            var wh;
            var moverHeight="150";
            var moveRespondPosition;

            $(document).ready(function()
            {
                ww=$(window).width();
                wh=$(window).height();

                $("#mover")
                .css("height",moverHeight)
                .css("bottom",-moverHeight);

                moveRespondPosition=wh-(wh/3);

                $(document).mousemove(function(e)
                {
                    var mousePosition=e.pageY;
                    if(mousePosition>moveRespondPosition)
                    {
                        setTimeout('animateElem(0)',100);

                    }
                    else
                    {                        
                        setTimeout('animateElem(-moverHeight)',100);
                    }
                }); 
            });

            function animateElem(value)
            {
                $("#mover").animate
                ({
                    "bottom":value
                });
            }

        </script>

        <style type="text/css">
            *
            {
                padding: 0px;
                margin: 0px;
            }
            body
            {
                overflow-y: hidden;
            }
            #wraper
            {
                width: 100%;
                height: 100%;
                position: relative;
            }

            #mover
            {
                width: 100%;                
                background-color: #192B44;
                position: absolute;                
            }
        </style>
    </head>
    <body>
        <div id="wraper">
            <div id="mover">
                Content
            </div>
        </div>
    </body>
</html>
  • 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-03T01:38:38+00:00Added an answer on June 3, 2026 at 1:38 am

    The problem is this part:

    $(document).mousemove(function(e)
    {
        var mousePosition=e.pageY;
        if(mousePosition>moveRespondPosition)
        {
            setTimeout('animateElem(0)',100);
        }
        else
        {                        
            setTimeout('animateElem(-moverHeight)',100);
        }
    });
    

    Basically, you are getting an event on each pixel move of the mouse and creating a new timer. So if the mouse is moved 100 pixels across the screen, you are creating 100 new timers, 1000 pixels and you get 1000 timers.

    I’m not sure you even need to use setTimeout at all here, if you want a little delay you can do that with the animate call. But you’ll still have a problem with kicking off a jQuery animation on every pixel move. You should add a check so that you are only doing the animation when it’s needed.

    Something like this might work better:

    $(document).mousemove(animateElem);
    
    function animateElem(e)
    {
        var mousePosition=e.pageY;
        var value = mousePosition>moveRespondPosition ? 0 : -moverHeight;
    
        if ($('#mover').css('bottom') !== value) {
            $('#mover').stop().animate({'bottom',value}, 100);
        }
    }
    

    I would also recommend looking at http://benalman.com/projects/jquery-throttle-debounce-plugin/ to debounce your events. There is no need to be doing the calculation on every single pixel movement, if you just check once every 25ms it would look the same to the user but be much more performant.

    If you include the debounce script from the link, you can use it like:

    $(document).mousemove($debounce(25, animateElem));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program to play MPEG video on a window (of course, DirectShow
I wrote a program to solve a complicated problem. This program is just a
I wrote this program in Qt Creator but I'm not sure how to run
I wrote this program in C and also in erlang To practice I tried
I wrote a program that uses OLE and it was working fine until I
I wrote this program with virtual inheritance and I have a few questions. #include<iostream>
I wrote a program that uses builtin function bin(), but this function is new
I wrote a program that requires ~1GB of RAM zeroed at startup, and this
i wrote program to connect oracle database 11g for my android application to store
I wrote a program that forks some processes with fork(). I want to kill

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.