美文网首页
使用SCSS做shake特效的按钮

使用SCSS做shake特效的按钮

作者: percykuang | 来源:发表于2020-01-05 19:38 被阅读0次

    index.html结构

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <link rel="stylesheet" href="style.scss" />
      </head>
      <body>
        <div class="buttonWrapper">
          <button>第一个按钮</button>
          <button>第二个按钮</button>
        </div>
      </body>
    </html>
    

    index.scss源码

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    
    @mixin buttonWithShadow($color) {
      background: $color;
      box-shadow: 0px 5px 0px 0px darken($color, 15%);
    }
    
    .buttonWrapper {
      button {
        padding: 10px 20px;
        font-size: 20px;
        border-radius: 4px;
        border: none;
        color: white;
        margin: 0 40px;
        &:first-child {
          $blue: #55acee;
          @include buttonWithShadow($blue);
          &:hover {
            animation: x .5s;
          }
        }
        &:nth-child(2) {
          $green: #2ecc5e;
          @include buttonWithShadow($green);
          &:hover {
            animation: y .5s;
          }
        }
      }
    }
    
    $n1: 10%;
    $n2: 20%;
    $step: 25%;
    
    @keyframes x {
      @for $i from 0 to 4 {
        #{$i * $step} {
          @if $i % 2 == 0 {
            transform: translateX(-$n1);
          }@else{
            transform: translateX($n1);
          }
        }
      }
      100% {
        transform: translateX(0);
      }
    }
    
    @keyframes y {
      @for $i from 0 to 4 {
        #{$i * $step} {
          @if $i % 2 == 0 {
            transform: translateY(-$n2);
          }@else{
            transform: translateY($n2);
          }
        }
      }
      100% {
        transform: translateY(0);
      }
    }
    

    使用parcel工具打包运行后的效果

    shake.gif

    相关文章

      网友评论

          本文标题:使用SCSS做shake特效的按钮

          本文链接:https://www.haomeiwen.com/subject/mzscactx.html