Concealing and revealing an image with the onMouseOver and onMouseOut events - image quickly reemerges

After writing the following jQuery script, I encountered an issue.

<script type="text/javascript">
          $(document).ready(function(){
           $('#Oval-1').on('mouseover', function(e) {
                $("#Oval-1").fadeOut();
            });
                $('#Oval-1').on('mouseout', function(e) {
                         $("#Oval-1").fadeIn();
                 });
          });
        </script>

The ID Oval-1 corresponds to an SVG image on my webpage. The intention was for the image to fade out upon mouseover and fade back in upon mouseout. However, the current implementation is causing the image to blink once. What am I missing here?

Displayed below is the div containing the SVG:

<div class="wrapper">
        <svg width="1024px" height="279px" viewBox="0 0 1024 279" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 3.6.1 (26313) - http://www.bohemiancoding.com/sketch -->
<title>map png</title>
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="geschichte" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
    <g id="Desktop-Landscape" transform="translate(-145.000000, -332.000000)">
        <g id="map" transform="translate(107.000000, 246.000000)">
            <g id="mapback" transform="translate(38.000000, 86.000000)">
                <g id="map-png">

// Additional paths found within the SVG have been omitted

                                            <ellipse id="Oval-1" fill="#0B619B" opacity="0.141849347" cx="929.5" cy="94.5" rx="94.5" ry="94.5"></ellipse>

                </g>
            </g>
        </g>
    </g>
</g>

I came across this solution, but my concern lies specifically with jQuery rather than vanilla JavaScript.

Answer №1

One simple way to achieve this effect is by using CSS

#Oval-1 {
  opacity: 1;
  transition: opacity 400ms ease;
}

#Oval-1:hover {
  opacity: 0;
}

Answer №2

The current issue stems from the fact that the #Oval-1 element gets removed from the page, which in turn triggers the mouseout event.

A potential solution is to enclose the animated element and handle events on the parent container as shown below:

<div id="Oval-1">
    <div id="test">hello</div>
</div>

 $(document).ready(function(){
       $('#Oval-1').mouseover(function() {
            $("#test").fadeOut();
        });
        $('#Oval-1').mouseleave(function() {
            $("#test").fadeIn();
        });
  });

This approach seems to resolve the issue effectively.

Additionally, it's important to ensure that removing the child element does not cause the parent container to collapse, potentially triggering the mouseleave event as well.

To visually demonstrate the concept, consider implementing these CSS styles:

#test {
    position: absolute;
}

#Oval-1 {

    background: pink;
    width: 300px;
    height: 300px;
}

If you are using a fiddle or similar tool for testing, these styles will provide a clearer representation of the scenario.

Alternatively, applying the following CSS rules may also be effective:

#Oval-1 {
    transition: all 0.5s ease-in-out;
}
#Oval-1:hover {
    opacity: 0;
}

This alternative method could potentially address the issue as well.

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

Guide for building a Template-driven formArray in Angular

I am currently implementing a fixed number of checkboxes that are being bound using a for loop. <ul> <li *ngFor="let chk of checkboxes"> <input type="checkbox" [id]="chk.id" [value]="chk.value&q ...

jQuery's Multi-Category Filter feature allows users to filter content

I have been working on creating a filter function for my product list. The idea is that when one or more attributes are selected, it should fade out the elements that do not match. And then, if a filter is removed, those faded-out items should fade back in ...

I am currently familiarizing myself with backend routes and everything seems to be going smoothly for the first time around. However, upon attempting to revisit the site for

https://i.sstatic.net/ZOBl6.png https://i.sstatic.net/Xzhej.png Whenever I navigate from the home page to the contact page and then back to the home page, it displays an error message saying the site can't be reached. var http = require("http&q ...

Can someone recommend a streamlined JavaScript algorithm for identifying the unique arrays within a collection of arrays?

Consider the array below: let array = [[1, 2], [1, 2], [3, 4], [5, 6], [2, 1]] I am looking for a way to determine the number of unique arrays in this set. Using the array above as an example, the expected result is 3. How can I achieve this? The code sn ...

