scss function for animation keyframes

旧城冷巷雨未停 提交于 2019-12-21 20:33:40

问题


i would like to do a reusable sass function that makes it easy for me to write a @keyframes css animation without writing too much code but I'm not sure how to go about it, especially the math part.

i have a div containing a single background image with this css

height: 100vh;
width: 8000px;

this background image is composed of 25 frames. so each frame is 320px wide.

the animation should translateX() the div by multiple of 320px every 4%, so something like this:

@keyframes animation {
    0% {
        transform:translateX(0);
    }
    4% {
        transform:translateX(-320px);
    }
    8% {
        transform:translateX(-640px);
    }

    ...
}

i would like to make a function of this but I'm new to scss functions and i really don't know where to start. if someone could give me a hint that would be great!

thanks


回答1:


Could be better (modularise) but here is for you;

@mixin deneme($i){
  @for $i from 0 through 100/$i {
    #{$i * 4}% {
      transform:translateX(#{-320 * $i});
    } 
  }
}

UPDATE:

I think this one is a bit better.

@mixin deneme($increase, $angle){
  @for $x from 0 through 100/$increase {
    #{$x * $increase }% {
      transform:translateX($angle * $x);
    } 
  }
}


来源:https://stackoverflow.com/questions/45350940/scss-function-for-animation-keyframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!