Rotate an element depending on the click (or touch) location. Pure vanilla JS! Yay!
###Demos
- Include the
cbrotation.jsorcbrotation.min.jsfile (usually at the bottom of thebody) - Add the
rot-dirclass to any element(s) you want to have the effect - (optional) Set any of the data-attributes listed below to valid, custom values
- You may want to also set
transform-style: preserve-3d(with other prefixes) on therot-direlement as well, depending on how you want it to feel
Side note: If you want the elements to rotate on mouse move instead, you might look at this project
rot-type - Possible values: realistic (default) and simple - Demo page
realisticbases the effect on a calculation of distance from the centersimplehas 1 of 5 preset effects based on where is clicked
rot-origin - The transform origin for the rotation occurring - defaults to "center center" - Demo page
- Accepts all default CSS
transform-originvalues using the same syntax - Only applies to
simpletype. Also accepts a value ofoppositewhich will set thetransform-originto the opposite of the rotation direction, creating a rotating door effect
rot-dur - The duration, in seconds, of the total transition being applied - defaults to .1
rot-ease - The easing function applied to the transition, all CSS easing values allowed - defaults to ease-out
rot-perspective - Sets the perspective for the element's rotation - defaults to 400
rot-amount - The amount to turn each click - defaults to 15
- When using it with
simple, it is directly the number of degrees to rotate each click - When using it with
realistic, it affects the value by the following
:
myTrans = "translateZ(" + (-(maxD - d) / 8) + "px) " +
"rotateY(" + -dX / 8 * amount / 15 + "deg) " +
"rotateX(" + dY / 4 * amount / 15 + "deg)";
rot-init - The transform that the element goes to on start and after a click - defaults to rotate(0deg)
rot-start - A function to call when the element is clicked - demo page
rot-end - A function to call when the element is done being clicked
This will apply the effect using all of the default data values:
<div class="rot-dir">This is a minimal example</div>
This is an example using every attribute:
<div class="rot-dir" data-rot-type="simple"
data-rot-origin="opposite"
data-rot-dur=".2"
data-rot-ease="ease-in-out"
data-rot-perspective="800"
data-rot-amount="10"
data-rot-init="rotate3d(1,1,0,10deg)"
data-rot-start="atStart"
data-rot-end="atEnd"
>This has every attribute used</div>
<div class="rot-dir" data-rot-type="simple" data-rot-origin="opposite">I'm a Windows 8 tile</div>