简体   繁体   English

如何在缩放线性渐变 animation 时将其居中?

[英]How do I center the linear gradient animation as it zooms it?

I'm learning how to animate radial gradients by changing their background-positioning and the size of their elements.我正在学习如何通过更改它们的背景定位和元素的大小来为径向渐变设置动画。 I've tried using flexbox to vertically and horizontally align.anim2 inside of.animcontain, as well as setting padding and margins, but nothing seems to be working.我尝试使用 flexbox 在 .animcontain 中垂直和水平对齐 .anim2,以及设置填充和边距,但似乎没有任何效果。 How do I go about zooming into the center of the radial gradient background without squishing it?我如何 go 关于放大到径向渐变背景的中心而不挤压它?

 body { background-color: black; color: white; margin: 0; }.align { display: flex; flex-direction: column; height: 100vh; width: 100vw; justify-content: center; align-items: center; }.container { display: grid; grid-template-columns: repeat(3, minmax(80px, 20vmin)); grid-auto-rows: minmax(80px, 20vmin); grid-gap: 10px; }.container > div { border: 2px solid white; } @keyframes anim1 { 0% { background-position: 0 0; } 100% { background-position: 0 100%; } }.anim1 { animation: anim1 5s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; background-size: 100% 3000%; background-image: linear-gradient( to bottom, red, orange, yellow, green, blue, indigo, violet, indigo, blue, green, yellow, orange, red ); border-radius: 10px; } @keyframes anim2 { 0% { width: 100%; height: 100%; } 100% { width: 1000%; height: 1000%; } }.animcontain { overflow: hidden; border-radius: 50%; }.anim2 { background-image: radial-gradient( red, orange, yellow, green, blue, indigo, violet ); animation: anim2 3s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; }
 <!DOCTYPE html> <html> <head> </head> <body> <div class="align"> <div class="container"> <div class="anim1"></div> <div class="animcontain"> <div class="anim2"></div> </div> <div class="anim3"></div> </div> </div> </body> </html>

Don't animate width/height, animate the background-size不要为宽度/高度设置动画,为background-size设置动画

 body { background-color: black; color: white; margin: 0; }.align { display: flex; flex-direction: column; height: 100vh; width: 100vw; justify-content: center; align-items: center; }.container { display: grid; grid-template-columns: repeat(3, minmax(80px, 20vmin)); grid-auto-rows: minmax(80px, 20vmin); grid-gap: 10px; }.container > div { border: 2px solid white; } @keyframes anim1 { 0% { background-position: 0 0; } 100% { background-position: 0 100%; } }.anim1 { animation: anim1 5s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; background-size: 100% 3000%; background-image: linear-gradient( to bottom, red, orange, yellow, green, blue, indigo, violet, indigo, blue, green, yellow, orange, red ); border-radius: 10px; } @keyframes anim2 { 100% { background-size: 1000% 1000%; } }.animcontain { overflow: hidden; border-radius: 50%; background-image: radial-gradient( red, orange, yellow, green, blue, indigo, violet ); background-size: 100% 100%; background-position:center; animation: anim2 3s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; }
 <div class="align"> <div class="container"> <div class="anim1"></div> <div class="animcontain"> </div> <div class="anim3"></div> </div> </div>

To center anim2 you just have to add display grid with place-items center in animcontain class:要使 anim2 居中,您只需在 animcontain class 中添加带有位置项目中心的显示网格:

 body { background-color: black; color: white; margin: 0; }.align { display: flex; flex-direction: column; height: 100vh; width: 100vw; justify-content: center; align-items: center; }.container { display: grid; grid-template-columns: repeat(3, minmax(80px, 20vmin)); grid-auto-rows: minmax(80px, 20vmin); grid-gap: 10px; }.container>div { border: 2px solid white; } @keyframes anim1 { 0% { background-position: 0 0; } 100% { background-position: 0 100%; } }.anim1 { animation: anim1 5s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; background-size: 100% 3000%; background-image: linear-gradient(to bottom, red, orange, yellow, green, blue, indigo, violet, indigo, blue, green, yellow, orange, red); border-radius: 10px; } @keyframes anim2 { 0% { width: 100%; height: 100%; } 100% { width: 1000%; height: 1000%; } }.animcontain { overflow: hidden; display: grid; place-items: center; border-radius: 50%; }.anim2 { background-image: radial-gradient(red, orange, yellow, green, blue, indigo, violet); animation: anim2 3s; animation-iteration-count: infinite; animation-timing-function: ease-in-out; animation-direction: alternate; }
 <div class="align"> <div class="container"> <div class="anim1"></div> <div class="animcontain"> <div class="anim2"></div> </div> <div class="anim3"></div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM