Is there a way to deactivate JavaScript upon leaving a div with the mouse?

Is it possible to disable JavaScript when moving the mouse out of a div?

I have implemented code using onmousemove to adjust the width of the left div ID. However, I noticed that JavaScript continues to work even when I move the mouse out of the container ID. How can I prevent JavaScript from executing when I move the mouse out of the container ID?

I attempted the following:

    container.on('mouseout', function (e) {
        isResizing = false;
    });

Unfortunately, this solution did not work. Is there another way to achieve this functionality?

Link to the JSFiddle

var isResizing = false;
$(function () {
   var container = $('#container'),
        left = $('#left'),
        handle = $('#handle');

    container.on('mousemove', function (e) {
        isResizing = true;
    });

    container.on('mouseout', function (e) {
        isResizing = false;
    });

    $(document).on('mousemove', function (e) {
        if (!isResizing) 
        return;
        left.css('width', e.clientX - container.offset().left);
        handle.css('margin-left', e.clientX - container.offset().left);
    });
});

Answer №1

let resizingActive = false;
$(function () {
   const container = $('#container'),
        left = $('#left'),
        handle = $('#handle');

    left.on('mousemove', function (e) {
        resizingActive = true;
    });

    left.on('mouseout', function (e) {
        resizingActive = false;
    });

    $(document).on('mousemove', function (e) {
        if (!resizingActive) 
        return;
        left.css('width', e.clientX - container.offset().left);
        handle.css('margin-left', e.clientX - container.offset().left);
    });
});

CHECK OUT THE DEMO ON FIDDLE

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

Exploring the intricacies of initializing a JavaScript function

I recently inherited a large JavaScript file from a previous developer, and I'm trying to decipher some of the key sections. Here is the complete code: $(function () { var homepage = (function () { // Main functionalities are defined he ...

Create dynamic transitions for hidden elements using a special technique

Is it possible to smoothly transition a div element from display:none to display:block? I attempted to first set the display to block and then apply a transition, but it doesn't seem to be working as expected. HTML <input type="text" class="inp"& ...

Modify the size of the text in an <iframe> using inline css

After attempting the command below, I noticed that the text size within the iframe remained unchanged. Is there a different method to adjust it? <iframe id="myiframe" src="**" height="230px" scrolling="no" style ...

What is the best method for encrypting a URL that contains AngularJS data?

Here is the URL that needs to be encrypted: <a class="btn btn-success btn-sm btn-block" href="@Url.Action("myAction", "myController")?Id={{repeat.Id}}&HistoryId={{repeat.HistoryId}}" ng-cloak>View History</a> I am seeking guidance on enc ...

Using Javascript to fetch JSON data from a PHP file hosted on an IIS server, incorporating JSONP with

I have set up an HTML5, JavaScript, and CSS3 https application on Windows IIS (Internet Information Services). The structure of the root directory is as follows: index.html, src/index.js, src/send_payment.php Currently, I am attempting to retrieve a basi ...

Determine whether an array is undefined in a React component

I am facing an issue in my React component where I have a code that renders the match name. However, sometimes the current match has an empty object and I don't know how to fix this problem. Here is the component code: import React from 'react&ap ...

Is it possible to access a service within the config function or access a provider from inside a service?

Need help configuring angular bootstrap tooltip (uibTooltip) to be disabled on mobile devices with angular-bowser for device detection. One possible solution is: isMobile = _bowser.mobile $uibTooltipProvider.options = { trigger: isMobile ? "none" : "mous ...

"Implementing a where clause in an AJAX request with a date range

Here is the HTML code snippet: <div class="col-md-4 input-group input-group-lg search"> <span class="input-group-addon"><i class="fa fa-fw fa fa-calendar red"></i></span> <input id="config" class="form-control ...

Any suggestions on MySQL auto-inserted apostrophes?

Having trouble updating a value in my Database, as it's resulting in an error. Backend: router.patch("/toggleState", (req, res) => { const todoId = req.body.todoId; const attribute = req.body.attribute; const newValue = req.body.newValue ...

Remove any JSON objects in JavaScript or AngularJS that match another JSON object

JSON A ['711','722','733','744'] JSON B [{pid: 711, name: 'hello'},{pid: 733, name: 'world'}, {pid: 713, name: 'hello'},{pid: 744, name: 'hellosdaf'}] I am attempting to remo ...

identifying which specific button was clicked using JavaScript/jQuery

All of the buttons on my page are identical - same title, same value, everything is the same. Is there a way for me to identify which specific button was clicked? ...

I am encountering difficulty loading a .gltf file for use with Three.js on my React website

I uploaded my .gltf file of a 3D model to the public folder of my project and then ran the command "npx gltfjsx filename" to convert it into a .js React component. Within that .js file, I included the following line: const { nodes, materials } = useGLTF(&a ...

Utilizing selected Bootstrap dropdown option for extracting value

I'm currently learning about development and am trying to create a bootstrap dropdown with 3 different background image options. My goal is to change the background based on the selected option from the dropdown. However, I am struggling to figure out ...

What is the significance of "new" being omitted in the upcoming jQuery script?

After diving into the JQuery documentation, I discovered that it is possible to create the event object like this: //Instantiate a new jQuery.Event object without using the "new" keyword. var e = jQuery.Event( "click" ); I find it puzzling why we don&apo ...

Showing a loading overlay during an AJAX call on a JSP page

Hello there! I'm interested in adding a special effect to my JSP page's center when making an ajax call to load a dropdown menu. Can anyone help me with this? Thanks! ...

What is the process of retrieving a property value from a database using selected values from cascaded drop-down lists?

I am facing a challenge where I need to extract a property of an entity by passing the IDs of selected items from a cascaded dropdown list. The requirement is to update the price every time there is a change in either level 1 or level 2 of the cascading dr ...

Display the scrollbar in a Boostrap CSS table when the content exceeds the height of the div

I've been working on a Bootstrap page that looks like this: https://i.sstatic.net/kpHMM.png On the right side, there are two tables inside a div. The issue I'm facing is that when the tables contain too many elements, they grow in height instea ...

Testing an async function with Jest - Jest failed to exit within one second of completing the test

Looking to validate the functionality of my Next.js API functions using Jest along with node-mocks-http. The specific function I aim to test is as follows: export default async ( req: NextApiRequest, res: NextApiResponse ): Promise<void> => { ...

Tips for stopping .php page refreshing following an .ajax request

On my mathProblems.php page, I have set up a system to generate math problems for users. When the user submits their answers using a form with id='math_answers', an ajax call is made to grade the math questions and display the results. The gradi ...

Having trouble accessing a component property in Angular 2

Hello, I am currently diving into Angular 2 and working on a project that involves basic CRUD functionality. I am encountering an issue with my User component where I am trying to access its property in the ngAfterViewInit hook, but it keeps showing as und ...