Skip to content

Instantly share code, notes, and snippets.

@darcyclarke
Last active March 7, 2017 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darcyclarke/e582dd262e5c6d366b5747a5b944c6f3 to your computer and use it in GitHub Desktop.
Save darcyclarke/e582dd262e5c6d366b5747a5b944c6f3 to your computer and use it in GitHub Desktop.
jQuery plugin for animating elements with floats
/*!
* Animate Floats jQuery Plugin
* http://darcyclarke.me/articles/development/animate-float-positions-in-jquery-1-5/
*
* Copyright 2016, Darcy Clarke
* Do what you want license
*/
(function(window, $){
var $plugin = $.sub();
$plugin.fn.animate = function(props, speed, cb){
if(typeof(speed) == 'function')
cb = speed, speed = 500;
if(typeof(cb) != 'function')
cb = function(){};
return $.each(this, function(i, el){
el = $(el);
if(props.float && props.float != el.css('float')){
var elem = el.clone().css(props).insertBefore(el),
temp = (props.float == el.css('float')) ? elem.position().left : el.position().left;
props.marginLeft = elem.position().left;
elem.remove();
el.css({float:'left',marginLeft:temp});
}
$(this).animate(props, speed, function(){
$(this).css(props);
cb();
});
});
};
window.$ = $plugin;
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment