简体   繁体   English

如何触发翻转效果

[英]How to trigger a flip effect

I have the following code to create a flip image where I want the image to show a flip effect when user click it.我有以下代码来创建一个翻转图像,我希望图像在用户click它时显示翻转效果。

 let img = document.querySelector('img') let div; let click = 0; img.onclick=function(){ console.log(click) if(click %2 ==0){ div = document.createElement('div') document.body.appendChild(div); div.className = 'flip' div.textContent= 'Wikipedia' img.style.display = 'none' }else{ img.style.display = 'revert' div.remove() } click++ }
 @keyframes fliping{ from{ transform: rotateY(0deg); }to{ transform: rotateY(180deg); } } img{ width:100px; height:70px; position:absolute; top:20px; left:20px; text-align:center; line-height:70px; }.flip{ position:absolute; top:20px; left:20px; width:100px; height:70px; background:grey; animation-name: fliping; animation-duration: 1s; }
 <img src='https://blogs.stthomas.edu/english/files/2016/04/Wikipedia-Logo.jpg'>

I try to use keyframe to do that.我尝试使用keyframe来做到这一点。 However, there are two problems with my code:但是,我的代码有两个问题:

The text will flip as the element flips.文本将随着元素翻转而翻转。

I am unable to click the img again to flip back since I make the img tag to become display:none .由于我将img标签变为display:none ,因此我无法再次单击img来翻转。

Could anyone tell me how could I solve the problem or provide me a better solution to trigger a flip effect?谁能告诉我如何解决问题或为我提供更好的解决方案来触发翻转效果?

Thanks for any responds!感谢您的任何回复!

You should use opacity instead of display.您应该使用不透明度而不是显示。

 let img = document.querySelector('img') let div; let click = 0; img.onclick=function(){ console.log(click) if(click %2 ==0){ div = document.createElement('div') document.body.appendChild(div); div.className = 'flip' div.textContent= 'Wikipedia' img.style.opacity = '0' }else{ img.style.opacity = '100' div.remove() } click++ }
 @keyframes fliping{ from{ transform: rotateY(0deg); }to{ transform: rotateY(180deg); } } img{ width:100px; height:70px; position:absolute; top:20px; left:20px; text-align:center; line-height:70px; z-index: 2; }.flip{ position:absolute; top:20px; left:20px; width:100px; height:70px; background:grey; animation-name: fliping; animation-duration: 1s; }
 <img src='https://blogs.stthomas.edu/english/files/2016/04/Wikipedia-Logo.jpg'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM