Is it possible in JQuery for the mouseup event to not be triggered if the left click button is held down for 10 seconds and then released suddenly, causing the mouse to move too quickly?

How to Move a DIV Inside Another Parent DIV Using Mouse Events in jQuery

$(document).ready(function () {
            var drag = null; var Top = null; var Left = null;
            
            var O = $("#Outside").offset();
            var outsideTop = O.top;
            var outsideRight = O.left + $("#Outside").width();
            var outsideBottom = O.top + $("#Outside").height();
            var outsideLeft = O.left; var move;
            $('#Box').mousedown(function (e) {
                $("#Box").css({ "background-color": "violet" });
                drag = $(e.target);
                drag.css({ "cursor": "move" });
                var box = $("#Box").offset();
                Top = e.pageY - box.top;
                Left = e.pageX - box.left;
               
            });
            $('body').on('mouseup', function (e) {
                $("#Box").css({ "background-color": "skyblue", "cursor": "default" });
                $('body').unbind('mousemove');
                drag = null;
            });
            $(document).on('mousemove', function (e) {
                if (drag) {
                    drag.css({ "background-color": "greenyellow" });
                    var cursorTop = e.pageY - Top;
                    var cursorLeft = e.pageX - Left;
                    var cursorRight = (e.pageX - Left) + $("#Box").width();
                    var cursorBottom = (e.pageY - Top) + $("#Box").height();
                    if (((cursorTop >= outsideTop) && (cursorTop <= outsideBottom)) && ((cursorLeft >= outsideLeft) && (cursorLeft <= outsideRight)))
                        if ((((cursorRight) >= outsideLeft) && ((cursorRight) <= outsideRight)) && ((cursorBottom >= outsideTop) && ((cursorBottom) <= outsideBottom))) {
                            drag.offset({
                                top: e.pageY - Top,
                                left: e.pageX - Left
                            });
                        }
                }
            }).on('mouseup', function () {
                $('body').unbind('mousemove');
               drag=null;
            });
           
        });
#Outside
{
  height:500px;
  width:1000px;
  position:absolute;
  background-color:navajowhite;
  border:5px solid blue;
  margin-left:120px;
  margin-top:18px;


}
#Box
{
  height:100px;
  width:100px;
  /*position:absolute;*/
  background-color:lightskyblue;
  border:4px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>

<div id="Outside">
  <div id="Box"></div>
</div>

While using Chrome browser to test the result, I encountered an issue where the mouseup event is not triggered if the mousemove event is too fast. I am seeking a solution to this problem.

Answer №1

One reason for the conflict in your code is the use of both $(document).on('mouseup'.. and $('body').on('mouseup'.... To resolve this, I have replaced 'body' with 'document' for the mouseup event in your code.

Please consider the revised code snippet below.

$(document).ready(function () {
            var drag = null; var Top = null; var Left = null;
            
            var O = $("#Outside").offset();
            var outsideTop = O.top;
            var outsideRight = O.left + $("#Outside").width();
            var outsideBottom = O.top + $("#Outside").height();
            var outsideLeft = O.left; var move;
            $('#Box').mousedown(function (e) {
                $("#Box").css({ "background-color": "violet" });
                drag = $(e.target);
                drag.css({ "cursor": "move" });
                var box = $("#Box").offset();
                Top = e.pageY - box.top;
                Left = e.pageX - box.left;
               
            });
            $(document).on('mouseup', function (e) {
                $("#Box").css({ "background-color": "skyblue", "cursor": "default" });
                $(document).off('mousemove');
                drag = null;
                console.log("ok");
            });
            $(document).on('mousemove', function (e) {
                if (drag) {
                    drag.css({ "background-color": "greenyellow" });
                    var cursorTop = e.pageY - Top;
                    var cursorLeft = e.pageX - Left;
                    var cursorRight = (e.pageX - Left) + $("#Box").width();
                    var cursorBottom = (e.pageY - Top) + $("#Box").height();
                    if (((cursorTop >= outsideTop) && (cursorTop <= outsideBottom)) && ((cursorLeft >= outsideLeft) && (cursorLeft <= outsideRight)))
                        if ((((cursorRight) >= outsideLeft) && ((cursorRight) <= outsideRight)) && ((cursorBottom >= outsideTop) && ((cursorBottom) <= outsideBottom))) {
                            drag.offset({
                                top: e.pageY - Top,
                                left: e.pageX - Left
                            });
                        }
                }
            })
           
        });
#Outside
{
  height:500px;
  width:1000px;
  position:absolute;
  background-color:navajowhite;
  border:5px solid blue;
  margin-left:120px;
  margin-top:18px;


}
#Box
{
  height:100px;
  width:100px;
  /*position:absolute;*/
  background-color:lightskyblue;
  border:4px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="Outside">
  <div id="Box"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When switching from JavaScript to jQuery, the button values become invisible

Currently, I have a functional app that can dynamically change the values of buttons based on user input. The current implementation is in vanilla JavaScript within the script.js file. However, I am looking to enhance the functionality and user experience ...

How to vertically align Bootstrap5 buttons on mobile devices

I'm currently developing a web application and I'm facing an issue with centering two bootstrap buttons next to each other on mobile devices. While it works fine on the computer, when I inspect the page on a mobile device, the buttons stack verti ...

Encountered issue with Ajax post request without any additional details provided

I could really use some assistance with this issue. It's strange, as Chrome doesn't seem to have the same problem - only Firefox does. Whenever I submit the form, Ajax creates tasks without any issues. My MySQL queries are running smoothly and I& ...

Angular2: Dynamically switch between selected checkboxes in a list

Is there a way to toggle a checked checkbox list in Angular2? I am trying to create a button that, when pressed while the full list is visible, will display only the checked items. Pressing the button again would show the entire list. Here's the Plu ...

WordPress footer widgets appearing misaligned

I have made a child theme for Twenty Thirteen and am in the process of making some updates to it. My development site is essentially a replica of the live site. The only modification I made in the footer section was to change the widget title styles, but I ...

Update and swap out outdated material

Struggling with replacing old content in a div after using AJAX. The issue is that both the new and old content are appearing inside the div. Here is my current Ajax code: $('#vu').on("click", function(e){ $.ajax({ url: 'url to pag ...

Eliminate the bottom padding on the body in your HTML and CSS code

My Unique Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <style> /*RESET BLOCK START ------------------------------------------------------------------------------------------------------------------*/ ...

Creating dynamic email templates in Node.js

I am working on a project using the MEAN stack and I need to implement email functionality. I have created separate email templates, but I want to include a common header and footer in all of them. Route.js router .route('/api/user/register&a ...

Rearrange the middle column to be the top column on smaller screens

I am trying to create a layout with 3 columns in a row. The challenge is to prioritize the middle column on top when viewed on small screens, with the left and right columns below it. To clarify, on large screens the order should be 1 2 3, while on small s ...

Applying CSS to color the letters of a text, without needing to alter the HTML code

I'm just beginning to learn CSS and today I received a school assignment: Below is the HTML code provided: <head> <meta charset="UTF-8"> <title>ABC</title> <style> /* CSS section */ </style> ...

Determine if a specific date occurred at least one day ago using momentjs

Is there a way to determine if a specific date is at least one day (24 hours) in the past using momentjs? For example: const currentDate = moment() const isAtLeastOneDayAgo = currentDate.subtract(dateToVerify) > 1 // How can this be done? ...

Guide to changing the border color in a Material-UI TextField component styled with an outline design

To complete the task, utilize the Material UI outlined input field (TextField component from "@material-ui/core": "^4.12.2",) and apply a custom blue color style to it. Here is the outcome of my work: https://i.sstatic.net/lw1vC.png C ...

Arrange content within DIVs using Bootstrap-4

I'm having trouble figuring out how to align and organize my div content using Bootstrap. I'm a bit confused about how it all fits together align-items- * text-* align-self- * justify-items- * justify-self- * I'm attempting to structure my ...

A guide on implementing CSS and Styling in a React component

Struggling to style my React.js file that was converted from a standard web page. I have the original CSS but unsure how to use React for proper styling. Any assistance is welcomed! var NavBar = React.createClass({ render: function() { return ( ...

Selecting an option from the knockout dropdown menu

I implemented a language dropdown in layout.chtml using knockout js. <select id="Language" class="styled-select" data-bind="value: Language,options: locale, value: selectedLocale, optionsText: 'name'"></select> var viewModel = th ...

Discovering the initial word of a string using jQuery

I'm currently facing an issue with jQuery that I need help solving. Here's the scenario: I am creating tooltips and positioning them directly under a specific trigger using CSS. Everything is functioning correctly, but there is one problem: In ...

How can we prevent the HTML escaping of text children in React.createElement?

Let's examine the call to React.createElement: React.createElement('span', null, null, ['&gt;&lt;',]) When executing this code, React will escape the characters &gt; and &lt;. Is there a method to prevent these sp ...

Automating the indexing of scroll positions in JavaScript

My code is functioning properly, but I had to input each case manually. Now, I am working on optimizing it to make it adaptable for any situation. However, I am struggling to figure out the best approach. The main objective is to determine my position on ...

Exploring the integration of PostgreSQL into JavaScript

As a beginner in JavaScript, I am looking to create a web page that can store data in a database. Can anyone provide guidance on what resources or materials I should study to learn more about this process? ...

Steps to capture cell click events in a Kendo grid

My grid is displaying information about students. I want to show a popup when the user clicks on the "StudentId" or "SubjectId" cell in those columns. How can I get the cell click event and verify that it is the right column? @(Html.kendo().Grid<St ...