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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:12:08+00:00 2026-06-09T05:12:08+00:00

Hello So I started to write some function witch will do this(closer you move

  • 0

Hello

So I started to write some function witch will do this(closer you move mouse to div the closer div moves to mouse position on parent X axsis, maximum div position is left:-40% and minimum is left: -80%):

Note: The black arrow is cursor(mouse position)

  1. List item
  2. List item
  3. List item

Code

HTML markup:

<div class="wraper">
    <div class="ls">
        <div class="ldiv">
        </div>
    </div>
    <div class="c">
        <div class="coordinates">
        </div>
        <div class="cdiv">
        </div>
    </div>
    <div class="rs">
        <div class="rdiv">
        </div>
    </div>
</div>

CSS markup:

body{
    margin: 0;
    padding: 0;
}
.wraper{
    width: 100%;
    height: 500px;
}
.ls, .rs{
    position: relative;
    float: left;
    width: 30%;
    height: 100%;
    background-color: #eeeeee;
}
.c{
    position: relative;
    float: left;
    width: 40%;
    height: 100%;
    background-color: #dddddd;
}
.cdiv{
    position: absolute;
    top: 25%;
    left: 10%;
    width: 80%;
    height: 50%;
    background-color: red;
}

.ldiv{
    position: absolute;
    top: 30%;
    left: -80%;
    width: 80%;
    height: 40%;
    background-color: red;
}
.rdiv{
    position: absolute;
    top: 30%;
    right: -40%;
    width: 80%;
    height: 40%;
    background-color: red;
}

Javacsript markup:

//ASsign global variables
var mouseX = 0;
var newTop = 0;

$("div.ls").mousemove(function(event) {
    // Get parrent offset
    var parentOffset = $(this).offset();
    // Get child division offset
    var division = $(".ldiv").offset();
    // Calculate mouse position inside left division
    var relX = event.pageX - parentOffset.left;
    var relY = event.pageY - parentOffset.top;
    // Check if mouse changed its position
    if (mouseX != relX){
        // Get new position of x axis
        var newPosX = $(this).width() - relX - 161;
        // Get new height of child element in percentage
        var newHeight = 100 * parseFloat($(".ldiv").height()) / parseFloat($(this).height());
        // Display important stuff
        $(".coordinates").text("Mouse position = X:" + relX + " Y:" + relY + "Div offset = X:" + division.left + " Y:" + division.top  + " Width = " + $(this).width()+" newHJeight = "+newHeight);
        //If mouse moves left
        if (mouseX > relX) {
            // Cant go lower then 0.2 because javascript is rounding it down
            newHeight += 0.2;
            // calculate new top so division stays in middle of parent
            newTop = (100 - newHeight) / 2;
            // Assign new css
            $(".ldiv").css({
                left: newPosX + "px",
                height: newHeight + "%",
                top: newTop + "%"
            });
        } 
        //If mouse moves right
        else {
            newHeight -= 0.2;
            newTop = (100 - newHeight) / 2;
            $(".ldiv").css({
                left: newPosX + "px",
                height: newHeight + "%",
                top: newTop + "%"
            });
        }
        // Record mouse position
        mouseX = relX;
    }
});

​

Here is live example in jsFiddle

Things I want to get done:

  1. How can I rewrite this code so it would work more like animation, and wouldn’t go out of order if I would move mouse to fast?

​

  • 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-09T05:12:09+00:00Added an answer on June 9, 2026 at 5:12 am

    I would’n rely on mousemove, I would Use requestAnimationFrame [or a simple setTimeout] instead.
    that way you would have a way lesser load on your cpu and a smoother animation.

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

Sidebar

Related Questions

Hello Guys! I started learning python GUI development. Let's say I have this script:
Hello I'm a newcomerin JavaScript language. I started to see some examples of JavaScript
I have the most simple .on function i could write. it started off as
I started out with hello world application from google documentation to write a chrome
I just started learning C, and wrote my hello world program: #include <stdio.h> main()
I just started learning C, and wrote my hello world program: #include <stdio.h> main()
Hello suddenly VS2010 has started formatting my if statements in a way that I
Hello I'm very new to OpenGlES and i have started to try to learn
Hello StackOverflow community! I started to learn Node.js recently, and decided to implement a
Hello! I've been programming for a long time but just started developing for android,

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.