Skip to content

Instantly share code, notes, and snippets.

@marciofmjr
Created January 3, 2020 23:20
Show Gist options
  • Save marciofmjr/f2e91d4ef7565c59124e8172bcb049bd to your computer and use it in GitHub Desktop.
Save marciofmjr/f2e91d4ef7565c59124e8172bcb049bd to your computer and use it in GitHub Desktop.
function _spread(e,n){var t=$(e),i=n.initElementsDelay?n.initElementsDelay:100,a=n.minAnimationSpeed?n.minAnimationSpeed:2,o=n.maxAnimationSpeed?n.maxAnimationSpeed:3,r=!!n.resizeElement&&n.resizeElement,s=n.hoveredWidth?n.hoveredWidth:30;if(o<a){var m=a;a=o,o=m}a<.5&&(a=.5),10<o&&(o=10),prepareMechanic(t),t.each(function(e,n){resetElement(n=$(n),r,s),setTimeout(function(){initElementRandomPosition(n),animateElement(n,a,o)},i*e)})}function setHoverAction(e,n){var t=parseInt(e.css("width"));n=t+n;e.hover(function(){e.css({width:n+"px"})},function(){e.css({width:t+"px"})})}function prepareMechanic(e){e.first().parent().css({display:"block",position:"relative",overflow:"hidden"})}function resetElement(e,n,t){var i=e.parent(),a=getContainerWidth(i),o=getContainerHeight(i),r=random(0,a,!0),s=random(0,o,!0);!1!==n&&e.css({width:n}),e.css({position:"absolute","margin-left":r+"px","margin-top":s+"px",display:"none"}),setHoverAction(e,t)}function animateElement(e,n,t){var i=e.parent(),a=parseInt(e.css("width")),o=getContainerWidth(i),r=(parseInt(e.css("margin-left")),random(n,t,!0));e.css({transition:"margin-left "+r+"s ease-in-out, width 0.3s ease-in-out","margin-left":"+="+(a+o)+"px"}),setTimeout(function(){e.css({transition:"margin-left 0s ease-in-out","margin-left":"-"+a+"px"}),animateElement(e,n,t)},1e3*r)}function getContainerWidth(e){return parseInt(e.css("width"))}function getContainerHeight(e){return parseInt(e.css("height"))}function initElementRandomPosition(e){e.fadeIn(1e3)}function random(e,n,t){e=Math.ceil(e),n=Math.floor(n);var i=Math.random()*(n-e+1);return t?Math.floor(i)+e:i.toFixed(2)}jQuery.fn.extend({spread:function(e){return _spread(this,e)}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment