Trying to learn JavaScript and this is my first 'real' project.
I am trying to add a delay to each .slice element that is created once the menu button is clicked. The while loop clones and appends and then the for loop changes the opacity to visible on each slice.
I'm trying to figure out how to add a different delay to each slice node so they appear one after each other instead of all at once.
Do I use forEach after the while loop? Any advice would be great.
JSFiddle: http://jsfiddle.net/5d2Gc/
JS:
(function () { 'use strict';
var m = document.getElementById('btn-nav'); var a = document.getElementById('main-nav'); var b = a.getElementsByClassName('background')[0]; var s = a.getElementsByClassName('slice'); m.addEventListener('click', function() { m.classList.add('open'); var c = document.getElementsByClassName('slice')[0]; var sWidth = c.clientWidth; while(sWidth < a.clientWidth*(a.clientHeight / c.clientHeight)) { var d = c.cloneNode(true); c.parentNode.appendChild(d); sWidth = sWidth + d.clientWidth; } for(var i = 0; i < s.length; i++){ s[i].style.opacity = 1; } }, false);
}());
HTML
Menu
<div class="slice"></div> </div>
See this JsFiddle
Instead of :
for(var i = 0; i < s.length; i++){
s[i].style.opacity = 1;
}
Use this :
for (var i=0;i<s.length;i++) {
(function(ind) {
setTimeout(function(){s[ind].style.opacity = 1;}, 1000 + (1000 * ind));
})(i);
}
I have set timeout with every iteration.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments