or install via Bower
bower install miracle
Miracle plugin gives you control over appearance of elements. Helps you to turn appearance of elements on page load from stuttering chaos to perfect synchronous performance of the symphony.
Long read about problem that Miracle targeted to solve: «Elements animation on initial page render: CSS animations and load event to the rescue from messy page render»
Plugin can wait & show element with effect after:
- element's images dependencies loaded
- other element's show effect ended
- other element's images dependencies loaded
- trigger from your code
Declare with ease right in element attributes:
- built-in customizable show effects
- fully customizable show effects
- complex show order between many elements
- spinner before show
- show timeouts
- properties inheritance between miracles
Plugin needs jQuery 1.10.2 or later. Earlier versions are not tested with Miracle.
API navigation
- Built-in effects
- Custom effects: CSS classes approach
- Custom effects: Inline CSS approach
- data-m-opaque
- data-m-timeout
- Spinner
- data-m-id
- Await
- data-m-inherit
- If one of miracle's image dependencies was not loaded
- Dynamic elements rendering and Miracle. Manual initialization.
or install via Bower
bower install miracle
In your <html/> add this to <head/>:
<head>
...
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/spin.js"></script> <!-- optional -->
<script src="js/miracle.min.js"></script>
...
<head/>Assign class «miracle» to element:
<div class="miracle">...</div>Miracle initializes as soon as DOM is ready. Plugin parses for miracles (elements with class «miracle») and prepares them for special appearance behaviour. Then plugin waits when all image dependencies of miracle will be loaded then shows miracle with effect.
If you want to initialize Miracle manually for dynamically added elements — see here
Images dependencies:
-
<img/>elements - CSS declarations with
url("...")(shorthands & precise variants):-
background(&background-image; multiple backgrounds are handled too) -
list-style(&list-style-image) -
border-image(&border-image-source)
-
Without image dependencies miracle shows immediately according to declared properties (effect, custom effect, await, show timeout etc.)
You can customize miracle appearance behavior in declarative way right in html using data- attributes. You say — plugin works.
Example:
<div class="miracle" data-m-effect="from-space"
data-m-duration="444" data-m-await-show="prev"
data-m-timeout="111" data-m-spinner="true">...<div>Some miracles that uses built-in show effects initially hidden using opacity: 0. If you declare custom effect then you need to take care about both initial and final states styles yourself.
Miracles have implicitly declared properties that plugin needs to create basic show effect:
data-m-effect="fade-in"data-m-duration="500"data-m-easing="ease-in-out"
Choose one that you like. Built-in effects designed to work without any customization but if you want there all what you need — Built-in effects settings.
Default: «fade-in»
Built-in effects names:
-
fade-in: Transition from
opacity: 0toopacity: 1. -
ease-y: Transition from
scaleY(0.9)toscaleY(1)gives elegant presentation effect. -
ease-x: Transition from
scaleX(0.9)toscaleX(1)gives elegant presentation effect. -
from-space: Miracle lands from outer space like apps icons on iOS. Transition from
scale(3),translate(-200px)toscale(1)and original position. -
from-hell: Miracle comings from hell like a devil. Transition from
scale(0),translate(-200px)toscale(1)and original position. -
scale: Transition from
scale(0)toscaleY(1). -
slide: Miracle comings from another position. Transition from
translate(0px,-200px)to original position. This effect doesn't make sense without setting data-m-translate
Example:
<div class="miracle" data-m-effect="from-space">...</div>These properties work only with built-in effects.
How long effect should lasts.
Default: «500»
Example:
<div class="miracle" data-m-effect="ease-y"
data-m-duration="333">...</div>You can set your own transition easing for built-in effect.
Default: «ease-in-out»
Example:
<div class="miracle" data-m-effect="ease-y"
data-m-easing="cubic-bezier(0.1, 0.7, 1.0, 0.1)">...</div>
<div class="miracle" data-m-effect="ease-y"
data-m-easing="linear">...</div>You can set initial scale of element for built-in effect.
Affected effects:
-
ease-y: Initial
scaleY()value
Default: «0.9»
-
ease-x: Initial
scaleX()value
Default: «0.9»
-
from-space: Initial
scale()value
Default: «3»
-
from-hell: Initial
scale()value
Default: «0»
-
scale: Initial
scale()value
Default: «0»
Example:
<div class="miracle" data-m-effect="scale" data-m-scale-init="0.2">...</div>You can set the origin for transformations of an element for built-in effect.
Affected effects:
-
ease-y: Origin for
scaleY()transformation
Default: «center»
-
ease-x: Origin for
scaleX()transformation
Default: «center»
-
from-space: Origin for
scale()transformation
Default: «center»
-
from-hell: Origin for
scale()transformation
Default: «center»
-
scale: Origin for
scale()transformation
Default: «center»
Example:
<div class="miracle" data-m-effect="scale" data-m-origin="left top">...</div>Note:
If «x-offset» presented, but «y-offset» doesn't then «y» mimics «x».
You can set the values for initial state's translate transformations of built-in effect. From that position miracle effect will start.
Affected effects:
-
from-space: Values for initial state's
translate().
Default: «-200px, -200px»
-
from-hell: Values for initial state's
translate().
Default: «-200px, -200px»
-
slide: Values for initial state's
translate().
Default: «0px, -200px»
Example:
<div class="miracle" data-m-effect="slide" data-m-translate="-400px, -400px">...</div>You can set your own show effect with CSS classes or inline CSS declarations.
You can use CSS classes from your own stylesheets to create custom show effect.
Note dot before class name.
Declare initial state style of miracle:
Example:
<div class="miracle" data-m-style-init=".initialState">...</div>.initialState {
opacity: 0;
transform: scale(0) translateX(-200px) translateY(-200px);
}Declare final state style of miracle. How user should see miracle at the end of effect:
Example:
<div class="miracle" data-m-style-final=".finalState">...</div>.finalState {
opacity: 1;
transform: scale(1) translateX(0) translateY(0);
transition-property: opacity, transform;
transition-duration: 600ms;
transition-timing-function: ease-in-out;
}It's more powerful and convenient then inline CSS approach. Especially if you are using Autoprefixer that automatically adds prefixes to your CSS declarations.
With classes approach it's easy to create elements that responsive to images dependencies loading by deeply modifying element style from your separately defined css classes.
You can create custom effect right in HTML attributes with inline CSS declarations.
Declare initial style of miracle:
Example:
<div class="miracle"
data-m-style-init="opacity: 0; transform: scale(0) translateX(-200px) translateY(-200px);">...</div>Declare final style of miracle. How user should see miracle at the end of effect:
Example:
<div class="miracle"
data-m-style-final="opacity: 1; transform: scale(1) translateX(0) translateY(0); transition-property: opacity, transform;transition-duration: 600ms; transition-timing-function: ease-in-out;">...</div>Note:
- take care about vendor prefixes
- if built-in
data-m-effectpresented then custom effect will be ignored.
Some miracles that use built-in show effects initially hidden using opacity: 0. But sometimes you may want to rewrite it or rewrite inherited property of miracle.
Example:
<div class="miracle" data-m-opaque="true">Before shows himself miracle can wait some time after all other delays.
Example:
<div class="miracle" data-m-timeout="1000">...</div>Plugin can show spinner at the center of miracle position until miracle show effect begins.
You should add spin.js plugin (not included in Miracle plugin) in <head/> before miracle.js
Example:
<head>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/spin.js"></script>
<script src="js/miracle.js"></script>
</head>
<body>
<div class="miracle" data-m-spinner="true">
</body>Note:
- Miracle should have
position: relative, because spinner will be centered inside miracle withposition: absolute; width: 0px; left: 50%; top: 50%. - If there is no
spin.jsin HTML — there won't be a spinner.
You can set your own spinner style for miracle and apply this style only to selected miracles.
Go to spin.js page and configure your spinner style with handy sliders. Under title «Usage» on this page you will see object var opts = {...} with spinner's style options.
In your <html/> in <head/> add <script/> tag after miracle.js:
<head>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/spin.js"></script>
<script src="js/miracle.min.js"></script>
<script>
M.settings.spinner.style.customSpinnerStyle = {
lines: 5,
length: 10,
width: 2,
//... etc.
}
</script>
</head>In this <script/> tag create M.settings.spinner.style.customSpinnerStyle object where customSpinnerStyle is name of your custom spinner style.
Copy your earlier configured spinner options to this object.
Declare data-m-spinner-style property for miracle with customSpinnerStyle as value:
<div class="miracle" data-m-spinner="true"
data-m-style="customSpinnerStyle">...</div>Done. Plugin will use your custom spinner style for this miracle.
It is utility miracle property. It doesn't affect miracle appearance. Properties like «data-m-await-show» or «data-m-inherit» need it.
Example:
<div class="miracle" data-m-id="godzilla">...</div>
<div class="miracle" data-m-await-show="godzilla">...</div>In case above second miracle will await when miracle with m-id «godzilla» will be shown then it shows himself.
Example:
<div class="miracle" data-m-id="godzilla"
data-m-effect="from-space" data-m-duration="444"
data-m-spinner="true">...</div>
<div class="miracle" data-m-inherit="godzilla">...</div>In case above second miracle inherits data-m-effect, data-m-duration and data-m-spinner properties from miracle with m-id «godzilla».
Miracle can await when another miracle will be «loaded» or «shown» before start to show himself or own trigger. You can run complex ordered miracles orchestra. Declare and Conquer.
Miracle waits when miracle with specified m-id or just previous miracle will be loaded then shows himself.
Example:
<div class="miracle" data-m-id="first">...</div>
<div class="miracle" data-m-await-load="first">...</div>Two miracles can await each other «loaded» signal and show synchronously.
<div class="miracle" data-m-id="yellow" data-m-await-load="red">...</div>
<div class="miracle" data-m-id="red" data-m-await-load="yellow">...</div>This could work with custom effects only if you manually declared overall custom effect duration via data-m-duration. With built-in effects data-m-await-show works fine without this tuning.
Miracle waits when miracle with specified m-id or just previous miracle will be shown then shows himself
Example:
<div class="miracle" data-m-id="first">...</div>
<div class="miracle" data-m-await-show="first">...</div>Note:
«m-id» of awaited miracle can't be "prev".
Miracle doesn't start to show himself until your programmatically command to him from your code.
How to command to show miracle whenever you want:
$('#some-id').trigger('m-show');Example:
<div id="some-id" class="miracle" data-m-await-trigger="true">...</div>$('#some-id').trigger('m-show');One miracle can inherit other miracle properties. You don't need to repeat the similar properties between similar miracles. Especially bad case may be when your need to pollute your code with repeating same custom effect.
One miracle which lend properties called «typical», one which borrow properties called «similar».
Example:
<div class="miracle" data-m-id="typical-miracle"
data-m-effect="ease-y" data-m-duration="333"
data-m-spinner="true">
<div class="miracle" data-m-inherit="typical-miracle">
<div class="miracle" data-m-inherit="typical-miracle">Second and third miracles will have the same properties as «typical-miracle»: data-m-effect="ease-y" data-m-duration="333" data-m-spinner="true"
Example:
<div class="miracle" data-m-id="typical-miracle"
data-m-effect="ease-y" data-m-duration="333"
data-m-spinner="true">
<div class="miracle" data-m-inherit="typical-miracle"
data-m-duration="111" data-m-await="typical-miracle">Second miracle inherits all «typical-miracle» properties, rewrites data-m-duration property and extends his properties with data-m-await="typical-miracle".
To not interrupt overall miracles effects order on page, Miracle'll simulate load event for this image, continue to await other image dependencies and finally show miracle. So miracle element will be shown but with gap on not loaded image's place and will look according to your design without this image. Also plugin throws error in console to notify about this case.
«Image dependency was not loaded» example
To apply special style for miracle if one of image dependencies failed to load use data-m-error-style=".className". Where .className may be any class from your stylesheets. Note dot before class name.
In future there will functionality to choose which types of images dependencies should case error. For example, border-image may be not so important as <img /> to cause miracle restyling in failed to load case.
##Dynamic elements rendering and Miracle By default Miracle initializes as soon as DOM is ready. So plugin can't know about all future miracles you'll render dynamically from your code. Especially it is a problem for templates rendering.
To handle dynamically rendered elements you need to initialize Miracle manually as soon as element appended to the page:
/* Create element, turn it to miracle and append element to the page. */
var p = document.createElement("p");
p.className = 'miracle';
document.body.appendChild(p);
/* Initialize Miracle plugin. But firstly check it's avaliability. */
M ? M.init() : {};Imagine you have a Meteor template with miracles:
/* Meteor can say when template elements are rendered. */
Template.myTemplate.rendered = function ( ) {
M ? M.init() : {};
}When you trigger Miracle initialization plugin doesn't touch already parsed miracles. It's convenient and efficient.
Plugin uses hardware accelerated CSS declarations that should please you with silky smooth 60fps animations.
Supported: Modern webkit browsers, Firefox, IE 11.
In other browsers page loads as usual, without any special appearance.
- Plugin occupies
Mglobal variable.