【问题标题】:How to make background-image scroll continuously horizontally without a break in animation?如何使背景图像水平连续滚动而不中断动画?
【发布时间】:2020-10-19 22:17:24
【问题描述】:

我正在尝试使用 css3 动画制作一个无限横向滚动的横幅。问题是动画结束后重新启动时会出现严重的剪辑。我正试图弄清楚如何防止那种刺耳的动画。

我已经把我的代码放在这里了。

@keyframes slideleft {
from{background-position: right;}
to {background-position: left;}
}

@-webkit-keyframes slideleft {
from{background-position: right;}
to {background-position: left;}
}

#masthead {
background-image: url('http://static.communitytable.parade.com/wp-content/uploads/2014/06/dogs-in-world-cup-jerseys-ftr.jpg');
animation: slideleft 5s infinite ease-in-out;
-webkit-animation: slideleft 5s infinite ease-in-out;
width: 100%;
height: 1200px;
}
<div id="masthead"></div>

【问题讨论】:

  • 最后,我想限制 javascript 的使用,因为页面加载时间已经很长了。不过我并不反对。

标签: html css css-animations


【解决方案1】:

JavaScript 可能是处理这个问题的更好方法。尽管在 CSS 中,您可以重复背景图像并将背景位置和动画持续时间延长到一个非常高的数字。这是fiddle

@keyframes slideleft {
  from { background-position: 0%; }
  to { background-position: 90000%; }
}

#masthead {
  background-repeat: repeat-x;
  ...
  animation: slideleft 600s infinite linear;
}

如果您使用的是 jQuery,那将相当简单:

(function animateBG() {
    $('#masthead').animate({
        backgroundPosition: '+=5'
    }, 12, animateBG);
})();

@keyframes slideleft {
  from { background-position: 0%; }
  to { background-position: 90000%; }
}

#masthead {
  background-image: url('https://i.stack.imgur.com/TE4UI.jpg');
  background-repeat: repeat-x;
  animation: slideleft 600s infinite linear;
  -webkit-animation: slideleft 600s infinite linear;
  width: 100%;
  height: 1200px;
}
<div id="masthead"></div>

【讨论】:

  • 谢谢你,这很有帮助!
猜你喜欢
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2018-02-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多