'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; (function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if ((typeof module === 'undefined' ? 'undefined' : _typeof(module)) === 'object' && module.exports) { // Node/CommonJS module.exports = function (root, jQuery) { if (jQuery === undefined) { // require('jQuery') returns a factory that requires window to // build a jQuery instance, we normalize how we use modules // that require this pattern but the window provided is a noop // if it's defined (how jquery works) if (typeof window !== 'undefined') { jQuery = require('jquery'); } else { jQuery = require('jquery')(root); } } factory(jQuery); return jQuery; }; } else { // Browser globals factory(jQuery); } })(function ($) { $.fn.tilt = function (options) { /** * RequestAnimationFrame */ var requestTick = function requestTick() { if (this.ticking) return; requestAnimationFrame(updateTransforms.bind(this)); this.ticking = true; }; /** * Bind mouse movement evens on instance */ var bindEvents = function bindEvents() { var _this = this; $(this).on('mousemove', mouseMove); $(this).on('mouseenter', mouseEnter); if (this.settings.reset) $(this).on('mouseleave', mouseLeave); if (this.settings.glare) $(window).on('resize', updateGlareSize.bind(_this)); }; /** * Set transition only on mouse leave and mouse enter so it doesn't influence mouse move transforms */ var setTransition = function setTransition() { var _this2 = this; if (this.timeout !== undefined) clearTimeout(this.timeout); $(this).css({ 'transition': this.settings.speed + 'ms ' + this.settings.easing }); if (this.settings.glare) this.glareElement.css({ 'transition': 'opacity ' + this.settings.speed + 'ms ' + this.settings.easing }); this.timeout = setTimeout(function () { $(_this2).css({ 'transition': '' }); if (_this2.settings.glare) _this2.glareElement.css({ 'transition': '' }); }, this.settings.speed); }; /** * When user mouse enters tilt element */ var mouseEnter = function mouseEnter(event) { this.ticking = false; $(this).css({ 'will-change': 'transform' }); setTransition.call(this); // Trigger change event $(this).trigger("tilt.mouseEnter"); }; /** * Return the x,y position of the mouse on the tilt element * @returns {{x: *, y: *}} */ var getMousePositions = function getMousePositions(event) { if (typeof event === "undefined") { event = { pageX: $(this).offset().left + $(this).outerWidth() / 2, pageY: $(this).offset().top + $(this).outerHeight() / 2 }; } return { x: event.pageX, y: event.pageY }; }; /** * When user mouse moves over the tilt element */ var mouseMove = function mouseMove(event) { this.mousePositions = getMousePositions(event); requestTick.call(this); }; /** * When user mouse leaves tilt element */ var mouseLeave = function mouseLeave() { setTransition.call(this); this.reset = true; requestTick.call(this); // Trigger change event $(this).trigger("tilt.mouseLeave"); }; /** * Get tilt values * * @returns {{x: tilt value, y: tilt value}} */ var getValues = function getValues() { var width = $(this).outerWidth(); var height = $(this).outerHeight(); var left = $(this).offset().left; var top = $(this).offset().top; var percentageX = (this.mousePositions.x - left) / width; var percentageY = (this.mousePositions.y - top) / height; // x or y position inside instance / width of instance = percentage of position inside instance * the max tilt value var tiltX = (this.settings.maxTilt / 2 - percentageX * this.settings.maxTilt).toFixed(2); var tiltY = (percentageY * this.settings.maxTilt - this.settings.maxTilt / 2).toFixed(2); // angle var angle = Math.atan2(this.mousePositions.x - (left + width / 2), -(this.mousePositions.y - (top + height / 2))) * (180 / Math.PI); // Return x & y tilt values return { tiltX: tiltX, tiltY: tiltY, 'percentageX': percentageX * 100, 'percentageY': percentageY * 100, angle: angle }; }; /** * Update tilt transforms on mousemove */ var updateTransforms = function updateTransforms() { this.transforms = getValues.call(this); if (this.reset) { this.reset = false; $(this).css('transform', 'perspective(' + this.settings.perspective + 'px) rotateX(0deg) rotateY(0deg)'); // Rotate glare if enabled if (this.settings.glare) { this.glareElement.css('transform', 'rotate(180deg) translate(-50%, -50%)'); this.glareElement.css('opacity', '0'); } return; } else { $(this).css('transform', 'perspective(' + this.settings.perspective + 'px) rotateX(' + (this.settings.disableAxis === 'x' ? 0 : this.transforms.tiltY) + 'deg) rotateY(' + (this.settings.disableAxis === 'y' ? 0 : this.transforms.tiltX) + 'deg) scale3d(' + this.settings.scale + ',' + this.settings.scale + ',' + this.settings.scale + ')'); // Rotate glare if enabled if (this.settings.glare) { this.glareElement.css('transform', 'rotate(' + this.transforms.angle + 'deg) translate(-50%, -50%)'); this.glareElement.css('opacity', '' + this.transforms.percentageY * this.settings.maxGlare / 100); } } // Trigger change event $(this).trigger("change", [this.transforms]); this.ticking = false; }; /** * Prepare elements */ var prepareGlare = function prepareGlare() { var glarePrerender = this.settings.glarePrerender; // If option pre-render is enabled we assume all html/css is present for an optimal glare effect. if (!glarePrerender) // Create glare element $(this).append('