Javascript: Find all elements on page and add the same CSS -
currently learning javascript , trying create expanding menu.
i'm trying find elements of same class , setting css value on click elements in nodelist. error see in console is:
cannot read property 'style' of undefined
i know it's loop not setting variable correctly; not sure is.
jsfiddle
html
<div class="master"> <a href="#" id="btn-nav"> <span></span> <span>menu</span> <span></span> </a> <nav id="main-nav"> <div class="wrapper"> <div class="background"> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> <div class="slice"></div> </div> </div> </nav> </div><!-- end master -->
css
@import url(http://fonts.googleapis.com/css?family=droid+sans); body { background: #2980b9; font-family: 'droid sans', sans-serif; } #btn-nav { position:absolute; left:10px; top:10px; width:30px; height:30px; display:block; z-index:1; } #btn-nav span:nth-child(2) { top:10px; } #btn-nav span:nth-child(3) { top:20px; } #btn-nav span { height: 4px; width: 30px; background: white; content: ""; text-indent: -999em; display:inline-block; position:absolute; zoom: 1; top:0px; left:0px; text-align:center; -webkit-transition: top .3s cubic-bezier(0.165,.84,.44,1); -webkit-transform: rotate(0deg); } #btn-nav:hover > span { top:10px; -webkit-animation: rotateani .5s 1s forwards; } #btn-nav:hover > span:nth-child(1) { -webkit-animation: rotateani .5s .5s forwards, expandani .3s .8s forwards; } #btn-nav:hover > span:nth-child(2) { -webkit-animation: rotateani .5s .5s forwards, expandani-2 .3s .8s forwards; } #btn-nav:hover > span:nth-child(3) { -webkit-animation: rotateani .5s .5s forwards, expandani-3 .3s .8s forwards; } @-webkit-keyframes rotateani { 0% {-webkit-transform: rotate(0deg);} 100% {-webkit-transform: rotate(90deg);} } @-webkit-keyframes expandani { 0% {left:0;} 100% {left:-10px;} } @-webkit-keyframes expandani-2 { 0% {left:0;} 100% {left:0px;} } @-webkit-keyframes expandani-3 { 0% {left:0;} 100% {left:10px;} } #main-nav { position:fixed; left:0; top:0; display:block; width:100%; height:100%; z-index:-1; } #main-nav .wrapper { display:table; width:100%; height:100%; } .slice { width:25%; height:30%; float:left; background:black; opacity:0; -webkit-transition: 1s; } .background { position:absolute; left:0; top:0; width:100%; height:100%; z-index:-1; }
js
(function () { 'use strict'; var menubtn = document.getelementbyid('btn-nav'); var slice = document.queryselectorall('.slice'); for(var = 0; <= slice.length; i++) { var boxes = slice[i]; } menubtn.addeventlistener('click', function() { boxes.style.opacity = 1; }, false); }());
you need place loop inside event, it's doing setting last slice's opacity.
var menubtn = document.getelementbyid('btn-nav'); var slice = document.queryselectorall('.slice'); menubtn.addeventlistener('click', function() { for(var = 0; < slice.length; i++) { slice[i].style.opacity = 1; } }, false);
Comments
Post a Comment