I've got a button that says "SUBSCRIBED", and when the user mouses over it, I want to replace it with another button that says "UNSUBSCRIBE".
(Kind of like how Twitter has the FOLLOWING button that turns into UNFOLLOW if you hover.)
Here's what I've got so far.
$(document).ready(function(){
$('#subscribed_button').hover(function(){
$('#subscribed_button').css("display","none");
$('#unsubscribe_button').css("display","inline");
},function(){
});
$('#unsubscribe_button').hover(function(){
},function(){
$('#unsubscribe_button').css("display","none");
$('#subscribed_button').css("display","inline");
});
});
Everything is a-ok, except when I mouse quickly, in which case the "hover on" seems to get detected, but not the "hover off". So my button occasionally gets stuck in "UNSUBSCRIBE"...which is weird and visually distracting.
Most of the solutions I've seen on Stackoverflow have been related to stopping animations, but in this case, I'm not animating anything; it's a simple display on/off.
Any ideas?
Without seeing it in action, I'm assuming it's because you're showing/hiding 2 different html elements. When you quickly mouse over and out, it's possible the non-active element hasn't become active, etc. etc. causing issues.
For better stability and performance I'd recommend the following:
Use only a single html element for your subscribe/unsubscribe, and rather than showing/hiding 2 elements, simply change the text of the button and/or swap out the background image through a change of css class, etc. Even better if using an image background would be to use a single background image containing both states and just update the background position depending on current state.
The above is simply a concept as you haven't posted your entire code, so modifying it for the above is not possible.
I would use just one element:
$(function() {
var $sub = $('#subscribe_button');
$sub.hover(function() {
$sub.text('Unsubscribe');
}, function() {
$sub.text('Subscribe');
});
});
In jQuery there are methods called mouseenter and mouseleave. http://api.jquery.com/mouseenter/ If you use those it will help with the proper bubbling and should fix your problem.