前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >赏析一段优秀的开源sass代码

赏析一段优秀的开源sass代码

作者头像
拿我格子衫来
发布2022-01-24 16:11:07
3800
发布2022-01-24 16:11:07
举报
文章被收录于专栏:TopFETopFE

cssshake 的核心代码赏析,使用多个sass的高级属性,混入,函数,变量,

代码语言:javascript
复制
// Variables
$prefix: 'shake' !default;
$trigger: 'shake-trigger' !default;

// Placeholders
%shake {
  display: inherit;
  transform-origin: center center;
}

%paused   { animation-play-state: paused; }
%running  { animation-play-state: running; }

@function apply-random($input) {
  @return if($input != 0, random($input) - $input/2, 0);
}

/// Do The Shake
/// @param {String}  $name ['shake']              - Name for the keyframes animation
/// @param {Number}  $h [5px]                     - Max number for random to assign in x axis
/// @param {Number}  $v [5px]                     - Max number for random to assign in y axis
/// @param {Number}  $r [3deg]                    - Max number for random rotation
/// @param {Number}  $dur [100ms]                 - animation-duration; valid time value
/// @param {Number}  $precision [.1]              - Precision of the keyframes animation (i.e 2 > 2%, 4%, 6%...)
/// @param {Boolean} $opacity [false]             - To apply random animation also in the opacity property
/// @param {String}  $q [infinite]                - animation-iteration-count; valid value
/// @param {String}  $t [ease-in-out]             - animation-timing-function; valid value
/// @param {Number}  $delay [null]                - animation-delay; valid time value
/// @param {Number}  $chunk [100%]                - Part of 100% where apply the animation
@mixin do-shake(
  $name: 'shake', 
  $h: 5px, 
  $v: 5px, 
  $r: 3deg, 
  $dur: 100ms, 
  $precision: .02, 
  $opacity: false, 
  $q: infinite, 
  $t: ease-in-out, 
  $delay: null,
  $chunk: 100%
  ) {  

  $rotate: 0;
  $move-x: 0;
  $move-y: 0;

  $h: if(unitless($h) and $h != 0, $h * 1px, $h);
  $v: if(unitless($v) and $v != 0, $v * 1px, $v);
  $r: if(unitless($r) and $r != 0, $r * 1deg, $r);

  // Keyframes
  @at-root {
    @keyframes #{$name} {
      $interval: if($precision > 0 and $precision < 1, 100 * $precision, 10);
      $step: $interval * 1%;

      @while $step < $chunk {
        $rotate: apply-random($r);
        $move-x: apply-random($h);
        $move-y: apply-random($v);
        
        #{$step} {
          transform: translate($move-x, $move-y) rotate($rotate);
          @if $opacity { opacity: random(100) / 100; }
        }
        
        $step: $step + $interval;
      }

      #{ if($chunk < 100%, (0%, $chunk, 100%), (0%, 100%)) } {
        transform: translate(0, 0) rotate(0);
      }

    }
  }

  @extend %shake;

  &:hover,
  .#{$trigger}:hover &,
  &.#{$prefix}-freeze,
  &.#{$prefix}-constant {
    @if $delay { animation-delay: $delay; }
    animation-name: #{$name};
    animation-duration: $dur;
    animation-timing-function: $t;
    animation-iteration-count: $q;
  }

  &:hover,
  .#{$trigger}:hover & { @extend %running; }

}

.#{$prefix}-freeze,
.#{$prefix}-constant.#{$prefix}-constant--hover:hover,
.#{$trigger}:hover .#{$prefix}-constant.#{$prefix}-constant--hover {
  @extend %paused;
}

.#{$prefix}-freeze:hover,
.#{$trigger}:hover .#{$prefix}-freeze { @extend %running; }

地址 csshake/scss/_tools.scss

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
流计算 Oceanus
流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的企业级实时大数据分析平台,具备一站开发、无缝连接、亚秒延时、低廉成本、安全稳定等特点。流计算 Oceanus 以实现企业数据价值最大化为目标,加速企业实时化数字化的建设进程。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档