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

举报
拿我格子衫来 发表于 2022/03/18 00:20:23 2022/03/18
【摘要】  cssshake 的核心代码赏析,使用多个sass的高级属性,混入,函数,变量, // Variables$prefix: 'shake' !default;$trigger: 'shake-trigger' !default; // Placeholders%shake { display: inherit; tra...

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


  
  1. // Variables
  2. $prefix: 'shake' !default;
  3. $trigger: 'shake-trigger' !default;
  4. // Placeholders
  5. %shake {
  6. display: inherit;
  7. transform-origin: center center;
  8. }
  9. %paused { animation-play-state: paused; }
  10. %running { animation-play-state: running; }
  11. @function apply-random($input) {
  12. @return if($input != 0, random($input) - $input/2, 0);
  13. }
  14. /// Do The Shake
  15. /// @param {String} $name ['shake'] - Name for the keyframes animation
  16. /// @param {Number} $h [5px] - Max number for random to assign in x axis
  17. /// @param {Number} $v [5px] - Max number for random to assign in y axis
  18. /// @param {Number} $r [3deg] - Max number for random rotation
  19. /// @param {Number} $dur [100ms] - animation-duration; valid time value
  20. /// @param {Number} $precision [.1] - Precision of the keyframes animation (i.e 2 > 2%, 4%, 6%...)
  21. /// @param {Boolean} $opacity [false] - To apply random animation also in the opacity property
  22. /// @param {String} $q [infinite] - animation-iteration-count; valid value
  23. /// @param {String} $t [ease-in-out] - animation-timing-function; valid value
  24. /// @param {Number} $delay [null] - animation-delay; valid time value
  25. /// @param {Number} $chunk [100%] - Part of 100% where apply the animation
  26. @mixin do-shake(
  27. $name: 'shake',
  28. $h: 5px,
  29. $v: 5px,
  30. $r: 3deg,
  31. $dur: 100ms,
  32. $precision: .02,
  33. $opacity: false,
  34. $q: infinite,
  35. $t: ease-in-out,
  36. $delay: null,
  37. $chunk: 100%
  38. ) {
  39. $rotate: 0;
  40. $move-x: 0;
  41. $move-y: 0;
  42. $h: if(unitless($h) and $h != 0, $h * 1px, $h);
  43. $v: if(unitless($v) and $v != 0, $v * 1px, $v);
  44. $r: if(unitless($r) and $r != 0, $r * 1deg, $r);
  45. // Keyframes
  46. @at-root {
  47. @keyframes #{$name} {
  48. $interval: if($precision > 0 and $precision < 1, 100 * $precision, 10);
  49. $step: $interval * 1%;
  50. @while $step < $chunk {
  51. $rotate: apply-random($r);
  52. $move-x: apply-random($h);
  53. $move-y: apply-random($v);
  54. #{$step} {
  55. transform: translate($move-x, $move-y) rotate($rotate);
  56. @if $opacity { opacity: random(100) / 100; }
  57. }
  58. $step: $step + $interval;
  59. }
  60. #{ if($chunk < 100%, (0%, $chunk, 100%), (0%, 100%)) } {
  61. transform: translate(0, 0) rotate(0);
  62. }
  63. }
  64. }
  65. @extend %shake;
  66. &:hover,
  67. .#{$trigger}:hover &,
  68. &.#{$prefix}-freeze,
  69. &.#{$prefix}-constant {
  70. @if $delay { animation-delay: $delay; }
  71. animation-name: #{$name};
  72. animation-duration: $dur;
  73. animation-timing-function: $t;
  74. animation-iteration-count: $q;
  75. }
  76. &:hover,
  77. .#{$trigger}:hover & { @extend %running; }
  78. }
  79. .#{$prefix}-freeze,
  80. .#{$prefix}-constant.#{$prefix}-constant--hover:hover,
  81. .#{$trigger}:hover .#{$prefix}-constant.#{$prefix}-constant--hover {
  82. @extend %paused;
  83. }
  84. .#{$prefix}-freeze:hover,
  85. .#{$trigger}:hover .#{$prefix}-freeze { @extend %running; }

 

 

地址 csshake/scss/_tools.scss

文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。

原文链接:fizzz.blog.csdn.net/article/details/109669135

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。