From 8f52cd95e8949dbb58a6b3b628677c9fe53fbeb8 Mon Sep 17 00:00:00 2001 From: Jonathan Arbely Date: Sun, 7 Feb 2021 14:37:51 +0100 Subject: [PATCH] Fix #55 - Cannot read property 'x' --- src/tilt.jquery.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/tilt.jquery.js b/src/tilt.jquery.js index ab7598d..22fb8fa 100644 --- a/src/tilt.jquery.js +++ b/src/tilt.jquery.js @@ -116,15 +116,17 @@ const height = $(this).outerHeight(); const left = $(this).offset().left; const top = $(this).offset().top; - const percentageX = (this.mousePositions.x - left) / width; - const percentageY = (this.mousePositions.y - top) / height; - // x or y position inside instance / width of instance = percentage of position inside instance * the max tilt value - const tiltX = ((this.settings.maxTilt / 2) - ((percentageX) * this.settings.maxTilt)).toFixed(2); - const tiltY = (((percentageY) * this.settings.maxTilt) - (this.settings.maxTilt / 2)).toFixed(2); - // angle - const 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, tiltY, 'percentageX': percentageX * 100, 'percentageY': percentageY * 100, angle}; + try { + const percentageX = (this.mousePositions.x - left) / width; + const percentageY = (this.mousePositions.y - top) / height; + // x or y position inside instance / width of instance = percentage of position inside instance * the max tilt value + const tiltX = ((this.settings.maxTilt / 2) - ((percentageX) * this.settings.maxTilt)).toFixed(2); + const tiltY = (((percentageY) * this.settings.maxTilt) - (this.settings.maxTilt / 2)).toFixed(2); + // angle + const 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, tiltY, 'percentageX': percentageX * 100, 'percentageY': percentageY * 100, angle}; + } catch(e){} }; /** @@ -205,7 +207,6 @@ 'transform-origin': '0% 0%', 'opacity': '0', }); - }; /** @@ -302,4 +303,4 @@ $('[data-tilt]').tilt(); return true; -})); \ No newline at end of file +}));