mirror of
https://github.com/micku7zu/vanilla-tilt.js
synced 2024-06-08 16:52:13 +02:00
Build vanilla-tilt
This commit is contained in:
parent
dbfd1dfe5c
commit
04a1a4dd84
92
dist/vanilla-tilt.babel.js
vendored
92
dist/vanilla-tilt.babel.js
vendored
|
@ -36,24 +36,35 @@ var VanillaTilt = function () {
|
|||
this.settings = this.extendSettings(settings);
|
||||
|
||||
this.reverse = this.settings.reverse ? -1 : 1;
|
||||
// If [data-tilt-glare] or [data-tilt-glare="true"] or [data-tilt-glare="1"]
|
||||
this.glare = this.isSettingTrue(this.settings.glare);
|
||||
this.glarePrerender = this.isSettingTrue(this.settings["glare-prerender"]);
|
||||
|
||||
if (this.glare) this.prepareGlare();
|
||||
this.addEventListeners();
|
||||
}
|
||||
|
||||
VanillaTilt.prototype.isSettingTrue = function isSettingTrue(setting) {
|
||||
return setting === "" || setting === true || setting === 1;
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.addEventListeners = function addEventListeners() {
|
||||
this.onMouseEnterBind = this.onMouseEnter.bind(this);
|
||||
this.onMouseMoveBind = this.onMouseMove.bind(this);
|
||||
this.onMouseLeaveBind = this.onMouseLeave.bind(this);
|
||||
this.onWindowResizeBind = this.onWindowResizeBind.bind(this);
|
||||
|
||||
this.element.addEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.addEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.addEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.addEventListener("resize", this.onWindowResizeBind);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.removeEventListeners = function removeEventListeners() {
|
||||
this.element.removeEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.removeEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.removeEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.removeEventListener("resize", this.onWindowResizeBind);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.destroy = function destroy() {
|
||||
|
@ -98,6 +109,10 @@ var VanillaTilt = function () {
|
|||
|
||||
_this.element.style.transform = "perspective(" + _this.settings.perspective + "px) " + "rotateX(0deg) " + "rotateY(0deg) " + "scale3d(1, 1, 1)";
|
||||
});
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = "rotate(180deg) translate(-50%, -50%)";
|
||||
this.glareElement.style.opacity = "0";
|
||||
}
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.getValues = function getValues() {
|
||||
|
@ -132,6 +147,11 @@ var VanillaTilt = function () {
|
|||
|
||||
this.element.style.transform = "perspective(" + this.settings.perspective + "px) " + "rotateX(" + (this.settings.axis === "x" ? 0 : values.tiltY) + "deg) " + "rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " + "scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")";
|
||||
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = "rotate(" + values.angle + "deg) translate(-50%, -50%)";
|
||||
this.glareElement.style.opacity = "" + values.percentageY * this.settings["max-glare"] / 100;
|
||||
}
|
||||
|
||||
this.element.dispatchEvent(new CustomEvent("tiltChange", {
|
||||
"detail": values
|
||||
}));
|
||||
|
@ -139,13 +159,79 @@ var VanillaTilt = function () {
|
|||
this.updateCall = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends the glare element (if glarePrerender equals false)
|
||||
* and sets the default style
|
||||
*/
|
||||
|
||||
|
||||
VanillaTilt.prototype.prepareGlare = function prepareGlare() {
|
||||
// If option pre-render is enabled we assume all html/css is present for an optimal glare effect.
|
||||
if (!this.glarePrerender) {
|
||||
// Create glare element
|
||||
var jsTiltGlare = document.createElement("div");
|
||||
jsTiltGlare.classList.add("js-tilt-glare");
|
||||
|
||||
var jsTiltGlareInner = document.createElement("div");
|
||||
jsTiltGlareInner.classList.add("js-tilt-glare-inner");
|
||||
|
||||
jsTiltGlare.appendChild(jsTiltGlareInner);
|
||||
this.element.appendChild(jsTiltGlare);
|
||||
}
|
||||
|
||||
this.glareElementWrapper = this.element.querySelector(".js-tilt-glare");
|
||||
this.glareElement = this.element.querySelector(".js-tilt-glare-inner");
|
||||
|
||||
if (this.glarePrerender) return;
|
||||
|
||||
var glareElementWrapperStyle = {
|
||||
"position": "absolute",
|
||||
"top": "0",
|
||||
"left": "0",
|
||||
"width": "100%",
|
||||
"height": "100%",
|
||||
"overflow": "hidden"
|
||||
};
|
||||
|
||||
Object.assign(this.glareElementWrapper.style, glareElementWrapperStyle);
|
||||
|
||||
var glareElementStyle = {
|
||||
'position': 'absolute',
|
||||
'top': '50%',
|
||||
'left': '50%',
|
||||
'pointer-events': 'none',
|
||||
'background-image': "linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)",
|
||||
'width': this.element.offsetWidth * 2 + "px",
|
||||
'height': this.element.offsetWidth * 2 + "px",
|
||||
'transform': 'rotate(180deg) translate(-50%, -50%)',
|
||||
'transform-origin': '0% 0%',
|
||||
'opacity': '0'
|
||||
};
|
||||
|
||||
Object.assign(this.glareElement.style, glareElementStyle);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.updateGlareSize = function updateGlareSize() {
|
||||
Object.assign(this.glareElement.style, {
|
||||
'width': "" + this.element.offsetWidth * 2,
|
||||
'height': "" + this.element.offsetWidth * 2
|
||||
});
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.onWindowResizeBind = function onWindowResizeBind() {
|
||||
this.updateGlareSize();
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.setTransition = function setTransition() {
|
||||
var _this2 = this;
|
||||
|
||||
clearTimeout(this.transitionTimeout);
|
||||
this.element.style.transition = this.settings.speed + "ms " + this.settings.easing;
|
||||
if (this.glare) this.glareElement.style.transition = "opacity " + this.settings.speed + "ms " + this.settings.easing;
|
||||
|
||||
this.transitionTimeout = setTimeout(function () {
|
||||
return _this2.element.style.transition = "";
|
||||
_this2.element.style.transition = "";
|
||||
if (_this2.glare) _this2.glareElement.style.transition = "";
|
||||
}, this.settings.speed);
|
||||
};
|
||||
|
||||
|
@ -159,11 +245,13 @@ var VanillaTilt = function () {
|
|||
speed: "300",
|
||||
transition: true,
|
||||
axis: null,
|
||||
glare: false,
|
||||
"max-glare": 1,
|
||||
"glare-prerender": false,
|
||||
reset: true
|
||||
};
|
||||
|
||||
var newSettings = {};
|
||||
|
||||
for (var property in defaultSettings) {
|
||||
if (property in settings) {
|
||||
newSettings[property] = settings[property];
|
||||
|
|
2
dist/vanilla-tilt.babel.min.js
vendored
2
dist/vanilla-tilt.babel.min.js
vendored
|
@ -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.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;return t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1),{tiltX:(this.reverse*(this.settings.max/2-t*this.settings.max)).toFixed(2),tiltY:(this.reverse*(e*this.settings.max-this.settings.max/2)).toFixed(2),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}();
|
||||
undefined
|
92
dist/vanilla-tilt.js
vendored
92
dist/vanilla-tilt.js
vendored
|
@ -27,24 +27,35 @@ class VanillaTilt {
|
|||
this.settings = this.extendSettings(settings);
|
||||
|
||||
this.reverse = this.settings.reverse ? -1 : 1;
|
||||
// If [data-tilt-glare] or [data-tilt-glare="true"] or [data-tilt-glare="1"]
|
||||
this.glare = this.isSettingTrue(this.settings.glare);
|
||||
this.glarePrerender = this.isSettingTrue(this.settings["glare-prerender"]);
|
||||
|
||||
if (this.glare) this.prepareGlare();
|
||||
this.addEventListeners();
|
||||
}
|
||||
|
||||
isSettingTrue(setting) {
|
||||
return setting === "" || setting === true || setting === 1;
|
||||
}
|
||||
|
||||
addEventListeners() {
|
||||
this.onMouseEnterBind = this.onMouseEnter.bind(this);
|
||||
this.onMouseMoveBind = this.onMouseMove.bind(this);
|
||||
this.onMouseLeaveBind = this.onMouseLeave.bind(this);
|
||||
this.onWindowResizeBind = this.onWindowResizeBind.bind(this);
|
||||
|
||||
this.element.addEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.addEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.addEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.addEventListener("resize", this.onWindowResizeBind);
|
||||
}
|
||||
|
||||
removeEventListeners() {
|
||||
this.element.removeEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.removeEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.removeEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.removeEventListener("resize", this.onWindowResizeBind);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
@ -90,6 +101,10 @@ class VanillaTilt {
|
|||
"rotateY(0deg) " +
|
||||
"scale3d(1, 1, 1)";
|
||||
});
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = `rotate(180deg) translate(-50%, -50%)`;
|
||||
this.glareElement.style.opacity = `0`;
|
||||
}
|
||||
}
|
||||
|
||||
getValues() {
|
||||
|
@ -127,6 +142,10 @@ class VanillaTilt {
|
|||
"rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " +
|
||||
"scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")";
|
||||
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = `rotate(${values.angle}deg) translate(-50%, -50%)`;
|
||||
this.glareElement.style.opacity = `${values.percentageY * this.settings["max-glare"] / 100}`;
|
||||
}
|
||||
|
||||
this.element.dispatchEvent(new CustomEvent("tiltChange", {
|
||||
"detail": values
|
||||
|
@ -135,10 +154,77 @@ class VanillaTilt {
|
|||
this.updateCall = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the glare element (if glarePrerender equals false)
|
||||
* and sets the default style
|
||||
*/
|
||||
prepareGlare() {
|
||||
// If option pre-render is enabled we assume all html/css is present for an optimal glare effect.
|
||||
if (!this.glarePrerender) {
|
||||
// Create glare element
|
||||
const jsTiltGlare = document.createElement("div");
|
||||
jsTiltGlare.classList.add("js-tilt-glare");
|
||||
|
||||
const jsTiltGlareInner = document.createElement("div");
|
||||
jsTiltGlareInner.classList.add("js-tilt-glare-inner");
|
||||
|
||||
jsTiltGlare.appendChild(jsTiltGlareInner);
|
||||
this.element.appendChild(jsTiltGlare);
|
||||
}
|
||||
|
||||
this.glareElementWrapper = this.element.querySelector(".js-tilt-glare");
|
||||
this.glareElement = this.element.querySelector(".js-tilt-glare-inner");
|
||||
|
||||
if (this.glarePrerender) return;
|
||||
|
||||
const glareElementWrapperStyle = {
|
||||
"position": "absolute",
|
||||
"top": "0",
|
||||
"left": "0",
|
||||
"width": "100%",
|
||||
"height": "100%",
|
||||
"overflow": "hidden"
|
||||
};
|
||||
|
||||
Object.assign(this.glareElementWrapper.style, glareElementWrapperStyle);
|
||||
|
||||
const glareElementStyle = {
|
||||
'position': 'absolute',
|
||||
'top': '50%',
|
||||
'left': '50%',
|
||||
'pointer-events': 'none',
|
||||
'background-image': `linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)`,
|
||||
'width': `${this.element.offsetWidth*2}px`,
|
||||
'height': `${this.element.offsetWidth*2}px`,
|
||||
'transform': 'rotate(180deg) translate(-50%, -50%)',
|
||||
'transform-origin': '0% 0%',
|
||||
'opacity': '0',
|
||||
};
|
||||
|
||||
Object.assign(this.glareElement.style, glareElementStyle);
|
||||
}
|
||||
|
||||
updateGlareSize() {
|
||||
Object.assign(this.glareElement.style, {
|
||||
'width': `${this.element.offsetWidth*2}`,
|
||||
'height': `${this.element.offsetWidth*2}`,
|
||||
});
|
||||
}
|
||||
|
||||
onWindowResizeBind() {
|
||||
this.updateGlareSize();
|
||||
}
|
||||
|
||||
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);
|
||||
if (this.glare) this.glareElement.style.transition = `opacity ${this.settings.speed}ms ${this.settings.easing}`;
|
||||
|
||||
this.transitionTimeout = setTimeout(() => {
|
||||
this.element.style.transition = "";
|
||||
if (this.glare) this.glareElement.style.transition = "";
|
||||
}, this.settings.speed);
|
||||
|
||||
}
|
||||
|
||||
extendSettings(settings) {
|
||||
|
@ -151,11 +237,13 @@ class VanillaTilt {
|
|||
speed: "300",
|
||||
transition: true,
|
||||
axis: null,
|
||||
glare: false,
|
||||
"max-glare": 1,
|
||||
"glare-prerender": false,
|
||||
reset: true
|
||||
};
|
||||
|
||||
let newSettings = {};
|
||||
|
||||
for (var property in defaultSettings) {
|
||||
if (property in settings) {
|
||||
newSettings[property] = settings[property];
|
||||
|
|
2
dist/vanilla-tilt.min.js
vendored
2
dist/vanilla-tilt.min.js
vendored
|
@ -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.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;return t=Math.min(Math.max(t,0),1),e=Math.min(Math.max(e,0),1),{tiltX:(this.reverse*(this.settings.max/2-t*this.settings.max)).toFixed(2),tiltY:(this.reverse*(e*this.settings.max-this.settings.max/2)).toFixed(2),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}();
|
||||
undefined
|
|
@ -24,24 +24,35 @@ class VanillaTilt {
|
|||
this.settings = this.extendSettings(settings);
|
||||
|
||||
this.reverse = this.settings.reverse ? -1 : 1;
|
||||
// If [data-tilt-glare] or [data-tilt-glare="true"] or [data-tilt-glare="1"]
|
||||
this.glare = this.isSettingTrue(this.settings.glare);
|
||||
this.glarePrerender = this.isSettingTrue(this.settings["glare-prerender"]);
|
||||
|
||||
if (this.glare) this.prepareGlare();
|
||||
this.addEventListeners();
|
||||
}
|
||||
|
||||
isSettingTrue(setting) {
|
||||
return setting === "" || setting === true || setting === 1;
|
||||
}
|
||||
|
||||
addEventListeners() {
|
||||
this.onMouseEnterBind = this.onMouseEnter.bind(this);
|
||||
this.onMouseMoveBind = this.onMouseMove.bind(this);
|
||||
this.onMouseLeaveBind = this.onMouseLeave.bind(this);
|
||||
this.onWindowResizeBind = this.onWindowResizeBind.bind(this);
|
||||
|
||||
this.element.addEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.addEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.addEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.addEventListener("resize", this.onWindowResizeBind);
|
||||
}
|
||||
|
||||
removeEventListeners() {
|
||||
this.element.removeEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.removeEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.removeEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.removeEventListener("resize", this.onWindowResizeBind);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
@ -87,6 +98,10 @@ class VanillaTilt {
|
|||
"rotateY(0deg) " +
|
||||
"scale3d(1, 1, 1)";
|
||||
});
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = `rotate(180deg) translate(-50%, -50%)`;
|
||||
this.glareElement.style.opacity = `0`;
|
||||
}
|
||||
}
|
||||
|
||||
getValues() {
|
||||
|
@ -124,6 +139,10 @@ class VanillaTilt {
|
|||
"rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " +
|
||||
"scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")";
|
||||
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = `rotate(${values.angle}deg) translate(-50%, -50%)`;
|
||||
this.glareElement.style.opacity = `${values.percentageY * this.settings["max-glare"] / 100}`;
|
||||
}
|
||||
|
||||
this.element.dispatchEvent(new CustomEvent("tiltChange", {
|
||||
"detail": values
|
||||
|
@ -132,10 +151,77 @@ class VanillaTilt {
|
|||
this.updateCall = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the glare element (if glarePrerender equals false)
|
||||
* and sets the default style
|
||||
*/
|
||||
prepareGlare() {
|
||||
// If option pre-render is enabled we assume all html/css is present for an optimal glare effect.
|
||||
if (!this.glarePrerender) {
|
||||
// Create glare element
|
||||
const jsTiltGlare = document.createElement("div");
|
||||
jsTiltGlare.classList.add("js-tilt-glare");
|
||||
|
||||
const jsTiltGlareInner = document.createElement("div");
|
||||
jsTiltGlareInner.classList.add("js-tilt-glare-inner");
|
||||
|
||||
jsTiltGlare.appendChild(jsTiltGlareInner);
|
||||
this.element.appendChild(jsTiltGlare);
|
||||
}
|
||||
|
||||
this.glareElementWrapper = this.element.querySelector(".js-tilt-glare");
|
||||
this.glareElement = this.element.querySelector(".js-tilt-glare-inner");
|
||||
|
||||
if (this.glarePrerender) return;
|
||||
|
||||
const glareElementWrapperStyle = {
|
||||
"position": "absolute",
|
||||
"top": "0",
|
||||
"left": "0",
|
||||
"width": "100%",
|
||||
"height": "100%",
|
||||
"overflow": "hidden"
|
||||
};
|
||||
|
||||
Object.assign(this.glareElementWrapper.style, glareElementWrapperStyle);
|
||||
|
||||
const glareElementStyle = {
|
||||
'position': 'absolute',
|
||||
'top': '50%',
|
||||
'left': '50%',
|
||||
'pointer-events': 'none',
|
||||
'background-image': `linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)`,
|
||||
'width': `${this.element.offsetWidth*2}px`,
|
||||
'height': `${this.element.offsetWidth*2}px`,
|
||||
'transform': 'rotate(180deg) translate(-50%, -50%)',
|
||||
'transform-origin': '0% 0%',
|
||||
'opacity': '0',
|
||||
};
|
||||
|
||||
Object.assign(this.glareElement.style, glareElementStyle);
|
||||
}
|
||||
|
||||
updateGlareSize() {
|
||||
Object.assign(this.glareElement.style, {
|
||||
'width': `${this.element.offsetWidth*2}`,
|
||||
'height': `${this.element.offsetWidth*2}`,
|
||||
});
|
||||
}
|
||||
|
||||
onWindowResizeBind() {
|
||||
this.updateGlareSize();
|
||||
}
|
||||
|
||||
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);
|
||||
if (this.glare) this.glareElement.style.transition = `opacity ${this.settings.speed}ms ${this.settings.easing}`;
|
||||
|
||||
this.transitionTimeout = setTimeout(() => {
|
||||
this.element.style.transition = "";
|
||||
if (this.glare) this.glareElement.style.transition = "";
|
||||
}, this.settings.speed);
|
||||
|
||||
}
|
||||
|
||||
extendSettings(settings) {
|
||||
|
@ -148,11 +234,13 @@ class VanillaTilt {
|
|||
speed: "300",
|
||||
transition: true,
|
||||
axis: null,
|
||||
glare: false,
|
||||
"max-glare": 1,
|
||||
"glare-prerender": false,
|
||||
reset: true
|
||||
};
|
||||
|
||||
let newSettings = {};
|
||||
|
||||
for (var property in defaultSettings) {
|
||||
if (property in settings) {
|
||||
newSettings[property] = settings[property];
|
||||
|
|
|
@ -35,24 +35,35 @@ var VanillaTilt = function () {
|
|||
this.settings = this.extendSettings(settings);
|
||||
|
||||
this.reverse = this.settings.reverse ? -1 : 1;
|
||||
// If [data-tilt-glare] or [data-tilt-glare="true"] or [data-tilt-glare="1"]
|
||||
this.glare = this.isSettingTrue(this.settings.glare);
|
||||
this.glarePrerender = this.isSettingTrue(this.settings["glare-prerender"]);
|
||||
|
||||
if (this.glare) this.prepareGlare();
|
||||
this.addEventListeners();
|
||||
}
|
||||
|
||||
VanillaTilt.prototype.isSettingTrue = function isSettingTrue(setting) {
|
||||
return setting === "" || setting === true || setting === 1;
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.addEventListeners = function addEventListeners() {
|
||||
this.onMouseEnterBind = this.onMouseEnter.bind(this);
|
||||
this.onMouseMoveBind = this.onMouseMove.bind(this);
|
||||
this.onMouseLeaveBind = this.onMouseLeave.bind(this);
|
||||
this.onWindowResizeBind = this.onWindowResizeBind.bind(this);
|
||||
|
||||
this.element.addEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.addEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.addEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.addEventListener("resize", this.onWindowResizeBind);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.removeEventListeners = function removeEventListeners() {
|
||||
this.element.removeEventListener("mouseenter", this.onMouseEnterBind);
|
||||
this.element.removeEventListener("mousemove", this.onMouseMoveBind);
|
||||
this.element.removeEventListener("mouseleave", this.onMouseLeaveBind);
|
||||
if (this.glare) window.removeEventListener("resize", this.onWindowResizeBind);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.destroy = function destroy() {
|
||||
|
@ -97,6 +108,10 @@ var VanillaTilt = function () {
|
|||
|
||||
_this.element.style.transform = "perspective(" + _this.settings.perspective + "px) " + "rotateX(0deg) " + "rotateY(0deg) " + "scale3d(1, 1, 1)";
|
||||
});
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = "rotate(180deg) translate(-50%, -50%)";
|
||||
this.glareElement.style.opacity = "0";
|
||||
}
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.getValues = function getValues() {
|
||||
|
@ -131,6 +146,11 @@ var VanillaTilt = function () {
|
|||
|
||||
this.element.style.transform = "perspective(" + this.settings.perspective + "px) " + "rotateX(" + (this.settings.axis === "x" ? 0 : values.tiltY) + "deg) " + "rotateY(" + (this.settings.axis === "y" ? 0 : values.tiltX) + "deg) " + "scale3d(" + this.settings.scale + ", " + this.settings.scale + ", " + this.settings.scale + ")";
|
||||
|
||||
if (this.glare) {
|
||||
this.glareElement.style.transform = "rotate(" + values.angle + "deg) translate(-50%, -50%)";
|
||||
this.glareElement.style.opacity = "" + values.percentageY * this.settings["max-glare"] / 100;
|
||||
}
|
||||
|
||||
this.element.dispatchEvent(new CustomEvent("tiltChange", {
|
||||
"detail": values
|
||||
}));
|
||||
|
@ -138,13 +158,79 @@ var VanillaTilt = function () {
|
|||
this.updateCall = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends the glare element (if glarePrerender equals false)
|
||||
* and sets the default style
|
||||
*/
|
||||
|
||||
|
||||
VanillaTilt.prototype.prepareGlare = function prepareGlare() {
|
||||
// If option pre-render is enabled we assume all html/css is present for an optimal glare effect.
|
||||
if (!this.glarePrerender) {
|
||||
// Create glare element
|
||||
var jsTiltGlare = document.createElement("div");
|
||||
jsTiltGlare.classList.add("js-tilt-glare");
|
||||
|
||||
var jsTiltGlareInner = document.createElement("div");
|
||||
jsTiltGlareInner.classList.add("js-tilt-glare-inner");
|
||||
|
||||
jsTiltGlare.appendChild(jsTiltGlareInner);
|
||||
this.element.appendChild(jsTiltGlare);
|
||||
}
|
||||
|
||||
this.glareElementWrapper = this.element.querySelector(".js-tilt-glare");
|
||||
this.glareElement = this.element.querySelector(".js-tilt-glare-inner");
|
||||
|
||||
if (this.glarePrerender) return;
|
||||
|
||||
var glareElementWrapperStyle = {
|
||||
"position": "absolute",
|
||||
"top": "0",
|
||||
"left": "0",
|
||||
"width": "100%",
|
||||
"height": "100%",
|
||||
"overflow": "hidden"
|
||||
};
|
||||
|
||||
Object.assign(this.glareElementWrapper.style, glareElementWrapperStyle);
|
||||
|
||||
var glareElementStyle = {
|
||||
'position': 'absolute',
|
||||
'top': '50%',
|
||||
'left': '50%',
|
||||
'pointer-events': 'none',
|
||||
'background-image': "linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%)",
|
||||
'width': this.element.offsetWidth * 2 + "px",
|
||||
'height': this.element.offsetWidth * 2 + "px",
|
||||
'transform': 'rotate(180deg) translate(-50%, -50%)',
|
||||
'transform-origin': '0% 0%',
|
||||
'opacity': '0'
|
||||
};
|
||||
|
||||
Object.assign(this.glareElement.style, glareElementStyle);
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.updateGlareSize = function updateGlareSize() {
|
||||
Object.assign(this.glareElement.style, {
|
||||
'width': "" + this.element.offsetWidth * 2,
|
||||
'height': "" + this.element.offsetWidth * 2
|
||||
});
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.onWindowResizeBind = function onWindowResizeBind() {
|
||||
this.updateGlareSize();
|
||||
};
|
||||
|
||||
VanillaTilt.prototype.setTransition = function setTransition() {
|
||||
var _this2 = this;
|
||||
|
||||
clearTimeout(this.transitionTimeout);
|
||||
this.element.style.transition = this.settings.speed + "ms " + this.settings.easing;
|
||||
if (this.glare) this.glareElement.style.transition = "opacity " + this.settings.speed + "ms " + this.settings.easing;
|
||||
|
||||
this.transitionTimeout = setTimeout(function () {
|
||||
return _this2.element.style.transition = "";
|
||||
_this2.element.style.transition = "";
|
||||
if (_this2.glare) _this2.glareElement.style.transition = "";
|
||||
}, this.settings.speed);
|
||||
};
|
||||
|
||||
|
@ -158,11 +244,13 @@ var VanillaTilt = function () {
|
|||
speed: "300",
|
||||
transition: true,
|
||||
axis: null,
|
||||
glare: false,
|
||||
"max-glare": 1,
|
||||
"glare-prerender": false,
|
||||
reset: true
|
||||
};
|
||||
|
||||
var newSettings = {};
|
||||
|
||||
for (var property in defaultSettings) {
|
||||
if (property in settings) {
|
||||
newSettings[property] = settings[property];
|
||||
|
|
Loading…
Reference in a new issue