CSS3 动画 - 当 div 的宽度改变时图像保持移动

标签 css animation

我正在尝试模仿以下网站:http://shiz.co/http://www.maison-vignaux.com/work

图像显示的方式,就像它们没有移动一样,但更多的图像会在一段时间内显示。我想要这种类型的动画。现在,我的图像移动了,而不是像上面的网站那样显示更多。

我不知道如何完成这个。

这是我的 fiddle :https://jsfiddle.net/z7ukk6kb/ (忽略动画名称)

编辑:问题是 div 上的背景位置。现在它做我想做的事。

 <div class="parallax-elem">
   <div class="img"></div>
 </div>

$('.img').addClass('slide-top');

我的 CSS:

.slide-top {
  -webkit-animation: slide-top 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
          animation: slide-top 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}

@keyframes slide-top {
  0% {
      width:0;
  }
  100% {
      width:100%;
  }
}

.parallax-elem {
  overflow:hidden;
  position: absolute;
  height:600px;
  width:100%;
}

.parallax-elem:after {
  content:"";
  background-color:#eee;
  width:100%;
  height:100%;
  top:0;
  left:0;
  right:0;
  position: absolute;
  z-index:10;
}

.img {
  background:url('http://media.nj.com/entertainment_impact_dining/photo/coffee-stock-photo-0e8b300f42157b6f.jpg') no-repeat;
  background-size: cover;
  background-position: center center;
  position: relative;
  height: 100%;
  z-index:11;
    width: 100%;
}

最佳答案

尝试从 .img 中删除 background-position

由于您设置了 background-position: center center,随着 div 的宽度在动画期间增加,背景图像会不断调整以保持居中。这就是它不断移动的原因。

$('.img').addClass('slide-top');
.slide-top {
          animation: slide-top 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both infinite;
}


@keyframes slide-top {
  0% {
            width:0;
  }
  100% {
            width:100%;
  }
}

body {
  max-width:800px;
	margin:0 auto;
	position:relative;
}

.parallax-elem {
  overflow:hidden;
  position: absolute;
  height:600px;
  width:100%;
}

.parallax-elem:after {
  content:"";
  background-color:#eee;
  width:100%;
  height:100%;
  top:0;
  left:0;
  right:0;
  position: absolute;
  z-index:10;
}

.img {
  background:url('http://media.nj.com/entertainment_impact_dining/photo/coffee-stock-photo-0e8b300f42157b6f.jpg') no-repeat;
  background-size: cover;
  position: relative;
  height: 100%;
  z-index:11;
    width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parallax-elem">
    <div class="img"></div>
</div>

关于CSS3 动画 - 当 div 的宽度改变时图像保持移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732711/

相关文章:

html - 我无法使用纯 css 检索 flex 的边框效果

javascript - 更改标签颜色

c# - 如何在 C# 中设置 Storyboard.TargetProperty?

ios - 创建 WatchKit PNG 动画

javascript - 使用 JS mousemove 性能更改 CSS 属性

java - 无限旋转的 ObjectAnimator 卡顿

jquery - 无法使用 jQuery 和 CSS3 正确交叉淡入淡出图像?

css - 媒体查询仅使用最大宽度

css 背景位置动画无限

html - 我可以使用 CSS/HTML 重新排序还是需要 JavaScript?