I'm working on a navigation plug-in which takes a list and places each item in its on div which will act like a button. Its similar to a standard drop down menu.
My problem comes in at the function setFocus(). This will take a selected div and add a hover/mouseOver/mouseEnter function.
I list all three because I've tried all three and have the same issue with all of them. If you notice within the function, I currently have it set to alert with the text 'over' when the mouse enters the div.
For some reason, this runs whether or not the cursor has entered the div. When the page loads, even if the cursor isn't within the window space, the alert fires as if the cursor has entered the div and mouseLeave never responds even if you roll over the div. Has anyone else had an issue similar to this? I'm stumped.
var divCode = String();
//grab each list item and put it in its own div
$('#nav li').each(function() {
divCode += "<div>" + $(this).html() + "</div> ";
});
//get rid of the list and replace it with a plane ol' div
//then fill that it with our new "button" divs
$('#nav').replaceWith('<div id="newNav"> </div)');
$('#newNav').html(divCode);
//add some functionality to the divs
$('#newNav div').addClass('fader')
.each(function() {
if ($(this).css('position')!='relative' || $(this).css('position')!='absolute'){
$(this).css('position','relative');
}
})
.css('cursor', 'pointer')
.click(function() {
switchDivs($(this));
});
//set the min-height of the nav div
$('#newNav').css('minHeight', $('#newNav').height());
setFocus($('#newNav div').first());
$('.fader').hide();
function fadeOn(speed) {
$('.fader').fadeTo(speed, 100);
alert('over');
}
function fadeWipe(speed) {
$('.fader').fadeTo(speed, 0);
alert('out');
}
function setFocus(obj) {
obj.removeClass('fader')
.addClass('focus')
.css('backgroundColor', 'red')
.hover(fadeOn(500),fadeWipe(500));
}
function switchDivs(obj2) { //obj2 is the object to become the focus div
var obj1 = $('.focus');
//if the two objects are the same, quit
if (obj1.text() == obj2.text()) {
return;
}
var oneOff = obj1.offset();
var oneDirectionY = "-"; //lol...one direction
var twoOff = obj2.offset();
var twoDirectionY = "-";
var movementTotalY = 0;
if (oneOff.top <= twoOff.top) {
oneDirectionY = "+";
movementTotalY = twoOff.top - oneOff.top;
} else {
twoDirectionY = "+";
movementTotalY = oneOff.top - twoOff.top;
}
var oneDirectionX = "-"; //lol...one direction
var twoDirectionX = "-";
var movementTotalX = 0;
if (oneOff.left <= twoOff.left) {
oneDirectionX = "+";
movementTotalX = twoOff.left - oneOff.left;
} else {
twoDirectionX = "+";
movementTotalX = oneOff.left - twoOff.left;
}
obj1.animate({ top : oneDirectionY+"="+movementTotalY+"px",
left : oneDirectionX+"="+movementTotalX+"px"
},1500);
obj2.animate({ top : twoDirectionY+"="+movementTotalY+"px",
left : twoDirectionX+"="+movementTotalX+"px"
},1500);
//remove focus from object 1
obj1.removeClass('focus')
.addClass('fader');
//add focus to object 2
obj2.removeClass('fader')
.addClass('focus');
}
});