Ensure that the hover div remains open even after the mouse leaves, and only closes when clicked outside of the div window or on a

I have a jQuery script that controls the opening and closing of the "shopping_cart" div when hovering over the "shopping_button".

$('#shopping_button, .shopping_cart').on('mouseenter',function(){
  $(this).css('z-index','300');
  $(this).css('visibility','visible');
 })
$('#shopping_button, .shopping_cart').on('mouseleave',function(){
  $(this).css('z-index','189');
  $(this).css('visibility','hidden');
  $(this).css('opacity','0');
 });

I want the "shopping_cart" to stay open when the mouse leaves, and only close when clicking outside of the div or using a close button inside the "shopping_cart".

Thank you to everyone for your help.

Edited

I added this code to the existing script:

$('html').click(function () {
    $('.shopping_cart').css('visibility','hidden');
});

While this successfully closes the window when clicking outside of it, now the "shopping_cart" won't open again when hovering over the "shopping_button". It seems to remain hidden according to the inspect element tool.

$('#shopping_button, .shopping_cart').on('mouseenter',function(){
  $(this).css('z-index','300');
  $(this).css('visibility','visible'); <---This Remain Hidden
 })

Any suggestions?

Sorted
I added this line to the mouseenter function:

  $('.shopping_cart').css('visibility','visible');

And it works! Still open to cleaner solutions if anyone has any.

Thanks again for the assistance.

Answer №1

Be sure to take a look at this JSFiddle: http://jsfiddle.net/m8nrzL7d/2/

$('#shopping_button').mouseenter(function(){
   $("#shopping_cart").css('display','block');
})
$('#btn-close').click(function(){
   $("#shopping_cart").css('display','none');
})
$('html').click(function() {
   $("#shopping_cart").css('display','none');
});

$('#shopping_cart').click(function(event){
    event.stopPropagation();
});

I made some tweaks to your code. I introduced a button for quick implementation and changed the cart to an ID instead of a class based on my past experiences with classes and jquery. I opted for display: none over visibility: none as it doesn't take up space (or does it need to? Check here)

UPDATE: I also included a feature to close the cart when clicking outside. More details can be found here!

I trust this information is helpful to you...

Answer №2

It's best practice to utilize CSS classes for styling elements:

.cart_display {
z-index: 189;
visibility: hidden;
opacity: 0;
}
.cart_display_active {
z-index: 300;
visibility: visible;
}

To activate the class cart_display_active on hover, you can do the following:

$('#cart_button, .cart_display').on('hover', function(){
$(this).addClass('cart_display_active');
});

If you want to deactivate it outside of the element, you can use this code:

$(html).on('click', function(){
$('.cart_display').removeClass('cart_display_active');
})

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

Ajax sending incorrect parameter values

