Search code examples
javascripthtmlinline-coderollover-effect

Hover over element animate next element - How to remove inline JS


I'm relatively new to JavaScript, but I'm trying to find a more efficient method for calling a rollover function without using inline events within the HTML. Below is the method I'm currently using:

HTML

        <div id="work_square">
        <img onmouseover="rolloverIn('rollover_1');" onmouseout="rolloverOut('rollover_1');" src="images/frugal_image.png" width="100%"/>
        <div onmouseover="rolloverIn('rollover_1');" onmouseout="rolloverOut('rollover_1');" id="rollover_1" class="rollovers">
            <div id="rollover_text">
                <h2>ROLLOVER 1 TITLE</h2>
                <p>This is rollover 1.</p>
            </div>
        </div>
    </div>
    <div id="work_square">
        <img onmouseover="rolloverIn('rollover_2');" onmouseout="rolloverOut('rollover_2');" src="images/exhibiton_image.jpg" width="100%"/>
        <div onmouseover="rolloverIn('rollover_2');" onmouseout="rolloverOut('rollover_2');" id="rollover_2" class="rollovers">
            <div id="rollover_text">
                <h2>ROLLOVER 2 TITLE</h2>
                <p>This is rollover 2.</p>
            </div>
        </div>
    </div>

JS

<script>
function rolloverIn(el){
    var elem = document.getElementById(el);
    elem.style.opacity = 1;
    elem.style.transform = "scale(1)";
}
function rolloverOut(el){
    var elem = document.getElementById(el);
    elem.style.opacity = 0;
    elem.style.transform = "scale(0)";
}

Basically I'm calling a function to apply a CSS transform and opacity alteration for a rollover placed over each work_square when either the image or rollover is moused over, and then to remove the alterations on mouse out.

This method works, but it's my understanding that inline coding is bad practice. Could someone point me in the right direction towards a more efficient method?

Thanks.


Solution

  • Sorry to ruin your dream of using JS but
    this is all doable in pure CSS:

    .work_square{ position:relative; }
    .work_square > img{ width:100%; }
    
    .work_square .rollovers{
      position:absolute;
      top:0;
      opacity:0;
      transform: scale(0);
      transition: 0.6s;
    }
    .work_square:hover .rollovers{
      transform: scale(1);
      opacity:1;
    }
        <div class="work_square">
          <img src="//placehold.it/800x300/cf5" />
          <div class="rollovers">
            <div class="rollover_text">
              <h2>ROLLOVER 1 TITLE</h2>
              <p>This is rollover 1.</p>
            </div>
          </div>
        </div>
        <div class="work_square">
          <img src="//placehold.it/800x300/f5f" />
          <div class="rollovers">
            <div class="rollover_text">
              <h2>ROLLOVER 2 TITLE</h2>
              <p>This is rollover 2.</p>
            </div>
          </div>
        </div> 

    Note that I've removed all unnecessary ID (hey, you cannot use duplicate ID's in a valid HTML document).
    Use your container class .work_square and use the CSS :hover on it to add that listener, than simply add the desired class of the children element to target:

    .work_square:hover .rollovers{