Skip to content

Instantly share code, notes, and snippets.

@jney
Created October 6, 2009 08:52
Show Gist options
  • Save jney/202872 to your computer and use it in GitHub Desktop.
Save jney/202872 to your computer and use it in GitHub Desktop.
// toggle opacity for jQuery. toggles opacity between 1.0 and the specified value.
//
// usage:
//
// jQuery(selectory).toggleOpacity();
// jQuery(selectory).toggleOpacity({speed:"slow"});
//
//
// http://drawohara.com/post/205626543/jquery-toggleopacity
jQuery.fn.extend({
toggleOpacity: function(opts){
var e = jQuery(this);
opts = opts ? opts : {};
e.data('opacityMin', (e.data('opacityMin') || opts.max || (e.css('opacity')/3)));
e.data('opacityMax', (e.data('opacityMax') || opts.min || e.css('opacity')));
e.data('opacitySpeed', (e.data('opacitySpeed') || opts.speed || 'fast'));
if(!e.data('toggleOpacity')){
e.fadeTo(e.data('opacitySpeed'), e.data('opacityMin'));
e.data('toggleOpacity', true);
} else {
e.fadeTo(e.data('opacitySpeed'), e.data('opacityMax'));
e.data('toggleOpacity', false);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment