-2

For the following HTML:

<li id="li1">
    <div id="card-container">
        <div id="card">
            <div class="front">
            <img id="track_image" src="image.jpg" />
        </div>
        <div class="back">
            <div id="controls">
                <audio controls></audio>
            </div>
        </div>  
    </div>
</li>

I have the following CSS, which will flip it on hover.

#card-container:hover .back {   
    -webkit-transform: rotateX(0deg);
}

#card-container:hover .front {      
    -webkit-transform: rotateX(-180deg);
}

But I want to change it so the flip instead happens on a click, I've attached a click listener and am trying this code below but it doesn't do anything

function click(evt, listElementId) {
       var listElement = document.getElementById(listElementId);
       var front = listElement.getElementsByClassName("front")[0];
       front.style.webkitTransition = "rotateX(-180deg)";
       var back = listElement.getElementsByClassName("back")[0];
       back.style.webkitTransition = "rotateX(0deg)"

What should the JavaScript be to achieve the same as the CSS is doing?

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

2 Answers2

2

You have confused transitions and transformations. According to this answer, you should use

front.style.webkitTransform = "rotateX(-180deg)";
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Use a class:

#card-container.flipped .back {   
    -webkit-transform: rotateX(0deg);
}

#card-container.flipped .front {      
    -webkit-transform: rotateX(-180deg);
}
function click(evt, listElementId) {
    document.getElementById(listElementId).classList.add('flipped');
}
Ry-
  • 218,210
  • 55
  • 464
  • 476