So my script works but the issue I am having is that when i use the close-but function it hides the div, but after a few seconds the div slidesdown. Am I missing something with like a if statement? Thank you.
$(document).ready(function() {
$(".cart").hover(function() {
$(".dropcart").slideDown('slow');
});
$('.dropcart').mouseleave(function() {
$('.dropcart').css({
'opacity': '.5'
}, setTimeout(function() {
$('.dropcart').slideUp('slow');
}, 3000));
});
$('.dropcart').mouseenter(function() {
$('.dropcart').css({
'opacity': '1'
});
});
$('.close-but').click( function() {
$('.dropcart').hide();
});
});
hoveris a shorthand for themouseenter/mouseleavehandlers (see: http://api.jquery.com/hover/).I tend not to use hover for more complex interactions since, as you’ve discovered, there are some quirks to how the event handler works. I’ve found that binding an event handler to
hoverfires the handler twice. Instead, what you should do is:One think you might want to consider using is jquery’s livequery plugin, which improves event binding/handling for DOM elements. So, your example could be refactored to look something like this:
Edits:
Changed pathing to find the parent element that contains both
.cartand.dropcartUpdate
I didn’t see the fiddle in the initial comments, and now that I understand the structure, I’ve revised the examples above to reflect (see revisions above). A couple additional things I would also recommend doing:
class="cart-container float-left"orid="cart-container"An example of the html refactoring would be:
Supporting CSS:
Your jQuery script would be something like:
You can see it in action here: http://jsfiddle.net/kwbbh/2/ (forked the OPs fiddle)