Having some trouble with an ajax request where the parameter I am trying to send is not being transmitted correctly. The issue seems to be in my user.php file. <script> function showUser(str) { if (str=="") { document.getElementById("txtHint" ...

Transform form information for Web Api put request using MVC 5

I am working on a strongly-typed Razor View that presents a record for a particular class. The view includes a form allowing the user to update the record through various controls such as a dropdown, checkboxes, and textboxes. Whenever a user modifies a fi ...

Initiating the animation/game loop for a three.js object

I am utilizing angularJS and Three.js for the frontend of my project. The Three.js object is created in the following manner: var ThreeObject = (function() { //constructor function ThreeObject() {} ThreeObject.prototype.init = functio init (){ //initial ...

Retrieving data from MongoDB for rendering on an HTML page

I have successfully inserted data into my MongoDB database, but I am facing issues with the "get" function to display this data on my HTML page. My current setup involves using Node.js with Express framework and Angular for front-end development and routi ...

Is there a way to select and style the nth-child element within a Bootstrap .card body?

I am experimenting with changing the background-color of the card-body in bootstrap 4, but I am unsure about how to access the card-body element since each body has a different parent (I believe my understanding is correct). Can this be achieved using the ...

Sharing Data Across Multiple Windows in Angular 2 Using a Singleton List

My multiplayer game involves adding new players to a single game room lobby, which is essentially a list of current players. How can I update this list for all connected players when new ones join? I implemented a service and included it in the providers ...

Is there a way to assign the scroll event context to `document.body` instead of `window`?

Discovery As I delved into the intricacies of web development, I stumbled upon a curious observation. In a particular MDN page, I noticed that the left and right sidebars had their scrollbars contained within themselves. However, the content block's ...

Tips on assigning a value to a dynamically generated drop-down element

I used arrays of strings to populate drop-down menus. Is there a way to automatically set the value of each option to match the text content? el.value = opt; seems to be ineffective. var validCoursesKeys = ['opt 1','opt 2','opt ...

Issue with redirecting after confirming user info permissions on Facebook login

Can anyone assist me with the final step of my Facebook login feature? The current issue is that when a new user first signs in, they are greeted with a popup labeled 'www.facebook.com/v11.0/dialog/oauth' that asks them to authorize my page to a ...

Tips for aligning content in the Login Screen of Framework7?

I've been working on a cross-platform mobile application with Framework7. For the login screen, I followed a tutorial from https://www.tutorialspoint.com/framework7/login_screen_start_app.htm Here is the code snippet of my index.html with the login m ...

Could you provide some insights on the topic of "Methods" within a jquery plugin?

Check out the following link: There is a "Methods" section, but I am confused about its usage as there are no examples provided. Specifically, I am unsure how to use the method "markAllVisited". Does it mean something like this in the code snippet below ...

Setting value in Angular's ng-repeat directive

Utilizing ng-repeat, I am creating a table with radio buttons. The main goal is to assign each radio button a value based on the position of the object in the original array (before any sorting takes place). However, using $index assigns the position in ...

Adjusting column widths in Material-Table: A guide to resizing columns

I am currently using the Material Table library, recommended by Google Material UI as a top data table library. I am facing some issues related to configuring the width of columns in this library. The column's `width` property seems to be functioning ...

Property that is dynamically populated based on data retrieved from an external API

My template relies on an API call (Firebase) to determine the return value of my computed property, which in turn decides whether certain elements are displayed. However, I've noticed that my computed property is not reactive - its value in the templ ...

How can I set the width of a container dynamically using the Next JS Image component?

I have a situation where I am trying to set the height of an image within a container div to be equal to 100% of the container's height, while allowing the width of the container to adjust based on the image's aspect ratio. When using a standard ...

Is there a way to create a list of languages spoken using Angular?

I am in search of a solution to create a <select> that contains all the language names from around the world. The challenge is, I need this list to be available in multiple languages as well. Currently, I am working with Angular 8 and ngx-translate, ...

Leverage scope variables and dynamic CSS properties when using ngClass

Is there a way to manipulate the CSS dynamically in HTML using a scope variable? <div ng-class="myClassScope, { 'dynamic-class': !ifIsNot }"> ...

Assistance needed with incorporating an additional link button into the extensive menu

I recently checked out the impressive mega menus featured on the w3schools website. You can find them here: (https://www.w3schools.com/howto/howto_css_mega_menu.asp). While using the "try it yourself" option, I tried to experiment with adding another mega ...

Utilizing setCustomValidity in Bootstrap 4 to dynamically display error messages in .is-invalid classes

The documentation for Bootstrap mentions that custom validity messages can be provided using setCustomValidity in JavaScript. According to the documentation, custom validity messages can be set using the setCustomValidity method in JavaScript. As inter ...

Make sure to allow the async task to complete before beginning with Angular JS

As I develop an app using MobileFirst v8 and Ionic v1.3.1, I encounter issues with timing in my code execution. Specifically, when the application initiates, the regular ionic angular code within my app.js file runs. This section of the code handles the i ...