Use jQuery to toggle a div inside another div when hovered over

I am trying to implement a quick view tab that appears when a customer hovers over a product. I have almost figured it out, but the issue is that once the mouse moves onto the quick view div button, it starts to fade away because technically it has moved off the trigger div. How can I make sure it stays visible?

Below is the jQuery code I am using:

    $("#cat_product").hover(
    function () {
        $(".quickview").fadeIn();
    },
    function () {
        $(".quickview").fadeOut();
    }
);

Here is the link to the fiddle where I am testing things out - https://jsfiddle.net/a0j96wyc/

Answer №1

One suggestion is to implement a hover event for the .quickview element as well. When moving the cursor from #cat_product to .quickview, the hover event for .quickview will take precedence and prevent the button from fading out.

Since the button is fully enclosed within #cat_product, there is no need for a separate "hover off" event for .quickview. You will always be hovering over #cat_product when interacting with the button.

Additionally, I am utilizing the stop() function to clear the animation queue and avoid fade flickers.

$("#cat_product").hover(
  function () {$(".quickview").stop(true,true).fadeIn();},
  function () {$(".quickview").fadeOut();}
);

$(".quickview").hover(
  function () {$(this).stop(true, true).show(0);}
);
.quickview {
  display: block;
}
.hidden {
  display: none;
}
... (The rest of the code remains unchanged)

Edit:

CSS Solution

By slightly modifying your HTML structure, you can leverage CSS transitions to animate opacity without the need for JavaScript. Here's an example:

.quickview {
  opacity: 0;
  -webkit-transition: opacity .2s ease-in-out 0s;
  transition: opacity .2s ease-in-out 0s;
}
div#cat_product:hover div.quickview {
  opacity: 1;
}
... (The rest of the CSS styles remain unchanged)

Answer №2

Check out this alternative solution I came up with. By utilizing the setTimeout() function and adjusting the css properties, we can achieve the desired animation effect.

CSS:

.quickview{
    display: block;
    opacity: 0;
    transition: opacity .2s linear;
}

JavaScript:

var $quickView = $(".quickview");
var timeout = null;

$("#cat_product").on({
    mouseenter: function () {
        $quickView.css("opacity", 1);
    },

    mouseleave: function () {
        timeout = setTimeout(function() {
            $quickView.css("opacity", 0);
        }, 50);
    }
});

$('.quickview').on({
    mouseenter: function() {
        clearTimeout(timeout);
    }
}); 

Take a look at the live demo here.

UPDATE: For an alternative version that does not rely on jQuery, check out this link.

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

The issue of the JQuery div failing to center itself persists even after refreshing the page

I have implemented a script to centrally align the main content div within the viewing area. It works perfectly on page load or resize, but upon page refresh, the content div shifts to the left side of the page. You can see the issue here when pressing re ...

Elements appear with varying widths in Firefox compared to Webkit-based browsers

I believe it would be simpler for you to inspect the elements rather than me writing the code, so I have provided a link: music band site. Now onto the issue: When the window width is reduced in Firefox, you will notice that the social icons in the yellow ...

Dealing with Jquery AJAX requests to NLB and navigating the challenges of cross-domain scripting restrictions

I am currently working on a dashboard application that utilizes Jquery AJAX calls to interact with a RESTFUL WCF service. Everything runs smoothly when the UI application and service are on the same server. However, due to issues with cross site scripting ...

The formatter function provides the complete HTML markup for the slickgrid

Currently, I'm working on a project where I need to identify if a cell has been edited in slickgrid. To achieve this, I am attempting to utilize a custom formatter. function determineEdit(row, cell, value, columnDef, dataContext){ var varRow = da ...

What is the process for creating a two-column table in Angular?

Currently, I have an Angular template code that displays the header on the left and data on the right in a top to bottom layout. <div class="panel-body"> <table class="table table-striped"> <tr ng-repeat="f in fields"&g ...

Can a URL be included in the options of an rmarkdown header?

We have developed a method within our team to create branded reports from rmarkdown documents using custom CSS and HTML files. To maintain consistency, we utilize absolute paths to the style documents in our shared file structure. This allows us to easily ...

How can I programmatically close the Date/Time Picker in Material UI Library?

Is there a way to programmatically close the Date/Time Picker in Material UI Library? ...

Adjusting the screen width to activate a responsive nav bar with a centered title

I'm facing an issue with the alignment of the heading within a navigation bar, causing the links to wrap before the collapse of the nav-bar occurs - https://i.sstatic.net/bKyPM.png I am trying to make the links collapse earlier than they currently d ...

What would be the best method for refreshing CSS following the dynamic addition of data using jQuery for optimal efficiency?

This paragraph was inserted following an ajax request: <tr id="product1" class = "success"> <td>1</td> <td>product one</td> </tr> The success class gives the row a green background, but this style is not applie ...

Issue displaying content in a two-column setup on Chrome and Mozilla browsers due to ASP problem

I've encountered an issue with a footer appearing incorrectly in Chrome and Mozilla browsers when using a 2-column layout, although it displays fine in IE8. Currently, I'm coding in asp with css includes to render the footer. Here's the CSS ...

What could be causing the issue with setting the right margin (element.style.marginRight = xxx) not working?

I used JavaScript to set the margin-right of a div when the window is resized. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device- ...

Add another condition to the current JavaScript rule

http://jsfiddle.net/e8B9j/2/ HTML <div class="box" style="width:700px">This is a sentence</div> <div class="box" style="width:600px">This is a sentence</div> <div class="box" style="width:500px">This is a sentence</div> ...

Struggling to modify CSS properties for :before and :after pseudo-elements?

My current styling looks like this: section.tipos .content > div:before { background: none repeat scroll 0 0 #DDDDDD; content: " "; height: 10px; left: 32%; position: absolute; top: -10px; width: 10px; } It's working p ...

Creating an app using Ionic 1 that features scrollable tabs with the ability to handle overflow

My current code snippet is displayed below. On smaller mobile screens, all 7 tabs are not visible at once and instead appear in two rows which looks messy. I want to modify the display so that only 3 tabs are visible at a time and can be scrolled through. ...

Are jquery form labels a myth? Do they even exist?

I am currently in the process of creating a dynamic input element and I am looking for ways to set the label for it. I have experimented with various methods, but haven't found the perfect solution yet. //getting the value from the input var getValue ...

Ways to style text using variables in SASS without using quotation marks

When working in SASS, I have created the following mixin: @mixin background_gradient($dir, $from, $to) { background: linear-gradient('to #{$dir}', $from, $to); // for IE10 } However, when viewing it in the browser, the output appears as: b ...

The steps to triggering a button click after e.preventDefault()

When attempting to prevent a click() event of a button by using preventDefault() after unbinding the button with unbind(), I encountered an issue where it did not work as expected. <script> $("#update2FAButton").on("click",function(e){ e.pre ...

Discovering the data content received from a server using jQuery ajax techniques

Main.php <!DOCTYPE html> <html> <head> <title>Tutorial 21: Simple AJAX Requests with jQuery</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script> & ...

Customizing Bullet Points in CSS Styling

In an attempt to replicate a specific style in my <ul>, I have utilized the CSS property list-style-image. However, I am facing difficulties aligning the text after the image. How can I achieve this desired alignment? You can view the current output ...

Inserting lines into the text, creating a visually appealing layout

Can a notepad design be created using CSS only? Is it feasible to include lines between text lines without using underscores? I want it to look like this example but with minimal spacing between lines and no manual insertion of span tags since the text wi ...