How to refine a schema by applying filters from another schema

I am working with 2 different Schemas const mongoose = require('mongoose'); const Schema = mongoose.Schema; const {ObjectId} = mongoose.Schema; const matchList = new Schema({ user:[{ type: ObjectId, ref:'User' } ...

The image from the local source displays correctly in the initial component without any issues, but it does not appear in its corresponding detail component

My blog has a component for displaying posts and another component for showing the details of a single post as seen below. A key point to note is that while the blog component can load and display images, the blog details component cannot. Why is this? h ...

Angular JS: How to dynamically add and remove checkboxes in ng-repeat?

Currently, I have successfully implemented a Miller column using Angular and Bootstrap. To view the functionality in action, you can check out the code snippet at this link. In the second column of my setup, clicking on a word opens up the third column. ...

Dynamic line breaks within an unordered list

I have a requirement where I need to display the last two li elements from an ul list on a new line if the screen width is less than 625px. The code snippet below achieves this functionality: ... ... ... ... However, this code fails valida ...

What purpose does the div tagged with the class selection_bubble_root serve in Nextjs react?

Just starting out with Nextjs and setting up a new Next.js React project. I used create-next-app to initialize the code base, but then noticed an odd div tag with the class 'selection_bubble_root' right inside the body. Even though its visibility ...

Modify the background image in the Style Sheet using a combination of Javascript and Jquery

I am attempting to develop a javascript and jquery function that will allow me to change the background-image specified in my css file. However, while I am able to successfully modify the background-color property, I am facing difficulties when attempting ...

How can I modify the font style in Bootstrap?

How can I integrate a custom open font into Bootstrap without using a CDN? Should I create a CSS rule to override Bootstrap's default font or locate the font files in Bootstrap and replace them with my desired font? ...

jQuery unable to find designated elements in uploaded templates

I have a specific route linked to a specific controller and view: app.config(['$routeProvider', function ($routeProvider) { $routeProvider .when('/create', { templateUrl: 'partials/form/form.html', controlle ...

Adjusting the template layout of Django Rest Framework

Modifying the login function in the code is my current task - {% block userlinks %} {% if user.is_authenticated %} {% optional_logout request user %} {% else %} {% optional_login request %} {% endif %} {% endblock %} I am loo ...

Updating values of nested objects by ID using Mongoose

As a newcomer to MongoDB, I've been struggling with a simple task that involves changing the status of a process. I've attempted using methods like "FindAndUpdate," "UpdateOne," and "FindByIdAndUpdate," but none seem to be working as expected. ...

JavaScript Object Retrieval: [object Object]

I am currently facing an issue with my node/express app where I keep running into a problem with [object Object]. When rendering a page, I use the following code: app.get('/members', checkAuthentication, function(req, res){ res.render('m ...

Stop hyperlinks from automatically opening in a new tab or window

I'm having trouble with my website links opening in new tabs. Even after changing the attributes to _self, it still doesn't work. Can someone please review my code below and provide a solution? Feel free to ask for more clarification if needed. ...

Submit an Ajax form to process data in PHP upon selecting a radio button

I am troubleshooting an issue with a form that is not submitting data to processor.php for processing and storing responses in a database. Ensuring that the form submission does not cause a page refresh is crucial, as there is an iframe below the form tha ...

Include a description in the cell of the table

I am struggling with adding a small description below one of the columns in my three-column table. I have tried using Grid, but so far, nothing has worked for me. Can anyone provide assistance? To give you a better idea, I have highlighted the desired res ...

Tips on sorting numbers in a column in the x e y format (displayed as powers of 10) the right way

Looking for a way to sort numbers represented as powers of 10, like 5e6 (5,000,000) and 4e4 (40,000), in jqgrid efficiently. Any suggestions? I was considering calculating values in a hidden column and sorting based on that when sorting the x e y column, ...

Trouble with choosing multiple values in Select2

I'm facing a challenge with my webpage that necessitates the use of the select2 component. The requirement is to display selected values upon loading as well. In my JavaScript file, I have two distinct constructs for this purpose. JavaScript - Constr ...