Home

Awesome

universal-tilt.js

NPM version NPM downloads

About

JavaScript & jQuery elements movement library based on:

Tilt.js by Gijs Rogé and vanilla-tilt.js by Șandor Sergiu

universal-tilt.js contains additional functions for mobile devices with gyroscope, new Position Base option and more!

Demo

See plugin in action

React plugin

If you use React, install component with the implementation of the universal-tilt.js library! More here

How to Install

First, install the library in your project by npm:

$ npm install universal-tilt.js

Or Yarn:

$ yarn add universal-tilt.js

You can also connect script via one of CDNs:<br> bundle.run: https://bundle.run/universal-tilt.js<br> jsDelivr: https://cdn.jsdelivr.net/npm/universal-tilt.js/<br> unpkg: https://unpkg.com/universal-tilt.js/

Getting Started

Connect libary with project using script tag in HTML:

<script src="/path/to/universal-tilt.js"></script>

ES6 import:

import UniversalTilt from 'universal-tilt.js';

Or CommonJS:

const UniversalTilt = require('universal-tilt.js');

Next use library with:

• Vanilla JavaScript e.g:

const elems = document.querySelectorAll('.tilt');

const tilt = UniversalTilt.init({
  elements: elems,
  settings: {
    // options...
  },
  callbacks: {
    // callbacks...
  }
});

• or jQuery e.g:

Connect jQuery in HTML

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

Or include via command line and CommonJS

$ npm install jquery
$ yarn add jquery
$ bower install jquery
const jQuery = require('jquery');

And call plugin on element

$('.tilt').universalTilt({
  settings: {
    // options...
  },
  callbacks: {
    // callbacks...
  }
});

• Plugin supports autoinit

To use it, add data-tilt to html element e.g:

<div data-tilt></div>

Methods

• Destroy method

elems.universalTilt.destroy();

• Get values method

elems.universalTilt.getValues();

• Reset method

elems.universalTilt.reset();

Options

Settings

NameTypeDefaultDescriptionAvailable options
basestringelementThe surface from which the location of the mouse is capturedelement or window
disabledstringnullDisable axisx or y
easingstringcubic-bezier(.03, .98, .52, .99)Transition easingcubic-bezier/ease/linear/etc.
excludeRegExpnullDisable tilt effect on selected user agentse.g: <code>/(Firefox|iPad)/</code>
maxnumber35Max tilt valuee.g: 28
perspectivenumber1000Tilt effect perspectivee.g: 700
resetbooleantrueEnable/disable element position reset after mouseouttrue (enable), false (disable)
reversebooleanfalseReverse tilt effect directorytrue (reverse directory), false (standard directory)
scalenumber1.0Element scale on mouseover0.9/1.3/etc.
shinebooleanfalseAdd/remove shine effect on mouseovertrue (add), false (remove)
shine-opacity<sup>1</sup>number0Add/remove shine effect on mouseovervalues >= 0 and <= 1
shine-save<sup>1</sup>booleanfalseSave/reset shine effect on mouseouttrue (save), false (reset)
speednumber300Transition speed (ms)e.g: 500

Callbacks

NameDescriptionAvailable options
onDestroyCallback on plugin destroyel => { /* code */ }
onDeviceMove<sup>2</sup>Callback on device moveel => { /* code */ }
onInitCallback on plugin initel => { /* code */ }
onMouseEnterCallback on mouse enterel => { /* code */ }
onMouseLeaveCallback on mouse leaveel => { /* code */ }
onMouseMoveCallback on mouse moveel => { /* code */ }

<sup>1</sup> shine value must be true<br> <sup>2</sup> only for devices supported device motion

Event

tiltChange event will output the x, y & angle of tilting

License

This project is licensed under the MIT License © 2018-present Jakub Biesiada