I'm somewhat new to Javascript / jQuery (but not to programming), so please excuse any ignorance on this matter. I've already looked at this question and this one, but neither one solved my problem.
My scenario: I have a slideshow written in Javascript / jQuery. In this slideshow, I would like show the controls (play button, backward, forward) when the user hovers over the slide and hide them when the mouse leaves the slide area. The buttons show and hide as expected, but when I hover over one of the buttons (which are also over the slideshow) it disappears (which I know is because of "mouseleave" on the slide).
I tried adding hover event handlers to my buttons to change a global boolean so that the slideshow controls will only be hidden if that boolean is false (meaning the "mouseenter" for one of the buttons wasn't fired). I then registered the "mouseenter" event before the "mouseleave", but it didn't work.
Here's the problem: Despite the "mouseenter" event for the button being fired before the "mouseleave" event for the slide, the event handler callbacks are executed in the opposite order. I'm not entirely sure why this is happening, but I know that it has to do with how hover events are handled in Javascript.
I read a little bit about Deferred objects and I'm wondering if this might be the right way to go about this?
The syntax is incorrect, but I'm thinking something along the lines of:
function isMouseOverButton() {
if(mouseEnteredButtonEvent()) {
window.isOverButton = true;
} else {
window.isOverButton = false;
}
function hideControls() {
if(!window.isOverButton) {
//hide buttons
}
}
$.when($("#slide").mouseleave()).then(isMouseOverButton()).then(hideControls());
Is there anyway to combine event handlers and deferred objects like this? Or, is there a better way than deferred objects?
EDIT: I just came across this, but I would prefer not to use a plugin to accomplish this.