0

I have a parent div the width of the screen with a set height. I want to animate smaller boxes of equal length inside it. I want to 'slide' the color of the smaller boxes with single a gradient for the parent div. This is one way I have implemented it with every box being a div with variable width:

tele

It has issues with mobile display so I need a new solution. I know that animating gradients with background position is possible, but what about animating portions of a gradient at a time.

The final version of this would include about 100 smaller boxes inside this div.I am looking for advice so if this makes more sense to just do in a canvas please let me know.

4
  • you cannot, but you have background-size that works . a mixed of bg position/size/repeat or multiple gradient should do Commented Mar 17, 2016 at 14:52
  • You can't animate gradients like that. Use child divs or canvas. Commented Mar 17, 2016 at 14:54
  • This question is either too broad, opinion based or requires discussion and so is off-topic for Stack Overflow. If you have a specific, answerable, programming issue, please provide full details. Commented Mar 17, 2016 at 14:56
  • here is my idea of size/position/repeat codepen.io/anon/pen/jqyGYP Commented Mar 17, 2016 at 14:59

2 Answers 2

1

You could do it with each box being an individual gradient and then animate that gradient's background-size. However, I wouldn't recommend doing this. Animating 100 gradients is bound to be pretty bad for performance. As is animating the width of 100 elements (animating a scale() transform for each div would be better in that case).

If canvas is an option, then yes, use canvas, it's a much better choice in this case, both from a maintainability and from a performance point of view.

Sign up to request clarification or add additional context in comments.

1 Comment

If there are say 20 rows that each contain one of these animated divs, would using canvas still be manageable for mobile devices? Only one would animate at a time, but they all need to be rendered on the same page
0

If the boxes are equal length, you can animate just the main background, using steps: (hover the div)

.test {
  width: 600px;
  height: 50px;
  background-image: linear-gradient(90deg, yellow 50%, green 50%);
  background-size: 200% 100%;
  background-position: 0% 0;
  transition: background-position 40s steps(100);
}
.test:hover {
  background-position: -100% 0;
}
<div class="test"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.