javascript - animation Flip div when clicked -
i trying make divs flip whenever click on them. i'm not sure why doesn't work. please help.
here demo of code. http://langbook.co/testicles-1-2-flashcards/
<!doctype html> <html> <head> <style type="text/css"> #flip3d{ width:240px; height:200px; margin:10px; float:left; } #flip3d > #front{ position:absolute; transform: perspective( 600px ) rotatey( 0deg ); background:#fc0; width:240px; height:200px; border-radius: 7px; backface-visibility: hidden; transition: transform .5s linear 0s;} #flip3d > #back{ position:absolute; transform: perspective( 600px ) rotatey( 180deg ); background: #80bfff; width:240px; height:200px; border-radius: 7px; backface-visibility: hidden; transition: transform .5s linear 0s;} </style> <script> function flip(el){ el.children[1].style.transform = "perspective(600px) rotatey(-180deg)"; el.children[0].style.transform = "perspective(600px) rotatey(0deg)";} var vlib = document.getelementbyid('front'); vlib.addeventlistener(click, flip(el)); </script> <title>flashcards</title> </head> <body> <div id="flip3d"> <div id="back">box - back</div> <div id="front">box - front </div> </div> </body> </html>
first, move js end of page.
it seems want calling flip() on #flip3d, because trying access it's child elements, so:
var vlib = document.getelementbyid('flip3d'); vlib.addeventlistener('click', flip(vlib));
to flip back, keep object's state in attribute know side it's on. note flip(vlib) called immediately; wait click event, you'll need pass reference function:
vlib.addeventlistener('click', flip);
seems work: jsfiddle
Comments
Post a Comment