I am new to Javascript and Jquery and will write a script that changes the CSS 'transform' values ββwhen the mouse moves in the browser window to make the image change perspective.
Everything works great! I just want to optimize javascript / jquery in order to reduce the amount of code (since I focus on CSS -webkit- -moz- -ms- etc.), and also simplify the setup of the script.
So here is the part I want to optimize:
$('#logo').css({ '-webkit-transform': "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/300 + "deg)", '-moz-transform': "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/300 + "deg)", '-ms-transform': "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/300 + "deg)", 'transform': "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/300 + "deg)", });
As you can see, the only change in each of the four central sections is the specific elements of the CSS browser (-webkit-, -moz-, etc.), so naturally, I want to create an array that will contain each of these options and then iterate over the options.
This is my first attempt to use javascript "for" for a loop:
var browserTransitionCSS = [ '-webkit-transition', '-moz-transition', '-ms-transition', 'transition' ]; for(var i=0; i < browserTransformCSS.length; i++) { $('#logo').css({ browserTransformCSS: "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/200 + "deg)", }); };
This is my second attempt using jquery "each" to loop:
var browserTransitionCSS = [ '-webkit-transition', '-moz-transition', '-ms-transition', 'transition' ]; $.each(browserTransitionCSS, function(something) { $('#logo').css({ something : "rotateY(" + (e.pageX-(current_width/2))/150 + "deg) rotateX(" + (e.pageY-(e.pageY*2-600)-(current_height/2))/200 + "deg)", }); });
And now, after several hours of fighting, I am leaving and asking if anyone can help me.
If it would be helpful to see my URL, please ask.
Any help here would be greatly appreciated. Thank you very much.