简体   繁体   English

在CSS中点击时如何创建一个按钮总是同向旋转?

[英]How can I create a button that always rotates in the same direction when clicked in CSS?

my goal is to make a flippable card in css that always rotates around the x-Axis when clicked, but I can't figure out how to make the card rotate in the same direction everytime.我的目标是在 css 中制作一张可翻转的卡片,点击时它总是绕 x 轴旋转,但我不知道如何让卡片每次都沿相同方向旋转。 What I got is only to make the card rotate 180° when clicked the first time, but it will rotate back in the opposite direction when clicked again, which is not the kind of effect I wanted to achieve.我得到的只是第一次点击时卡片旋转了180°,再次点击时它会向相反的方向旋转回去,这不是我想要达到的那种效果。 I found this solution but it uses JQuery and feels very hacky, is there a more straight-forward solution maybe even in pure CSS?我找到了这个解决方案,但它使用 JQuery 并且感觉很老套,是否有更直接的解决方案,甚至在纯 CSS 中?

Any help would be appreciated thanks in advance.提前感谢任何帮助。

A little code example of what I achieved so far:到目前为止我取得的成就的一个小代码示例:

 function flip(id) { document.getElementById(id).querySelector(".flip-content").classList.toggle("flipped"); }
 .flip-card { background: transparent; perspective: 1000px; width: 200px; height: 200px; transition: transform 0.3s; transform: scale(90%); }.flip-content { width: 100%; height: 100%; transform-style: preserve-3d; transition: all 0.6s; box-shadow: 0 20px 32px 0 rgba(0, 0, 0, 0.2); }.flip-content.flipped { transform: rotateX(180deg); box-shadow: 0 -20px 32px 0 rgba(0, 0, 0, 0.2); }
 <div id="flip" class="flip-card" onclick="flip('flip')"> <div class="flip-content"> </div> </div>

You could run the transform directly onto the element with javascript without toggling class, like so:您可以直接在 javascript 元素上运行转换,而无需切换 class,如下所示:

 let rot = 0; function flip(id) { rot += 180; const flipContent = document.getElementById(id).querySelector(".flip-content"); flipContent.style.transform = `rotateX(${rot}deg)`; flipContent.style['box-shadow'] = (rot % 360 === 0)? `0 20px 32px 0 rgba(0, 0, 0, 0.2)`: `0 -20px 32px 0 rgba(0, 0, 0, 0.2)`; }
 .flip-card { background: transparent; perspective: 1000px; width: 200px; height: 200px; transition: transform 0.3s; transform: scale(90%); }.flip-content { width: 100%; height: 100%; transform-style: preserve-3d; transition: all 0.6s; box-shadow: 0 20px 32px 0 rgba(0, 0, 0, 0.2); }.flip-content.flipped { transform: rotateX(180deg); box-shadow: 0 -20px 32px 0 rgba(0, 0, 0, 0.2); }
 <div id="flip" class="flip-card" onclick="flip('flip')"> <div class="flip-content"> </div> </div>

暂无
暂无

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

相关问题 单击按钮时如何删除CSS部分 - How can I remove a css portion when a button is clicked 单击按钮时如何打开相同的选项卡 - How can I open the same tab when a button is clicked 如何创建一个在单击时增加计数器的按钮? - How can I create a button that increments a counter when clicked? 在a.html中单击按钮并仍然使用相同的javascript文件时,如何在b.html中播放css动画? - How can I play a css animation in b.html when a button is clicked in a.html and still use the same javascript file? 如何使用CSS创建一个三角形div,它在单击时会消失,但在单击“溢出”部分时不会消失? - How can I create a triangle div with CSS which will disappear when clicked but not when the 'overflow' part is clicked? 单击按钮时如何创建烟花 - How can create fireworks when a button is clicked 使用CSS3或JavaScript单击时,如何“突出显示”此CSS按钮? - How can I “highlight” this CSS button when is clicked using CSS3 or JavaScript? 当使用html和css单击按钮图像时,如何使用按钮图像获取按钮图像源 - how can I use button images to get the button image source when they are clicked using html and css 我如何创建一个固定的按钮,当使用HTML和CSS单击该按钮时会打开一个联系表单? - How do I create a fixed button which opens a contact form when clicked using HTML & CSS? 单击另一个div中的按钮时,如何为div添加CSS样式 - how can i add css style for a div when i clicked on a button in another div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM