From 9b18567c72869db987a76d14a6d3a8834e22c441 Mon Sep 17 00:00:00 2001 From: micku7zu Date: Mon, 30 Jan 2017 21:33:56 +0200 Subject: [PATCH] Add reverse option --- README.md | 1 + dist/vanilla-tilt.babel.js | 9 ++++++--- dist/vanilla-tilt.babel.min.js | 2 +- dist/vanilla-tilt.js | 9 ++++++--- dist/vanilla-tilt.min.js | 2 +- package.json | 2 +- src/vanilla-tilt.js | 9 ++++++--- 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bcc5e7e..5bb3072 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ A smooth 3D tilt javascript library forked from [Tilt.js (jQuery version)](https ### Options ```js { + reverse: false, // reverse the tilt direction max: 35, // max tilt rotation (degrees) perspective: 1000, // Transform perspective, the lower the more extreme the tilt gets. scale: 1, // 2 = 200%, 1.5 = 150%, etc.. diff --git a/dist/vanilla-tilt.babel.js b/dist/vanilla-tilt.babel.js index 0a0fd5e..83275f2 100644 --- a/dist/vanilla-tilt.babel.js +++ b/dist/vanilla-tilt.babel.js @@ -11,7 +11,7 @@ var classCallCheck = function (instance, Constructor) { * Created by Șandor Sergiu (micku7zu) on 1/27/2017. * Original idea: https://github.com/gijsroge/tilt.js * MIT License. - * Version 1.1.0 + * Version 1.2.0 */ var VanillaTilt = function () { @@ -35,6 +35,8 @@ var VanillaTilt = function () { this.element = element; this.settings = this.extendSettings(settings); + this.reverse = this.settings.reverse ? -1 : 1; + this.addEventListeners(); } @@ -105,8 +107,8 @@ var VanillaTilt = function () { x = Math.min(Math.max(x, 0), 1); y = Math.min(Math.max(y, 0), 1); - var tiltX = (this.settings.max / 2 - x * this.settings.max).toFixed(2); - var tiltY = (y * this.settings.max - this.settings.max / 2).toFixed(2); + var tiltX = (this.reverse * (this.settings.max / 2 - x * this.settings.max)).toFixed(2); + var tiltY = (this.reverse * (y * this.settings.max - this.settings.max / 2)).toFixed(2); return { tiltX: tiltX, @@ -149,6 +151,7 @@ var VanillaTilt = function () { VanillaTilt.prototype.extendSettings = function extendSettings(settings) { var defaultSettings = { + reverse: false, max: 35, perspective: 1000, easing: "cubic-bezier(.03,.98,.52,.99)", diff --git a/dist/vanilla-tilt.babel.min.js b/dist/vanilla-tilt.babel.min.js index 7b196f3..1556c2f 100644 --- a/dist/vanilla-tilt.babel.min.js +++ b/dist/vanilla-tilt.babel.min.js @@ -1 +1 @@ -var VanillaTilt=function(){"use strict";var t=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},VanillaTilt=function(){function VanillaTilt(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(t(this,VanillaTilt),!(e instanceof Node))throw"Can't initialize VanillaTilt because "+e+" is not a Node.";this.width=null,this.height=null,this.left=null,this.top=null,this.transitionTimeout=null,this.updateCall=null,this.updateBind=this.update.bind(this),this.element=e,this.settings=this.extendSettings(i),this.addEventListeners()}return VanillaTilt.prototype.addEventListeners=function(){this.onMouseEnterBind=this.onMouseEnter.bind(this),this.onMouseMoveBind=this.onMouseMove.bind(this),this.onMouseLeaveBind=this.onMouseLeave.bind(this),this.element.addEventListener("mouseenter",this.onMouseEnterBind),this.element.addEventListener("mousemove",this.onMouseMoveBind),this.element.addEventListener("mouseleave",this.onMouseLeaveBind)},VanillaTilt.prototype.removeEventListeners=function(){this.element.removeEventListener("mouseenter",this.onMouseEnterBind),this.element.removeEventListener("mousemove",this.onMouseMoveBind),this.element.removeEventListener("mouseleave",this.onMouseLeaveBind)},VanillaTilt.prototype.destroy=function(){this.removeEventListeners(),this.element.vanillaTilt=null,delete this.element.vanillaTilt,this.element=null},VanillaTilt.prototype.onMouseEnter=function(t){this.updateElementPosition(),this.element.style.willChange="transform",this.setTransition()},VanillaTilt.prototype.onMouseMove=function(t){null!==this.updateCall&&cancelAnimationFrame(this.updateCall),this.event=t,this.updateCall=requestAnimationFrame(this.updateBind)},VanillaTilt.prototype.onMouseLeave=function(t){this.setTransition(),this.settings.reset&&this.reset()},VanillaTilt.prototype.reset=function(){var t=this;requestAnimationFrame(function(){t.event={pageX:t.left+t.width/2,pageY:t.top+t.height/2},t.element.style.transform="perspective("+t.settings.perspective+"px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)"})},VanillaTilt.prototype.getValues=function(){var t=(this.event.clientX-this.left)/this.width,e=(this.event.clientY-this.top)/this.height;t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1);var i=(this.settings.max/2-t*this.settings.max).toFixed(2),n=(e*this.settings.max-this.settings.max/2).toFixed(2);return{tiltX:i,tiltY:n,percentageX:100*t,percentageY:100*e}},VanillaTilt.prototype.updateElementPosition=function(){var t=this.element.getBoundingClientRect();this.width=this.element.offsetWidth,this.height=this.element.offsetHeight,this.left=t.left,this.top=t.top},VanillaTilt.prototype.update=function(){var t=this.getValues();this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX("+("x"===this.settings.axis?0:t.tiltY)+"deg) rotateY("+("y"===this.settings.axis?0:t.tiltX)+"deg) scale3d("+this.settings.scale+", "+this.settings.scale+", "+this.settings.scale+")",this.element.dispatchEvent(new CustomEvent("tiltChange",{detail:t})),this.updateCall=null},VanillaTilt.prototype.setTransition=function(){var t=this;clearTimeout(this.transitionTimeout),this.element.style.transition=this.settings.speed+"ms "+this.settings.easing,this.transitionTimeout=setTimeout(function(){return t.element.style.transition=""},this.settings.speed)},VanillaTilt.prototype.extendSettings=function(t){var e={max:35,perspective:1e3,easing:"cubic-bezier(.03,.98,.52,.99)",scale:"1",speed:"300",transition:!0,axis:null,reset:!0},i={};for(var n in e)if(n in t)i[n]=t[n];else if(this.element.hasAttribute("data-tilt-"+n)){var s=this.element.getAttribute("data-tilt-"+n);try{i[n]=JSON.parse(s)}catch(t){i[n]=s}}else i[n]=e[n];return i},VanillaTilt.init=function(t,e){t instanceof Node&&(t=[t]),t instanceof NodeList&&(t=[].slice.call(t)),t instanceof Array&&t.forEach(function(t){"vanillaTilt"in t||(t.vanillaTilt=new VanillaTilt(t,e))})},VanillaTilt}();return"undefined"!=typeof document&&(window.VanillaTilt=VanillaTilt,VanillaTilt.init(document.querySelectorAll("[data-tilt]"))),VanillaTilt}(); \ No newline at end of file +var VanillaTilt=function(){"use strict";var t=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},VanillaTilt=function(){function VanillaTilt(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(t(this,VanillaTilt),!(e instanceof Node))throw"Can't initialize VanillaTilt because "+e+" is not a Node.";this.width=null,this.height=null,this.left=null,this.top=null,this.transitionTimeout=null,this.updateCall=null,this.updateBind=this.update.bind(this),this.element=e,this.settings=this.extendSettings(i),this.reverse=this.settings.reverse?-1:1,this.addEventListeners()}return VanillaTilt.prototype.addEventListeners=function(){this.onMouseEnterBind=this.onMouseEnter.bind(this),this.onMouseMoveBind=this.onMouseMove.bind(this),this.onMouseLeaveBind=this.onMouseLeave.bind(this),this.element.addEventListener("mouseenter",this.onMouseEnterBind),this.element.addEventListener("mousemove",this.onMouseMoveBind),this.element.addEventListener("mouseleave",this.onMouseLeaveBind)},VanillaTilt.prototype.removeEventListeners=function(){this.element.removeEventListener("mouseenter",this.onMouseEnterBind),this.element.removeEventListener("mousemove",this.onMouseMoveBind),this.element.removeEventListener("mouseleave",this.onMouseLeaveBind)},VanillaTilt.prototype.destroy=function(){this.removeEventListeners(),this.element.vanillaTilt=null,delete this.element.vanillaTilt,this.element=null},VanillaTilt.prototype.onMouseEnter=function(t){this.updateElementPosition(),this.element.style.willChange="transform",this.setTransition()},VanillaTilt.prototype.onMouseMove=function(t){null!==this.updateCall&&cancelAnimationFrame(this.updateCall),this.event=t,this.updateCall=requestAnimationFrame(this.updateBind)},VanillaTilt.prototype.onMouseLeave=function(t){this.setTransition(),this.settings.reset&&this.reset()},VanillaTilt.prototype.reset=function(){var t=this;requestAnimationFrame(function(){t.event={pageX:t.left+t.width/2,pageY:t.top+t.height/2},t.element.style.transform="perspective("+t.settings.perspective+"px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)"})},VanillaTilt.prototype.getValues=function(){var t=(this.event.clientX-this.left)/this.width,e=(this.event.clientY-this.top)/this.height;t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1);var i=(this.reverse*(this.settings.max/2-t*this.settings.max)).toFixed(2),n=(this.reverse*(e*this.settings.max-this.settings.max/2)).toFixed(2);return{tiltX:i,tiltY:n,percentageX:100*t,percentageY:100*e}},VanillaTilt.prototype.updateElementPosition=function(){var t=this.element.getBoundingClientRect();this.width=this.element.offsetWidth,this.height=this.element.offsetHeight,this.left=t.left,this.top=t.top},VanillaTilt.prototype.update=function(){var t=this.getValues();this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX("+("x"===this.settings.axis?0:t.tiltY)+"deg) rotateY("+("y"===this.settings.axis?0:t.tiltX)+"deg) scale3d("+this.settings.scale+", "+this.settings.scale+", "+this.settings.scale+")",this.element.dispatchEvent(new CustomEvent("tiltChange",{detail:t})),this.updateCall=null},VanillaTilt.prototype.setTransition=function(){var t=this;clearTimeout(this.transitionTimeout),this.element.style.transition=this.settings.speed+"ms "+this.settings.easing,this.transitionTimeout=setTimeout(function(){return t.element.style.transition=""},this.settings.speed)},VanillaTilt.prototype.extendSettings=function(t){var e={reverse:!1,max:35,perspective:1e3,easing:"cubic-bezier(.03,.98,.52,.99)",scale:"1",speed:"300",transition:!0,axis:null,reset:!0},i={};for(var n in e)if(n in t)i[n]=t[n];else if(this.element.hasAttribute("data-tilt-"+n)){var s=this.element.getAttribute("data-tilt-"+n);try{i[n]=JSON.parse(s)}catch(t){i[n]=s}}else i[n]=e[n];return i},VanillaTilt.init=function(t,e){t instanceof Node&&(t=[t]),t instanceof NodeList&&(t=[].slice.call(t)),t instanceof Array&&t.forEach(function(t){"vanillaTilt"in t||(t.vanillaTilt=new VanillaTilt(t,e))})},VanillaTilt}();return"undefined"!=typeof document&&(window.VanillaTilt=VanillaTilt,VanillaTilt.init(document.querySelectorAll("[data-tilt]"))),VanillaTilt}(); \ No newline at end of file diff --git a/dist/vanilla-tilt.js b/dist/vanilla-tilt.js index 6b84c8d..cfd939e 100644 --- a/dist/vanilla-tilt.js +++ b/dist/vanilla-tilt.js @@ -5,7 +5,7 @@ var VanillaTilt = (function () { * Created by Șandor Sergiu (micku7zu) on 1/27/2017. * Original idea: https://github.com/gijsroge/tilt.js * MIT License. - * Version 1.1.0 + * Version 1.2.0 */ class VanillaTilt { @@ -26,6 +26,8 @@ class VanillaTilt { this.element = element; this.settings = this.extendSettings(settings); + this.reverse = this.settings.reverse ? -1 : 1; + this.addEventListeners(); } @@ -97,8 +99,8 @@ class VanillaTilt { x = Math.min(Math.max(x, 0), 1); y = Math.min(Math.max(y, 0), 1); - let tiltX = (this.settings.max / 2 - x * this.settings.max).toFixed(2); - let tiltY = (y * this.settings.max - this.settings.max / 2).toFixed(2); + let tiltX = (this.reverse * (this.settings.max / 2 - x * this.settings.max)).toFixed(2); + let tiltY = (this.reverse * (y * this.settings.max - this.settings.max / 2)).toFixed(2); return { tiltX: tiltX, @@ -141,6 +143,7 @@ class VanillaTilt { extendSettings(settings) { let defaultSettings = { + reverse: false, max: 35, perspective: 1000, easing: "cubic-bezier(.03,.98,.52,.99)", diff --git a/dist/vanilla-tilt.min.js b/dist/vanilla-tilt.min.js index d303e36..ffe3db2 100644 --- a/dist/vanilla-tilt.min.js +++ b/dist/vanilla-tilt.min.js @@ -1 +1 @@ -var VanillaTilt=function(){"use strict";class t{constructor(t,e={}){if(!(t instanceof Node))throw"Can't initialize VanillaTilt because "+t+" is not a Node.";this.width=null,this.height=null,this.left=null,this.top=null,this.transitionTimeout=null,this.updateCall=null,this.updateBind=this.update.bind(this),this.element=t,this.settings=this.extendSettings(e),this.addEventListeners()}addEventListeners(){this.onMouseEnterBind=this.onMouseEnter.bind(this),this.onMouseMoveBind=this.onMouseMove.bind(this),this.onMouseLeaveBind=this.onMouseLeave.bind(this),this.element.addEventListener("mouseenter",this.onMouseEnterBind),this.element.addEventListener("mousemove",this.onMouseMoveBind),this.element.addEventListener("mouseleave",this.onMouseLeaveBind)}removeEventListeners(){this.element.removeEventListener("mouseenter",this.onMouseEnterBind),this.element.removeEventListener("mousemove",this.onMouseMoveBind),this.element.removeEventListener("mouseleave",this.onMouseLeaveBind)}destroy(){this.removeEventListeners(),this.element.vanillaTilt=null,delete this.element.vanillaTilt,this.element=null}onMouseEnter(t){this.updateElementPosition(),this.element.style.willChange="transform",this.setTransition()}onMouseMove(t){null!==this.updateCall&&cancelAnimationFrame(this.updateCall),this.event=t,this.updateCall=requestAnimationFrame(this.updateBind)}onMouseLeave(t){this.setTransition(),this.settings.reset&&this.reset()}reset(){requestAnimationFrame(()=>{this.event={pageX:this.left+this.width/2,pageY:this.top+this.height/2},this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)"})}getValues(){let t=(this.event.clientX-this.left)/this.width,e=(this.event.clientY-this.top)/this.height;t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1);let i=(this.settings.max/2-t*this.settings.max).toFixed(2),s=(e*this.settings.max-this.settings.max/2).toFixed(2);return{tiltX:i,tiltY:s,percentageX:100*t,percentageY:100*e}}updateElementPosition(){let t=this.element.getBoundingClientRect();this.width=this.element.offsetWidth,this.height=this.element.offsetHeight,this.left=t.left,this.top=t.top}update(){let t=this.getValues();this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX("+("x"===this.settings.axis?0:t.tiltY)+"deg) rotateY("+("y"===this.settings.axis?0:t.tiltX)+"deg) scale3d("+this.settings.scale+", "+this.settings.scale+", "+this.settings.scale+")",this.element.dispatchEvent(new CustomEvent("tiltChange",{detail:t})),this.updateCall=null}setTransition(){clearTimeout(this.transitionTimeout),this.element.style.transition=this.settings.speed+"ms "+this.settings.easing,this.transitionTimeout=setTimeout(()=>this.element.style.transition="",this.settings.speed)}extendSettings(t){let e={max:35,perspective:1e3,easing:"cubic-bezier(.03,.98,.52,.99)",scale:"1",speed:"300",transition:!0,axis:null,reset:!0},i={};for(var s in e)if(s in t)i[s]=t[s];else if(this.element.hasAttribute("data-tilt-"+s)){let t=this.element.getAttribute("data-tilt-"+s);try{i[s]=JSON.parse(t)}catch(e){i[s]=t}}else i[s]=e[s];return i}static init(e,i){e instanceof Node&&(e=[e]),e instanceof NodeList&&(e=[].slice.call(e)),e instanceof Array&&e.forEach(e=>{"vanillaTilt"in e||(e.vanillaTilt=new t(e,i))})}}return"undefined"!=typeof document&&(window.VanillaTilt=t,t.init(document.querySelectorAll("[data-tilt]"))),t}(); \ No newline at end of file +var VanillaTilt=function(){"use strict";class t{constructor(t,e={}){if(!(t instanceof Node))throw"Can't initialize VanillaTilt because "+t+" is not a Node.";this.width=null,this.height=null,this.left=null,this.top=null,this.transitionTimeout=null,this.updateCall=null,this.updateBind=this.update.bind(this),this.element=t,this.settings=this.extendSettings(e),this.reverse=this.settings.reverse?-1:1,this.addEventListeners()}addEventListeners(){this.onMouseEnterBind=this.onMouseEnter.bind(this),this.onMouseMoveBind=this.onMouseMove.bind(this),this.onMouseLeaveBind=this.onMouseLeave.bind(this),this.element.addEventListener("mouseenter",this.onMouseEnterBind),this.element.addEventListener("mousemove",this.onMouseMoveBind),this.element.addEventListener("mouseleave",this.onMouseLeaveBind)}removeEventListeners(){this.element.removeEventListener("mouseenter",this.onMouseEnterBind),this.element.removeEventListener("mousemove",this.onMouseMoveBind),this.element.removeEventListener("mouseleave",this.onMouseLeaveBind)}destroy(){this.removeEventListeners(),this.element.vanillaTilt=null,delete this.element.vanillaTilt,this.element=null}onMouseEnter(t){this.updateElementPosition(),this.element.style.willChange="transform",this.setTransition()}onMouseMove(t){null!==this.updateCall&&cancelAnimationFrame(this.updateCall),this.event=t,this.updateCall=requestAnimationFrame(this.updateBind)}onMouseLeave(t){this.setTransition(),this.settings.reset&&this.reset()}reset(){requestAnimationFrame(()=>{this.event={pageX:this.left+this.width/2,pageY:this.top+this.height/2},this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)"})}getValues(){let t=(this.event.clientX-this.left)/this.width,e=(this.event.clientY-this.top)/this.height;t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1);let i=(this.reverse*(this.settings.max/2-t*this.settings.max)).toFixed(2),s=(this.reverse*(e*this.settings.max-this.settings.max/2)).toFixed(2);return{tiltX:i,tiltY:s,percentageX:100*t,percentageY:100*e}}updateElementPosition(){let t=this.element.getBoundingClientRect();this.width=this.element.offsetWidth,this.height=this.element.offsetHeight,this.left=t.left,this.top=t.top}update(){let t=this.getValues();this.element.style.transform="perspective("+this.settings.perspective+"px) rotateX("+("x"===this.settings.axis?0:t.tiltY)+"deg) rotateY("+("y"===this.settings.axis?0:t.tiltX)+"deg) scale3d("+this.settings.scale+", "+this.settings.scale+", "+this.settings.scale+")",this.element.dispatchEvent(new CustomEvent("tiltChange",{detail:t})),this.updateCall=null}setTransition(){clearTimeout(this.transitionTimeout),this.element.style.transition=this.settings.speed+"ms "+this.settings.easing,this.transitionTimeout=setTimeout(()=>this.element.style.transition="",this.settings.speed)}extendSettings(t){let e={reverse:!1,max:35,perspective:1e3,easing:"cubic-bezier(.03,.98,.52,.99)",scale:"1",speed:"300",transition:!0,axis:null,reset:!0},i={};for(var s in e)if(s in t)i[s]=t[s];else if(this.element.hasAttribute("data-tilt-"+s)){let t=this.element.getAttribute("data-tilt-"+s);try{i[s]=JSON.parse(t)}catch(e){i[s]=t}}else i[s]=e[s];return i}static init(e,i){e instanceof Node&&(e=[e]),e instanceof NodeList&&(e=[].slice.call(e)),e instanceof Array&&e.forEach(e=>{"vanillaTilt"in e||(e.vanillaTilt=new t(e,i))})}}return"undefined"!=typeof document&&(window.VanillaTilt=t,t.init(document.querySelectorAll("[data-tilt]"))),t}(); \ No newline at end of file diff --git a/package.json b/package.json index e61c4bf..06945c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vanilla-tilt", - "version": "1.1.0", + "version": "1.2.0", "description": "A smooth 3D tilt javascript library forked from Tilt.js", "main": "lib/vanilla-tilt.js", "dist": "dist/vanilla-tilt.js", diff --git a/src/vanilla-tilt.js b/src/vanilla-tilt.js index c6cb5e0..7130828 100644 --- a/src/vanilla-tilt.js +++ b/src/vanilla-tilt.js @@ -2,7 +2,7 @@ * Created by Șandor Sergiu (micku7zu) on 1/27/2017. * Original idea: https://github.com/gijsroge/tilt.js * MIT License. - * Version 1.1.0 + * Version 1.2.0 */ export default class VanillaTilt { @@ -23,6 +23,8 @@ export default class VanillaTilt { this.element = element; this.settings = this.extendSettings(settings); + this.reverse = this.settings.reverse ? -1 : 1; + this.addEventListeners(); } @@ -94,8 +96,8 @@ export default class VanillaTilt { x = Math.min(Math.max(x, 0), 1); y = Math.min(Math.max(y, 0), 1); - let tiltX = (this.settings.max / 2 - x * this.settings.max).toFixed(2); - let tiltY = (y * this.settings.max - this.settings.max / 2).toFixed(2); + let tiltX = (this.reverse * (this.settings.max / 2 - x * this.settings.max)).toFixed(2); + let tiltY = (this.reverse * (y * this.settings.max - this.settings.max / 2)).toFixed(2); return { tiltX: tiltX, @@ -138,6 +140,7 @@ export default class VanillaTilt { extendSettings(settings) { let defaultSettings = { + reverse: false, max: 35, perspective: 1000, easing: "cubic-bezier(.03,.98,.52,.99)",