css 漢堡排動畫變成X,按X再變回漢堡排?

clipboard.png

clipboard.png

css動畫一直是我想學的部分
有大神可以分享一下如何做到「漢堡排動畫變成X,按X再變回漢堡排」?
https://www.snapchat.com/
snapchat手機版有一樣的效果,但就是偷不走

阅读 4.4k
2 个回答

打开网站-> f12 -> 开启响应设计模式 -> 点击这个图标观察class变化和相应的css变化 -> 直接把相应代码拷出来
通过改变class .hamburger-expandedcss transition 实现

<div class="header-container">
    <div class="header-hamburger">
        <span></span>
        <span></span>
        <span></span>
    </div>
<div>
.header-hamburger {
    display: block;
    width: 20px;
    height: 15px;
    line-height: 15px;
    position: relative;
    top: 14px;
    right: 15px;
    float: right;
    margin: 0;
    transform: rotate(0deg);
    cursor: pointer;
}

.header-hamburger span {
    display: block;
    position: absolute;
    height: 2.5px;
    width: 100%;
    background: rgb(38, 38, 38);
    box-sizing: border-box;
    border-radius: 1px;
    opacity: 1;
    left: 0;
    transform: rotate(0deg);
    transition: .25s ease-in-out;
    transition-property: transform, opacity;
}

.header-hamburger span:nth-child(1) {
    top: 0px;
    transform-origin: left center;
}

.header-hamburger span:nth-child(2) {
    top: 6px;
    transform-origin: left center;
}

.header-hamburger span:nth-child(3) {
    top: 12px;
    transform-origin: left center;
}

.hamburger-expanded .header-hamburger span:nth-child(1) {
    transform: rotate(45deg) translateY(-1.5px);
}
.hamburger-expanded .header-hamburger span:nth-child(2) {
    transform: scaleX(0);
    opacity: 0;
}
.hamburger-expanded .header-hamburger span:nth-child(3) {
    transform: rotate(-45deg) translateY(1px);
}

原先看到过类似的效果,你看看这个是你想要的吗?
codepen demo

Update:

body,
html,
div {
  background: #292a38;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  text-align: center;
}
svg {
  width: 200px;
  height: 150px;
  cursor: pointer;
  -webkit-transform: translate3d(0, 0, 0);
  -moz-transform: translate3d(0, 0, 0);
  -o-transform: translate3d(0, 0, 0);
  -ms-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
path {
  fill: none;
  -webkit-transition: stroke-dashoffset 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25), stroke-dasharray 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25);
  -moz-transition: stroke-dashoffset 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25), stroke-dasharray 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25);
  -o-transition: stroke-dashoffset 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25), stroke-dasharray 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25);
  -ms-transition: stroke-dashoffset 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25), stroke-dasharray 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25);
  transition: stroke-dashoffset 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25), stroke-dasharray 0.5s cubic-bezier(0.25, -0.25, 0.75, 1.25);
  stroke-width: 40px;
  stroke-linecap: round;
  stroke: #a06ba5;
  stroke-dashoffset: 0px;
}
path#top,
path#bottom {
  stroke-dasharray: 240px 950px;
}
path#middle {
  stroke-dasharray: 240px 240px;
}
.cross path#top,
.cross path#bottom {
  stroke-dashoffset: -650px;
  stroke-dashoffset: -650px;
}
.cross path#middle {
  stroke-dashoffset: -115px;
  stroke-dasharray: 1px 220px;
}

codePen可以查看编译后的CSS和JS:

clipboard.png

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