Skip to content

Instantly share code, notes, and snippets.

@jcsrb
Created January 5, 2012 13:55
Show Gist options
  • Select an option

  • Save jcsrb/1565353 to your computer and use it in GitHub Desktop.

Select an option

Save jcsrb/1565353 to your computer and use it in GitHub Desktop.
jQuery Hover actions
<a
data-hover-actions="true"
data-hover-text="remove from favourites"
data-hover-class="ui button orange follow"
class="ui button green active follow"
href="/toggle_favourite"
>
<span class="icon"></span>
is one of your favourites
</a>
$(document).on("hover", "[data-hover-actions]=true", function(event){
$(this).has("span.icon").data('hover-icon',true); //check if target has an icon
if( event.type === 'mouseenter' ){
if ($(this).data('hover-text')!=undefined) {
$(this)
.data("original-text",$(this).html()) //save the old one
.text($(this).data("hover-text")); //add the new one
}
if ($(this).data('hover-class')) {
$(this)
.data("original-class",$(this).prop('class')) //save the old one
.removeClass() // remove all old classes
.addClass($(this).data("hover-class")); // add hover classes
}
}
else{
if ($(this).data('hover-text')!=undefined && $(this).data('original-text')!=undefined) {
$(this)
.text($(this).data("original-text")); //reinstate the old one
}
if ($(this).data('hover-class')!=undefined && $(this).data('original-class')!=undefined) {
$(this)
.removeClass() // remove all old classes
.addClass($(this).data("original-class")); //reinstate the old one
}
if($(this).is(":has(span.icon)")){
$(this).data('hover-icon',true).prepend('<span class="icon"></span>');
}
}
if($(this).data('hover-icon')){
$(this).prepend('<span class="icon"></span>'); // reinstate the icon after changes
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment