When triggering an event in an array element (with querySelectAll) trigger only one element of another array (also with querySelectAll)

2

In the code below, I would like that when passing and removing the mouseover and mouseout ) on a li , change would be made only in the corresponding element to this li , in this case, the tags a and i within li , just as happens with the CSS of the code background and color of the i tag changed with hover ). The problem I now have is that when I trigger the event, instead of changing only the element within the li that the mouse is over at the moment, all elements are affected at once.

To get better follow the codepen link with the full code: link

Thanks if you can help.

function abc() {

    var myLi = document.querySelectorAll('li'),
        icons = document.querySelectorAll('li i'),        

    //MOUSEOVER
    function changeI() {
        'use strict';
        if (icons.length) {
            for (var i = 0; i < icons.length; i++) {
                icons[i].style.fontSize = '2em';
            }
        }
    }
    //MOUSEOUT
    function backI() {
        'use strict';
        if (icons.length) {
            for (var i = 0; i < icons.length; i++) {
                icons[i].style.fontSize = '1em';
            }
        }
    }
    if (myLi.length) {
        for (var i = 0; i < myLi.length; i++) {
            myLi[i].addEventListener("mouseover", changeI);
            myLi[i].addEventListener("mouseout", backI);
        }
    }
}
abc();
    
asked by anonymous 14.03.2017 / 17:30

2 answers

1

Your main error is that you are passing the array with all the elements at the time of changing their style, you want to change the element itself, this .

var cresce = function() {
  this.style.fontSize = "2em";
}

var diminui = function() {
  this.style.fontSize = "1em"
}

// essa variável terá um array dentro
var lis = document.querySelectorAll('li');

// aí é só passar por ela e incluir os eventos
for (var el, ix = 0; ix < lis.length; ix++) {
    el = lis[ix];
    console.log(el);
    el.addEventListener('mouseover', cresce);
    el.addEventListener('mouseout', diminui);
}

Sergio's solution has the advantages of being a single function and using the javascript function return, which is really cool but hard to understand when you are learning to program. I hope this solution gets clearer, though it's less efficient.

    
14.03.2017 / 20:10
1

I think you're complicating. First of all this can be done only with CSS, like this:

* {
    font-family: sans-serif;
    margin: 0 auto;
    box-shadow: border-box;
    position: relative;
}

body {
    left: 0;
    right: 0;
    margin: 0;
    background: #898989;
}

#container {
    width: 1030px;
}

a {
    text-decoration: none!important;
    outline: 0!important;
    border: 0!important
}

a:hover {
    text-decoration: none;
    outline: 0!important;
    border: 0!important
}

li {
    list-style: none;
    position: relative;
}

img {
    left: 0;
    width: 100%;
    height: auto
}

footer {
    width: 100%;
    height: 13em;
    background-color: #a8a8a8;
    text-align: center;
    font-size: 1.4em;
    font-weight: 700;
    color: #e0e0e0
}

#footFlex {
    width: 80%;
    display: flex;
}

#footFlex > div:not(p){
    margin-top: 6%!important
}

#footFlex ul {
    display: flex;
    margin-right: 40px;
    width: 100%;
    height: 5em;
}

#footFlex li {
    border: 2px solid #fff;
    border-radius: 50%;
    padding: 10px;
    margin: 2px;
    cursor: pointer;
    transition: all .5s;
    height: 2.5em;
}

#footFlex li:hover {
    background-color: #fff;
    height: 5em;
}

#footFlex li:hover a{
    color: #722872;
    font-size: 2em;
    line-height: 2em;
}

#footFlex li i:hover{
    background-color: transparent;
}

#footFlex li a{
  line-height: 1em;
    transition: all .5s;
    color: #fff
}

#footFlex div:first-child p:nth-child(2){
    width: 65%;
    left: 17.5%
}

#footFlex div p:nth-child(1){
    font-size: 1.3em
}

#footFlex div:first-child p:nth-child(2) a{
    color: #722872;
    font-weight: 400;
}

#footFlex div:first-child p:nth-child(2) a:hover {
    border-bottom: 2px solid #722872!important
}

#footFlex div:first-child p:first-child, #footFlex div:nth-child(2) p:first-child {
    margin-bottom: 1em
}

footer + div {
    height: 5.6em;
    background-color: #722872;
    font-size: 1.4em;
    font-weight: 700;
    color: #e0e0e0;
    line-height: 5.6em;
    text-align: center
}
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="FreeCodeCamp.css">
</head>

<body id="about" data-spy="scroll" data-target=".navbar" data-offset="50">
 
    <footer>
    <div id="footFlex">
       <div>           
           <p>ABOUT THIS PAGE</p>
           <p>Made with <i class="fa fa-fw fa-music"></i> and <i class="fa fa-fw fa-coffee"></i> by <a href="">Justin Sane.</a></p>
       </div>
       <div>
           <p>AROUND THE WEB</p>
           <ul>
              <li><a rel="nofollow" class="button social" href="https://www.linkedin.com/in/hallaathrad"><i class="fa fa-fw fa-linkedin"></i></a></li>
              <li><a rel="nofollow" class="button social" href="https://github.com/hallaathrad"><i class="fa fa-fw fa-github"></i></a></li>
              <li><a rel="nofollow" class="button social" href="https://twitter.com/hallaathrad"><i class="fa fa-fw fa-twitter"></i></a></li>
              <li><a rel="nofollow" class="button social" href="https://www.flickr.com/photos/hallaathrad/" title="My Flickr"><i class="fa fa-fw fa-flickr"></i></a></li>
           </ul>
       </div>
    </div>
    </footer>
    <div>qlip © 2017. All Rights Reversed</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="Javascript.js"></script>
</body>

</html>

And if you want to use JavaScript you can directly pass in the elements and size to avoid having to iterate all over again:

function changeI(el, size) {
    'use strict';
  return function(){
    el.style.fontSize = size;
  }
}

if (liBArray.length) {
    for (var i = 0; i < liB.length; i++) {
        liBArray[i].addEventListener("mouseover", changeI(iconsArray[i], '2em'));
        liBArray[i].addEventListener("mouseout", changeI(iconsArray[i], '1em'));
    }
}
    
14.03.2017 / 17:44