vanilla-tilt.js

A smooth 3D tilt javascript library.

README

vanilla-tilt.js

npm version

A smooth 3D tilt javascript library forked from Tilt.js (jQuery version).


Usage


  1. ``` html
  2. <body>
  3.   
  4. <div class="your-element" data-tilt></div>
  5. <script type="text/javascript" src="vanilla-tilt.js"></script>
  6. </body>
  7. ```

If you want to use this library in IE, you need to include a CustomEvent polyfill: https://github.com/micku7zu/vanilla-tilt.js/issues/49#issuecomment-482711876 or maybe consider the jQuery version.

Options

  1. ``` js
  2. {
  3.     reverse:                false,  // reverse the tilt direction
  4.     max:                    15,     // max tilt rotation (degrees)
  5.     startX:                 0,      // the starting tilt on the X axis, in degrees.
  6.     startY:                 0,      // the starting tilt on the Y axis, in degrees.
  7.     perspective:            1000,   // Transform perspective, the lower the more extreme the tilt gets.
  8.     scale:                  1,      // 2 = 200%, 1.5 = 150%, etc..
  9.     speed:                  300,    // Speed of the enter/exit transition
  10.     transition:             true,   // Set a transition on enter/exit.
  11.     axis:                   null,   // What axis should be enabled. Can be "x" or "y".
  12.     reset:                  true,   // If the tilt effect has to be reset on exit.
  13.     easing:                 "cubic-bezier(.03,.98,.52,.99)",    // Easing on enter/exit.
  14.     glare:                  false,  // if it should have a "glare" effect
  15.     "max-glare":            1,      // the maximum "glare" opacity (1 = 100%, 0.5 = 50%)
  16.     "glare-prerender":      false,  // false = VanillaTilt creates the glare elements for you, otherwise
  17.                                     // you need to add .js-tilt-glare>.js-tilt-glare-inner by yourself
  18.     "mouse-event-element":  null,   // css-selector or link to an HTML-element that will be listening to mouse events
  19.     "full-page-listening":  false,  // If true, parallax effect will listen to mouse move events on the whole document, not only the selected element
  20.     gyroscope:              true,   // Boolean to enable/disable device orientation detection,
  21.     gyroscopeMinAngleX:     -45,    // This is the bottom limit of the device angle on X axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the left border of the element;
  22.     gyroscopeMaxAngleX:     45,     // This is the top limit of the device angle on X axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the right border of the element;
  23.     gyroscopeMinAngleY:     -45,    // This is the bottom limit of the device angle on Y axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the top border of the element;
  24.     gyroscopeMaxAngleY:     45,     // This is the top limit of the device angle on Y axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the bottom border of the element;
  25.     gyroscopeSamples:       10      // How many gyroscope moves to decide the starting position.
  26. }
  27. ```

Events

  1. ``` js
  2. const element = document.querySelector(".js-tilt");
  3. VanillaTilt.init(element);
  4. element.addEventListener("tiltChange", callback);
  5. ```

Methods

  1. ``` js
  2. const element = document.querySelector(".js-tilt");
  3. VanillaTilt.init(element);

  4. // Destroy instance
  5. element.vanillaTilt.destroy();

  6. // Get values of instance
  7. element.vanillaTilt.getValues();

  8. // Reset instance
  9. element.vanillaTilt.reset();

  10. // It also supports NodeList
  11. const elements = document.querySelectorAll(".js-tilt");
  12. VanillaTilt.init(elements);
  13. ```

Install

You can copy and include any of the following file:


NPM


Also available on npm https://www.npmjs.com/package/vanilla-tilt

  1. ```
  2. npm install vanilla-tilt
  3. ```

Import it using

  1. ```
  2. import VanillaTilt from 'vanilla-tilt';
  3. ```

Known issues


Credits


Original library: Tilt.js

Original library author: Gijs Rogé

Contributors


- [Livio Brunner](https://github.com/BrunnerLivio) <contact@brunnerliv.io> (Typings & Glare Effect)
- Matteo Rigon (Device orientation support)
- Corey Austin (Initial gyroscope position)
- rrroyal (Whole document mouse events listening)

Other projects


Play Store link: https://play.google.com/store/apps/details?id=com.quickcursor

Buy me a beer 🍻

If you want to thank me for vanilla-tilt.js or Quick Cursor Android app, you can donate on PayPal: https://www.paypal.me/micku7zu?locale.x=en_US

License


MIT License