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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:17:24+00:00 2026-05-21T17:17:24+00:00

If I have a div that is a 5×5 square or something. I want

  • 0

If I have a div that is a 5×5 square or something. I want to be able to click on it, then while holding down the mouse, move it in a direction, and upon release of the mouse, have the div fly towards the direction i “flicked”. How can I accomplish this with jquery or javascript? Not sure if an algorithm or logic is behind this.

  • 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-21T17:17:25+00:00Added an answer on May 21, 2026 at 5:17 pm

    From a conceptual perspective (I’ll be beaten with actual code no doubt shortly) I would register the mouse coordinates on MouseDown, and compare them against the mouse coordinates on MouseUp to determine the angle at which to move the div (this would allow the DIV to continue moving in the correct direction, even when the MouseUp was close to the DIV).

    The easier way would be to just move the square towards the MouseUp coordinates (i.e. mouse down coordinates don’t really matter in a small DIV), but this doesn’t work as well if the MouseUp is very close to the MouseDown.

    Either way, use something like this answer (How to make a div or object gradually move to point of mouseclick with javascript?), except on MouseUp/MouseRelease instead of click, ideally towards a projected point (along the line between MouseDown and MouseUp at a specified distance).

    Edit

    I’ve included a Prototype example below (it’s very rushed and could use plenty of optimisations + clearer concept on the differences between Page/Graph y-axis, as well as some better handling of steep slopes, as well as calculating distance to fling based on distance between mousedown/mouseup as fauxtrot mentions in comments, as well as perhaps disabling fling after the first fling as you can keep “flinging it around” at the moment, as well as “out of bounds” checks and perhaps reverse bouncing off the edges).

    Running Example: http://jsfiddle.net/9B9sA/

    HTML

    <div id="bluebox" 
      style="width:100px;
      height:100px;
      background-color:blue;
      position:absolute;
      left:300px;
      top:300px;"> </div>
    

    jQuery (including jQuery UI for animate)

        var startDownX, startDownY; 
    
        $(document).ready(function() {
    
        /* Stop default Firefox etc. drag */
        $(document).bind("dragstart", function() {
             return false;
        });
    
        /* Capture start of flings */
        $('#bluebox').mousedown(function (event) {
            startDownX = event.pageX;
            startDownY = event.pageY;
        });
    
        /* Work out fling direction on end of fling */
        $(document).mouseup(function(event){
            /* Page y-axis is different from graph */
            var rise = -(event.pageY - startDownY); 
            var run = event.pageX - startDownX;
            var newX = $('#bluebox').position().left;
            var newY = $('#bluebox').position().top;
            var distanceToFling = 100;
    
            if (run == 0 || Math.abs(rise/run) > 3) {
                if (rise > 0) {
                  newY -= distanceToFling;
                } else if (rise < 0) {
                  newY += distanceToFling;
                }
            }
            else {
                if (run > 0) {
                    newX += distanceToFling;
                    newY -= (rise/run) * distanceToFling;
                }
                else {
                    newX -= distanceToFling;
                    newY += (rise/run) * distanceToFling;
                }
            }
    
             $('#bluebox').animate({
                 left: newX,
                 top: newY
                }, 1000);
        }); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DIV that I want to touch the bottom of the screen
I have a div that I want to toggle between displaying or not when
I have a div that contains a student's schedule, and there is a drop-down
I have a div that looks like this <div class=listItem jrFeatured> I want it
I have div#myid that is resizeable with minHeight = 30px; I want to do
I have a div that i want to apply two backgrounds to it. Basically
I have a div that is scrolling vertically. I, however, really want it to
I have a div that contains links(dynamic) and want when users clicked on a
I have a div that I want to appear directly below a table cell
I have a div that contains a form with two drop down lists in

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.