Determine with jQuery whether the cursor is within a specified region

My webpage has a hidden area that is dynamically set to occupy 60% of the window's width and 70% of its height, centered on the page. This means that the area starts at 20% of the window's width and 15% of its height away from the top-left corner.

I am trying to make different interactions happen based on whether the cursor is inside or outside this area.

document.onmousemove = function(e){
    var cursorX = e.pageX;
    var cursorY = e.pageY;
    var windowW = $(window).width();
    var windowH = $(window).height();

    if (0.2 * windowW <= cursorX <= 0.8 * windowW && 0.15 * windowH <= cursorY <= 0.85 * windowH) {
      console.log("the cursor is inside the bound, do something.");
    } else {
      console.log("the cursor is outside the bound, do something else.");
    }
  }

Currently, my mouse movements are only registering as being inside the boundary. I suspect there may be an issue with the if statement. How can I correct this?

Edits: Please keep in mind that I also have a drawing canvas at the bottom of the page which prevents me from using mouseenter() and mouseleave() on a div overlay. Are there any alternative solutions?

Answer №1

Which element resides in the hidden region?

To uncover it, place a transparent div at that spot and use the mouseenter event to identify when the cursor first enters the invisible area.

For more information, refer to: https://api.jquery.com/mouseenter/

Answer №2

After reevaluating my previous code, I discovered a logical error. Modifying the if statement in a different way successfully resolved the issue.

if (cursorX >= 0.25 * windowW || cursorX <= 0.75 * windowW || cursorY >= 0.2 * windowH || cursorY <= 0.8 * windowH) {
  console.log("inside the boundary!")
} else {
  console.log("outside of the boundary!");
}

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

Navigating Links Inside a jQuery Mobile Panel

I've been searching high and low for the solution to this issue, but so far, I haven't had any luck. My dilemma lies within my jQuery mobile panel as I attempt to navigate to a specific section of the page: <div data-role="page" id="home"> ...

Is it possible to use JavaScript to extract data associated with a specific h2 class on a webpage and then present that information?

Just curious if it's feasible, not looking for actual code. For instance, say I have an h2 class called "stock-number" with a value of #NC123456. Is it possible for JavaScript to access each instance of that class, retrieve data from a different datab ...

Automatically submit HTML form data as a JSON object to an API endpoint

I am working on incorporating an online form from Formstack onto a website, and the form's code is in HTML. However, I am seeking assistance in configuring it so that whenever a user submits information through the form, it will automatically send me ...

Looking to extract and upload a thumbnail image from an HTML5 video element

I am trying to extract and upload a thumbnail from an HTML5 video tag, Below is my code snippet: var w = 135;//video.videoWidth * scaleFactor; var h = 101;//video.videoHeight * scaleFactor; var canvas = document.createElement('canvas&apo ...

Adjust the size of a div element with JavaScript

Currently, I am facing a challenge with resizing a div with the id="test". My goal is to adjust the size of the div based on the height of the browser window subtracting 80px so that the element fits perfectly within the visible space without req ...

Overlapping containers

Recently, I attempted to implement a Bootstrap 4.1 theme into my yii2 project. However, I encountered an issue where the containers were overlapping by about half of their width. Surprisingly, the classes I used are directly from the bootstrap framework. H ...

Testing a React component using Jest and Enzyme, incorporating async/await functionality within a try/catch block

I've been struggling with testing this method using Jest for a while now, and I haven't found any helpful solutions online. I believe it may be time to refactor the method, but I'm unsure how to do so in a way that is easily testable. class ...

Hide Bootstrap columns for Printing

I am currently working on a webpage layout that consists of two columns. <div class="container"> <div class="row"> <div class="col-md-8 "></div> <div class="d-print-none col-md-4"></div> </div ...

What are the necessary headers that must accompany a post request?

While testing the server with Postman, everything seems to be working fine as I receive a response: https://i.stack.imgur.com/yMRfj.png However, when attempting to make a POST request from the browser using the same address, it results in an error and th ...

extract the value from the chosen option in a web element

I'm struggling to retrieve the selected option value in my web component so that users can easily access it. It seems like the issue lies within the shadow root. __loadOptions() { const SELECT = this.shadowRoot.querySelector('select'); ...

Nested functions are only effective for a single use

I am facing an issue on my website where I have implemented a button that creates another button when clicked. However, the newly created button is supposed to generate a paragraph upon clicking, but this functionality only works once. I suspect that nesti ...

What is the best way to implement a hover feature on a Vue / Vuetify Autocomplete dropdown list?

I'm not very experienced with Vue and I'm finding it a bit complex to grasp. Perhaps you guys can provide the "best practice" or most efficient solution for my issue: I have an autocomplete dropdown box. When expanded, it shows a list with click ...

what is the best method to schedule tasks at a specific time in node-schedule?

I am facing an issue with running a node task daily at 8AM using the node-schedule package https://www.npmjs.com/package/node-schedule. Instead of running every day at 8AM, it is currently running every minute. What method should I use to correctly configu ...

Error when using a third-party library in a jQuery plugin on an Android device

Currently, I am working on an application that utilizes a jQuery UI plugin, which then relies on the Raphael library. Everything runs smoothly on iOS and standard browsers, but when it comes to Android, I encounter the following error: ReferenceError: can ...

Using Javascript to Manage Scroll Stop Events

I am currently facing a challenge while working on an IOS (Cordova) application. I am developing it using core Javascript without relying on any framework like Ionic or Onsen UI. The issue I am encountering is that I need to trigger an event when the scrol ...

Deleting a row from a table when a similar element is found in another location

My webpage features a table list that showcases users linked to an organization: <script type="text/html" id="userListTemplate"> <div id="user_list_box"> <table class="list" style="margin-bottom: 15px;"> {{#Users} ...

Implementing image-based autocomplete in a search bar within an MVC framework

Seeking assistance to implement a unique feature for my MVC application. I aim to create a search box that suggests images based on user input rather than text. The functionality involves matching the user's input with the "Title" property in an Ent ...

Refreshing Symfony2 values dynamically with ajax without the need for page reload

Let me start by saying that I am not a front-end expert, and this is my first attempt at working with ajax. My goal here is to update and display the new value without having to refresh the entire page. I only want to refresh the input field and its values ...

Label stops functioning properly when there is a line-height exceeding 1

The problem arises when the line-height of a paragraph is set to more than 1em and the label tag doesn't work properly between lines. <p style="line-height: 2em;"><input type="checkbox" id="xx" /> <label for="xx">Lorem ipsum dolor ...

Issue with Browser Geolocation API not throwing errors or properly handling timeouts

For quite some time, I've been mystified by the black magic of geolocation. I'm reaching out to see if any of you can shed some light on this for me. Here's the geolocation function in JS that I've been working with: // User Position ex ...