从汉堡棒转换和转换

编程入门 行业动态 更新时间:2024-10-24 16:29:44
从汉堡棒转换和转换 - >点击X(使用手写笔)(CSS transition & transform from hamburger bars --> X on click (using stylus))

我有一个移动断点的菜单汉堡“图标”。 我把它设置为3行,我希望它们转换为X(将关闭菜单)。

我希望顶部的条形走45度,中间的条形条消失,底部的条形条形走向45度。 然后顶部和底部的条形图将向上移动并创建一个X.

现在......只要我按住鼠标,它就会动画。 为什么会这样? 我只需要动画就可以完成点击。

HTML:

<a class="navbar-item-link" "javascript:void(0)" > <div class="hamburger-icon"><span></span></div> </a>

笔:

.hamburger-icon &:before, &.hamburger-icon span, &:after content '' display block height 2px width 20px background-size 100% background rgba(255,255,255,0.5) margin 6px auto 7px auto transition all 0.2s linear &:active &.hamburger-icon span background-color transparent &:before transform rotate(45deg) top -10px height 2px background rgba(255,255,255,0.5) width 30px &:after transform rotate(-45deg) bottom -10px height 2px background rgba(255,255,255,0.5) width 30px

i have a menu hamburger "icon" for the mobile breakpoint. i have it set up as 3 lines and i want them to transition to an X (that will close the menu).

i want the top bar to go 45 degrees, the middle bar to disappear, and the bottom bar to go 45 degrees the other way. then the top and bottom bars will shift up and create an X

AS OF NOW....it only animates for as long as i hold my mouse down. Why is this so? I just need the animation to complete itself on click.

html:

<a class="navbar-item-link" "javascript:void(0)" > <div class="hamburger-icon"><span></span></div> </a>

stylus:

.hamburger-icon &:before, &.hamburger-icon span, &:after content '' display block height 2px width 20px background-size 100% background rgba(255,255,255,0.5) margin 6px auto 7px auto transition all 0.2s linear &:active &.hamburger-icon span background-color transparent &:before transform rotate(45deg) top -10px height 2px background rgba(255,255,255,0.5) width 30px &:after transform rotate(-45deg) bottom -10px height 2px background rgba(255,255,255,0.5) width 30px

最满意答案

:active行为就像鼠标一样。 当您释放单击时,动画将停止。

您有一些使用JSCSS的解决方案。

CSS中 ,您可以使用keyframes来确保完成动画。

JS中 ,您可以使用click event ,该click event可以使用JS动画为您的图标设置动画,或者添加一个包含您点击的属性的类。


以下示例使用预处理器LESSSASS在这里具有相同的语法:

.hamburger-icon { /* .hamburger-icon styles */ &.active { span { background-color: transparent; } &:before { transform: rotate(45deg); top: -10px; height: 2px; background: rgba(255,255,255,0.5); width: 30px; } &:after { transform: rotate(-45deg); bottom: -10px; height: 2px; background: rgba(255,255,255,0.5); width: 30px; } } }

然后在jQuery中

$('.hamburger-icon').on('click', function(){ $(this).addClass('active'); });

希望你明白了。

祝你好运'

The :active acts like a mouse down. When you'll release the click, the animation will stop.

You have a few solutions using JS or CSS.

In CSS, you could use the keyframes to be sure your animation will be finished.

In JS, you could use a click event which could animate your icon with JS animations, or add a class which would contain your clicked properties.


The following example is using a preprocessor. LESS or SASS would have the same syntax here:

.hamburger-icon { /* .hamburger-icon styles */ &.active { span { background-color: transparent; } &:before { transform: rotate(45deg); top: -10px; height: 2px; background: rgba(255,255,255,0.5); width: 30px; } &:after { transform: rotate(-45deg); bottom: -10px; height: 2px; background: rgba(255,255,255,0.5); width: 30px; } } }

Then in jQuery

$('.hamburger-icon').on('click', function(){ $(this).addClass('active'); });

Hope you got the point.

Good Luck'

更多推荐

本文发布于:2023-08-05 04:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!