扫码关注官方订阅号
我想要平滑地移动我的具有多种颜色的渐变,但问题是动画不平滑。它只是在每一步中改变位置。
你好
是否可以在不使用jQuery的情况下实现?
我的jsfiddle链接是 https://jsfiddle.net/bAUK6
动态实现Dave的答案:
:root{ --overlay-color-1: #ff0000; --overlay-color-2: #0000ff; --anim-duration: 2s; } #gradient { opacity: 0.8; background: none; } #gradient:after, #gradient:before { content: ''; display: block; position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #gradient:before { background: linear-gradient(135deg, var(--overlay-color-2) 0%, var(--overlay-color-1) 100%); animation: OpacityAnim var(--anim-duration) ease-in-out 0s infinite alternate; } #gradient:after { background: linear-gradient(135deg, var(--overlay-color-1) 0%, var(--overlay-color-2) 100%); animation: OpacityAnim var(--anim-duration) ease-in-out calc(-1 * var(--anim-duration)) infinite alternate; } @keyframes OpacityAnim { 0%{opacity: 1.0} 100%{opacity: 0.0} }
<div id="gradient"></div>
请尝试以下代码:
#gradient { height:300px; width:300px; border:1px solid black; font-size:30px; background: linear-gradient(130deg, #ff7e00, #ffffff, #5cff00); background-size: 200% 200%; -webkit-animation: Animation 5s ease infinite; -moz-animation: Animation 5s ease infinite; animation: Animation 5s ease infinite; } @-webkit-keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} } @-moz-keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} } @keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} }
<html> <div id="gradient"> Hello </div> </html>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
动态实现Dave的答案:
:root{ --overlay-color-1: #ff0000; --overlay-color-2: #0000ff; --anim-duration: 2s; } #gradient { opacity: 0.8; background: none; } #gradient:after, #gradient:before { content: ''; display: block; position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #gradient:before { background: linear-gradient(135deg, var(--overlay-color-2) 0%, var(--overlay-color-1) 100%); animation: OpacityAnim var(--anim-duration) ease-in-out 0s infinite alternate; } #gradient:after { background: linear-gradient(135deg, var(--overlay-color-1) 0%, var(--overlay-color-2) 100%); animation: OpacityAnim var(--anim-duration) ease-in-out calc(-1 * var(--anim-duration)) infinite alternate; } @keyframes OpacityAnim { 0%{opacity: 1.0} 100%{opacity: 0.0} }请尝试以下代码:
#gradient { height:300px; width:300px; border:1px solid black; font-size:30px; background: linear-gradient(130deg, #ff7e00, #ffffff, #5cff00); background-size: 200% 200%; -webkit-animation: Animation 5s ease infinite; -moz-animation: Animation 5s ease infinite; animation: Animation 5s ease infinite; } @-webkit-keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} } @-moz-keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} } @keyframes Animation { 0%{background-position:10% 0%} 50%{background-position:91% 100%} 100%{background-position:10% 0%} }