Tips for preventing the mouseover event from the parent element when the mouse is leaving both the parent and child elements, especially when the parent has a border

Update: My goal is to prevent the nested div from moving when the mouse exits both it and the parent div. It seems like it is currently shifting because the border somehow extends the parent div further than the nested div. I want to maintain the border.

As the saying goes, a demo speaks volumes.

I have a situation where a div is nested within another div

<div class='parent'>
    <div>See me</div>
</div>

This setup includes some styling

.parent {
    width: 100%;
    border: 1px solid black;
}
.parent div {
    display: inline-block;
    position: relative
}

There's also some accompanying JavaScript code

var navBar = document.querySelector('div.parent');
var navItems = navBar.querySelector('div');
var moveNav = false;
var overItems = false;

navBar.addEventListener('mouseout', function() { moveNav = false; });
navItems.addEventListener('mouseover', function() { overItems = true; });
navItems.addEventListener('mouseout', function() { overItems = false; });
navBar.addEventListener('mouseover', function() { moveNav = !overItems && true; });
navBar.addEventListener('mousemove', moveToMouse);
function moveToMouse(e) {
    if(!moveNav)
        return;
    navItems.style.left = (e.offsetX - Math.floor((e.offsetX+navItems.offsetWidth)/navBar.offsetWidth) * (e.offsetX + navItems.offsetWidth - navBar.offsetWidth + 10)) + 'px'
}

The objective is to ensure that a portion of the child div stays under the mouse cursor while it is inside the .parent div.*

I'm interested in learning how to prevent the child div from moving once the mouse leaves the .parent div?

In simpler terms, I want it to behave like it does in this fiddle. The main distinction between the two examples is that one has a border around .parent, while the other doesn't.

Additionally, I've observed that the child div jumps instead of moving smoothly. Any suggestions on how to address this issue are appreciated but not mandatory.

*please share any alternative methods to achieve this as well**

**avoid suggesting "use jQuery"

Answer №1

When working with the div element that has a border, I chose to utilize the mouseenter event instead of mouseover and it appears to function according to your requirements.

navBar.addEventListener('mouseenter', moveToMouse);

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

Firing a custom jQuery event when a page loaded via AJAX is complete, ensuring it is triggered

I am facing an issue with a particular scenario. There is a page that contains a script to load a child page and log a custom event, which is triggered in a Subform. --Index.html-- <body> <input type="button" class="clickable" value="Load child ...

Tips on getting the Jquery .load() function to trigger just once and executing an Ajax request only once

Upon loading the page, I am utilizing the JQuery .load() function to retrieve content from a PHP file. The content loads successfully but it keeps reloading continuously as observed through Chrome Developer tools. I only want the content to load once. var ...

Istanbul provides me with a thorough analysis, yet it always seems to conclude with an error

Currently, I am experimenting with a basic application (found in the Mocha tutorial code available at ) to troubleshoot why Istanbul is giving me trouble. The issue is that Istanbul successfully generates a coverage summary but then throws an error for unk ...

Issue encountered: The CSS provided is not valid as it is expecting a selector or at-rule after "body", but instead found "{". This error is located on line 4 of the code

As I was working on my code in style.scss, I encountered an error message from my Sass compiler (Koala) while trying to include the following code: body {margin-top:105px;} Here's the specific error snippet from style.scss: Error: Invalid CSS after ...

Arrange an array that is separated into pages

I am facing an issue with sorting a paginated array by columns. When I try to sort from the current page, I end up getting either the beginning or the end of the sorted array. However, if I change the page and then sort, I get the correct sorted values. It ...

Learn how to iterate over a JSON object using TypeScript in Angular5 to generate an array of objects

Here is a sample JSON code that includes an array "Customers" with objects and arrays nested inside: This is my JSON code snippet: { "Customers": [ { "customerData": { "secondLastName": "Apale", "firstLastName": "Lara", ...

Ways to retrieve data from an AJAX success callback function

Within my front end JavaScript application, I need to execute an ajax request in order to retrieve data from the server. Once this data is acquired, I aim to display it within the view. var retrievedData; $.ajax({ url:"/getDataFromServer.json", ty ...

An easy guide to using validators to update the border color of form control names in Angular

I'm working on a form control and attempting to change the color when the field is invalid. I've experimented with various methods, but haven't had success so far. Here's what I've tried: <input formControlName="pe ...

Javascript: decodeURIComponent seems to be malfunctioning

I have encountered a situation where a guy helps me decode a string to make it readable. decodeURIComponent("Invalid Ticker Name \u0027t\u0027 at line: 3") "Invalid Ticker Name 't' at line: 3" Upon further inspection, it appears that ...

Storing a collection of objects in session storage

I've been struggling to save an array containing the items in my online shopping cart. Even though both the object and the array are being filled correctly, when I check the sessionStorage, it shows an array with an empty object. I've spent a lot ...

What is the appropriate alt attribute to use for images in a slideshow?

Within a website directory, each business showcases a slideshow featuring multiple images. When faced with uncertainty regarding the content of these pictures, how should one approach assigning the alt attribute to each image/link? For instance (modified) ...

Stopping the spread of popup alerts: A comprehensive guide

I'm having trouble explaining this in English;-) Whenever I select an option, an alert pops up for each choice and I don't know how to stop it. Step 1: Choose the "aaaa" option, for example 1111 alert ... Step 2: Choose the "bbbb" option, for ...

What causes html elements to lose their border-radius when they are being dragged?

Why did the element lose its border-radius when I dragged them? It seems like the element is cutting into the background to fill the four corners. Click here to see CSS class for item: .list_item_pic { display: flex; flex-direction: column; justify- ...

tips for understanding the contents of the upcoming css class

I am working with a CSS class that looks like this: #menuAccordion h2 a,.dir-rtl #menuAccordion .viewList{ padding-right:2.5%; padding-left:0 } Is it correct to assume that for any HTML elements using the menuAccordion class, if there is an & ...

I'm having trouble understanding the distinction between this.query and this.query.find(). Can you explain the difference to me?

Currently, I am working on a MERN tutorial where I am developing a full E-commerce application. This is the class that handles querying and various other tasks. constructor(query, querystr) { this.query = query; this.querystr = querystr; con ...

Tips for displaying tab content with CSS Grid areas

I have successfully implemented a tabbed interface using CSS Grid areas. However, I am encountering an issue where the tab content is not displaying and the console shows a value of null. Below is the code snippet that I am currently working with: funct ...

Creating responsive lines with CSS on top of an image

I would like to add lines on an image using CSS3 and HTML5 Canvas. I came across a tutorial and demo that utilizes an html div: However, when attempting this effect on an image, the line appears outside of the image. How can I ensure that the line is dra ...

placing one SWF file above another

Is it feasible to layer multiple SWF files on top of one another directly? I have been experimenting with divs and different z-index values, but I am unsure about how the Flash player comes into play. Is my assumption correct that each SWF has its own indi ...

Keep your filter content up-to-date with real-time updates in Vue.js

I am facing an issue with a markdown filter where the content of component.doc is set to update through a websocket. Despite updating the scope's component, the filtered content remains unchanged. Is there a way to dynamically refresh the v-html in t ...

If the URL matches a specific path, then append a parameter

I've created a script that adds a parameter to a URL based on specific subfolders. For example, if the URL contains /de, it will add ?_sft_language=german. However, I'm encountering an issue where the code is running multiple times instead of jus ...