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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:25:54+00:00 2026-05-28T21:25:54+00:00

On some condition I want to cancel onmousemove event when mouse goes down, for

  • 0

On some condition I want to cancel onmousemove event when mouse goes down, for example. Is it possible to determine the direction of onmousemove event? jQ or JS is Ok.

I have drag’n’drop elements. The user drags the elenment up. If, for example, the bottom of the element reaches some position in the document (i.e. 500px from the top of the document) the onmousemove stops. And if the user will try to drag the element up again the function will not start. Only drag down will be possible for this element. So I thought it would be quite easy to do it by catching the direction of the mousemove event. But it seems there’s no such kind of standard properties.

  • 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-28T21:25:55+00:00Added an answer on May 28, 2026 at 9:25 pm

    You can save the position of the last mousemove event to compare to the current position:

    //setup a variable to store our last position
    var last_position = {},
    $output       = $('#output');
    
    //note that `.on()` is new in jQuery 1.7 and is the same as `.bind()` in this case
    $(document).on('mousemove', function (event) {
    
        //check to make sure there is data to compare against
        if (typeof(last_position.x) != 'undefined') {
    
            //get the change from last position to this position
            var deltaX = last_position.x - event.clientX,
                deltaY = last_position.y - event.clientY;
    
            //check which direction had the highest amplitude and then figure out direction by checking if the value is greater or less than zero
            if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 0) {
                //left
            } else if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX < 0) {
                //right
            } else if (Math.abs(deltaY) > Math.abs(deltaX) && deltaY > 0) {
                //up
            } else if (Math.abs(deltaY) > Math.abs(deltaX) && deltaY < 0) {
                //down
            }
        }
    
        //set the new last position to the current for next time
        last_position = {
            x : event.clientX,
            y : event.clientY
        };
    });
    

    Here is a demo: http://jsfiddle.net/Dv29e/

    Update

    You can also throttle the mousemove event to get more of a general idea where the mouse has moved:

    var last_position = {},
        $output       = $('#output'),
        mousemove_ok  = true,
        mouse_timer   = setInterval(function () {
            mousemove_ok = true;
        }, 500);
    $(document).on('mousemove', function (event) {
        if (mousemove_ok) {
            mousemove_ok = false;
            ...
        }
    });
    

    This will only check the cursor’s position against it’s past position if:

    1. the last position exists.
    2. the mousemove_ok variable is set to true which is done every half second.

    Here is a throttled demo: http://jsfiddle.net/Dv29e/4/

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

Sidebar

Related Questions

I have a script that I want to exit early under some condition: if
I have a IQueryable which is ordered by some condition. Now I want to
I have 2 filters in my application. Based on some condition, I want to
I want to conditionally navigate to some page. If some condition is true i
my function: struct hostent * gethost(char * hostname){ if(/*some condition under which I want
I have some conditional formatting styles. I don't want to keep creating new rules
I have to display yes/no button on some condition on my asp.net page ,
I have some code that watches the event log for entry written events and
I want to Select * from my datatable under some condition and put the
I want to be able to write something as void Start(some condition that might

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.