The use of JQuery's .show() and .hide() functions may disrupt the functionality of CSS hover

When working with this jsFiddle, it appears that the jQuery .show() function does not behave as expected. Despite the documentation stating that it should revert CSS attributes to their original values, using .show(x).delay(x).hide(x) seems to cause the CSS hover code to stop functioning:

Here is the Javascript code being used:

$('.product').on('click', function(){
    $('.drawer').html("Something in your basket")
        .show(200).delay(500).hide(200);
});

And the corresponding CSS code:

.drawer {
    border: 1px solid black;
    padding: 10px;
    position: absolute;
    top: 20px;
    left: 0px;
    display: none;
    width: 200px;
} 
.basket {
    position: relative;
}
.basket:hover .drawer {
    display: block;
}

Finally, the HTML markup:

<div class="basket">
    <a href="#">Basket</a>
    <div class="drawer">No items in your basket</div>
</div>
<br/><br/><br/><br/><br/><br/>
<a href="#" class="product">Add X to basket</a>

There seems to be an issue with how these elements interact when tested on Firefox. Any insights on what might be causing this behavior?

Answer №1

When using jQuery, keep in mind that the CSS attributes are reverted when you use the .show() method, but not when using .hide().

According to the documentation for .hide():

This is similar to using .css( "display", "none" ), but jQuery stores the original display property value so it can be restored later.

If you first use .hide() and then .show(), jQuery will try to bring back the original display value. However, if you call them in the sequence .show(x).delay(x).hide(x), where .hide() is the last action, it will not restore the values but simply set .css("display", "none") on the element (and save a value for future use with .show()). The order matters.

In this scenario, it's recommended to use a complete callback function to remove the display attribute.

$('.product').on('click', function(){
    $('.drawer').html("Something in your basket")
        .show(200).delay(500).hide(200, function() {
            $(this).css("display", "");
        });
});

After making these adjustments, it should work perfectly.

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

Trimming Text with JQuery to Show Tooltip

I am trying to create a tooltip that displays company names when the company_id is hovered over. However, instead of showing the full name like "Company A," it only displays "Company," cutting off everything before a space. Below is the script I am using: ...

Why is the video background not taking up the entire width of the div element?

I've recently integrated the jquery.videoBG plugin into my project to add a video background. Here's the HTML code: <div id="div_demo"> <div class="videoBG"> <video autoplay="" src="media/snow.mp4" style="position: abso ...

The web server is serving an HTML file instead of the expected JSON response

Is there a way to extract the JSON data from ? I have tried using AJAX but it only retrieves the entire HTML page instead. $.ajax({ url: 'http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34', success: function(data) { ...

What is the method for centering an input field with inline CSS?

Is there a way to center align an input text box on a webpage using only inline CSS? I have tried various techniques, including the following code snippet, but nothing seems to work: <input type="text" id="myInput" style= "margi ...

Easily showcase a limitless number of items within a 10-column grid using Bootstrap!

This is the code snippet: <div *ngFor="let minute of state.minutes.specificMinutes.selectedMinutes | keyvalue" class="col-sm-1 checkbox-container"> <div class="custom-control custom-checkbox"> <input type="checkbox" (click)="state.m ...

Adjust the height to match the shortest sibling child div height

In my layout, I have multiple rows with each row containing multiple columns. Within each column, there is a div and a paragraph - the div contains an image. My goal is to set the div with the class cover-bg to the lowest height of cover-bg in the same row ...

Logging in using Mechanize with a jQuery AJAX submission form in Python: A Step-by-Step Guide

Currently, I'm experiencing difficulties logging into the system due to the way the website is handling the form using jQuery and ajax in the following script: function form_login() { if($('#main form p.button.disabled').length) return ...

Columns in Bootstrap compressed

Seeking advice regarding setting up a recruitment portal for my employer. Having trouble with the bootstrap scaffolding - columns appear squashed on smaller devices despite using xs columns. Looking for guidance to make it more responsive. Would greatly a ...

Element in Vue not displaying when modal is hidden

Having trouble displaying a Vue component on a modal controlled by semantic ui. The Vue element is not loading or mounting properly. My setup involves Laravel and jQuery with some Vue elements intertwined. I have a hunch that the issue stems from the moda ...

Explore our interactive map and widget features that will seamlessly fill your browser window for a

Struggling to make a webpage with a Google Map, Chart, and Grid expand to fill the available space on the screen. Tried various CSS tricks but nothing seems to work. Check out this simplified example in JsFiddle that showcases the issue: http://jsfiddle. ...

Is there a way to display product details in a pop-up instead of directing to a new page on Virtue Mart?

Currently, I am in the process of developing a website using Joomla and VirtueMart. My goal is to showcase product details within a lightbox or pop-up on the current page instead of directing users to another page. To get a better understanding, please v ...

Insert well-formed JSON into an HTML element

I'm facing a challenge while trying to dynamically embed a valid JSON array into HTML. The issue arises when the text contains special characters like quotes, apostrophes, or others that require escaping. Let me illustrate the problem with an example ...

Decrease the dimensions of the image while maintaining its width at 100%

I've got a dilemma with the images on my website that are linked from image hosting sites. They all need to be displayed at 100% width, but some of them have huge pixel dimensions which cause slow loading times. Is there a way to resize the images wi ...

The overlay image is not positioned correctly in the center

I am struggling with positioning a play icon overlay on the image in my list-group-item. The current issue is that the icon is centered on the entire list-group-item instead of just the image itself. I am working with bootstrap 4.4.1. Here is the HTML str ...

Affixing a navigation bar to the top while scrolling

Does anyone know how to create a navigation bar that will "dock" to the top of the browser when scrolled to, and undock when scrolled back up? Check out my code snippet here: http://jsfiddle.net/gLQtx/ $(function() { var initPos = $('#stickyNav&apo ...

Make sure the image is aligned in line with the unordered list

I've been having trouble trying to display an image inline with an unordered list. Here's the HTML code: <div class="customer-indiv man"> <a class="customer_thumb" href="/fan/332profile.com"> <img src="http://i.imgur.com/Wi ...

Is there a way to increment a counter with a click?

Seeking a method to create a counter that increments upon button click. Attempted code provided below, however the displayed value remains constant: $(document).ready(function () { $("#gonder").click(function() { var baslik = document.title; ...

Step-by-step guide to creating a border around text with a number included in between the lines

Is there a way to replicate the appearance of the image shown below using CSS, Bootstrap, or other client-side methods? ...

Retrieve the text input from its respective radio button

There are two radio buttons, each accompanied by an input text field. When a user selects a radio button, they can also enter some corresponding text. My inquiry is: What is the best method to retrieve the entered text for the selected radio button? ...

Utilizing jQuery/AJAX to implement the :patch method on the index page in Rails

Can anyone provide insights on how to implement a feature where there is a table named Post with a column called status (having values publish or unpublish)? On the posts#index page, I want to display buttons to mark a post as publish or unpublish. <%= ...