最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Setting linear-gradient offset as percentage without background-size - Stack Overflow

programmeradmin3浏览0评论

This looks simple and must be asked before, but I cannot find the question.

I have the following animation:

@keyframes bar-animation
{
    to
    {
        background-position-y: 20px;
    }
}

.bar
{
    width: 200px;
    height: 20px;
    background: linear-gradient(red, blue, red);
    animation: bar-animation 1s ease-in infinite;
}
<div class="bar"></div>

This looks simple and must be asked before, but I cannot find the question.

I have the following animation:

@keyframes bar-animation
{
    to
    {
        background-position-y: 20px;
    }
}

.bar
{
    width: 200px;
    height: 20px;
    background: linear-gradient(red, blue, red);
    animation: bar-animation 1s ease-in infinite;
}
<div class="bar"></div>

It works fine except the fact I should repeat 20px twice: when setting height of the div element and when setting the vertical offset (it must be divisible by the height to make the animation smooth).

Is there a way to make the class applicable to containers with different heights? Setting the offset as percentage requires background-size that I can't specify either.

Share Improve this question asked Feb 16 at 13:28 ShtoleShtole 3482 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use a repeating linear gradient to define your gradient. Define a height equal to 200% then you can easily slide it from top to bottom (or the opposite)

@keyframes bar-animation {
  to {
    background-position: top;
  }
}

.bar {
  height: 40px;
  background: 
    repeating-linear-gradient(red 0, blue, red 50%) 
    bottom/100% 200%;
  animation: bar-animation 1s ease-in infinite;
}
<div class="bar"></div>

发布评论

评论列表(0)

  1. 暂无评论