how to use 'for' to shorten my code ?
Hi,
I would like to replace this part of my code :
-
- $('.home').on('click', function() {
- $('html, body').animate({ scrollTop: $("#home").offset().top}, 1000);
- })
- $('.qui_suis-je').on('click', function() {
- $('html, body').animate({ scrollTop: $("#qui_suis-je").offset().top}, 1000);
- })
- $('.competences').on('click', function() {
- $('html, body').animate({ scrollTop: $("#competences").offset().top}, 1000);
- })
- $('.CV').on('click', function() {
- $('html, body').animate({ scrollTop: $("#CV").offset().top}, 1000);
- })
- $('.contact').on('click', function() {
- $('html, body').animate({ scrollTop: $("#contact").offset().top}, 1000);
- })
by this :
-
- var elementsmenu = new Array(['.home','#home'],['.qui_suis-je','#qui_suis-je'],['.competences', '#competences'],['.CV','#CV'],['.contact','#contact']);
-
- for(var i = 0; i < elementsmenu.length; i++) {
- $(elementsmenu[i][0]).on('click', function() {
- $('html, body').animate({ scrollTop: $(elementsmenu[i][1]).offset().top}, 1000);
- });
- }
but it doesn't work :( I don't understand what's wrong with the new code, can anybody help me ?
The code is supposed to scroll the page to a specific div when you click on a menu's element (you can see how it work directly on me website :
http://dorian.hermosa.free.fr/ )
Thank you :)