css - 背景位置不适用于 CSS 动画和线性渐变

标签 css animation css-animations

我正在尝试使用 CSS3(渐变和动画)制作类似于 iphone 幻灯片的动画来解锁渐变。但是,出于某种原因,背景位置仅在我使用静态像素数时才有效,而在我使用百分比时无效。理想情况下,我可以将此类应用于任何元素,并让动画工作,而无需针对特定宽度进行编码。有人可以指出我正确的方向吗?

JSFiddle:
https://jsfiddle.net/ekLamtbL/

.slideToUnlock {
  background: linear-gradient(-90deg, black 0%, gray 40%, white 45%, gray 55%, black 60%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  color: transparent;
    -webkit-animation: slidetounlock 5s infinite;
    animation: slidetounlock 5s infinite;
    -webkit-animation-direction: normal;
    animation-direction: normal;
}

@-webkit-keyframes slidetounlock {
  0% {
    background-position: -100% 0;
  }
  100% {
    background-position: 100% 0;
  }
}

最佳答案

添加了您的代码

background-size: 250% 250%;

例子
.slideToUnlock {
  background: linear-gradient(-90deg, black 0%, gray 40%, white 45%, gray 55%, black 60%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  color: transparent;
  -webkit-animation: slidetounlock 5s infinite;
  animation: slidetounlock 5s infinite;
  -webkit-animation-direction: normal;
  animation-direction: normal;
  background-size: 250% 250%;
}

https://jsfiddle.net/ekLamtbL/2/

关于css - 背景位置不适用于 CSS 动画和线性渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612631/

相关文章:

html - float 图像的CSS对齐

javascript - 随着内容大小的变化,以动画方式重新定位左浮动 DIV

html - 如何围绕圆边旋转对象(div)?仅 CSS

css - lessc 无论如何语法错误

android - 如何处理分辨率相同但屏幕高度不同的 Android 设备

css - 带有 CSS3 动画的 Twitter Bootstrap 正文背景

javascript - 将 tweenMax 导入 javascript

objective-c - 使用推送转换交换 NSView

html - 汉堡叠加菜单动画

html - 我是否必须为每个要在该类下更改的元素重新键入类?