交换类时保持CSS transform3d的位置

时间:2018-11-24 00:23:01

标签: css css-animations translate3d

我在div中有一个无限滚动的背景图片,如下面的代码片段所示。

其背后的想法是它是我可以更改速度的时间表,即正常速度,快,慢甚至反向。使用javascript,我可以成功更改div上的“速度类”,并且图像将成功更新为希望的速度和方向。

但是我的问题是,当我改变班级时,我失去了图像的相对translate3d位置,因此,随着班级的变化,背景图像的位置将被重置,然后新的动画开始。

我试图从css的角度纯粹通过尝试类的完全交换来解决此问题,尝试全班运行单个类并将选定的动画用作附加类,但是我我无法弄清楚应用新类时如何保持相对背景位置。

交换类时如何保持背景图像的位置,以免图像看起来从一个位置跳到另一个位置?

.timeline_frame {
  width: 100%;
  min-width: 1500px;
  background-color: #444;
  height: 250px;
  left: 0;
  right: 0;
  margin: auto auto;
  position: relative;
}

.timeline {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto auto;
  width: 1500px;
  height: 200px;
}

.timeline_container {
  width: 1500px;
  height: 200px;
  overflow: hidden;
  position: relative;
}

.timeline_background {
  background: url("https://www.aidanwardman.com/timeline-bg.png") repeat-x;
  background-color: rgba(0, 0, 0, 0.5);
  width: 3000px;
  height: 200px;
  vertical-align: bottom;
}

.timeline_speed_normal {
  animation: slide 20s linear infinite;
}

.timeline_speed_slow {
  animation: slide 40s linear infinite;
}

.timeline_speed_reverse {
  animation: slide-reverse 2s linear infinite;
}

@keyframes slide {
  0% {
    transform: translate3d(0, 0, 0);
  }
  100% {
    transform: translate3d(-1500px, 0, 0);
  }
}

@keyframes slide-reverse {
  0% {
    transform: translate3d(-1500px, 0, 0);
  }
  100% {
    transform: translate3d(0, 0, 0);
  }
}

.button_frame {
  left: 0;
  right: 0;
  margin: auto;
  width: 400px;
  padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button_frame">
  <button id="forward-normal">Forward Normal</button>
  <button id="forward-slow">Forward Slow</button>
  <button id="reverse">Reverse</button>
</div>
<div class="timeline_frame">
  <div class="timeline">
    <div class="timeline_container">
      <div class="timeline_background timeline_speed_normal"></div>
    </div>
  </div>
</div>

<script>
  $("#forward-normal").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_reverse").addClass("timeline_speed_normal");
  });
  $("#forward-slow").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_normal timeline_speed_reverse").addClass("timeline_speed_slow");
  });
  $("#reverse").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_normal").addClass("timeline_speed_reverse");
  });
</script>

1 个答案:

答案 0 :(得分:2)

我会使用过渡而不是动画来模拟这样的事情:

.timeline_frame {
  width: 100%;
  min-width: 1500px;
  background-color: #444;
  height: 250px;
  left: 0;
  right: 0;
  margin: auto auto;
  position: relative;
}

.timeline {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto auto;
  width: 1500px;
  height: 200px;
}

.timeline_container {
  width: 1500px;
  height: 200px;
  overflow: hidden;
  position: relative;
}

.timeline_background {
  background: url("https://www.aidanwardman.com/timeline-bg.png") repeat-x;
  background-color: rgba(0, 0, 0, 0.5);
  width: 3000px;
  height: 200px;
  vertical-align: bottom;
  transition:10s all;
}

.timeline_speed_normal {
  transform: translate3d(-1500px, 0, 0);
}

.timeline_speed_slow {
  transition:30s all;
  transform: translate3d(-1501px, 0, 0);
}

.timeline_speed_reverse {
  transform: translate3d(0, 0, 0);
}

.button_frame {
  left: 0;
  right: 0;
  margin: auto;
  width: 400px;
  padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button_frame">
  <button id="forward-normal">Forward Normal</button>
  <button id="forward-slow">Forward Slow</button>
  <button id="reverse">Reverse</button>
</div>
<div class="timeline_frame">
  <div class="timeline">
    <div class="timeline_container">
      <div class="timeline_background"></div>
    </div>
  </div>
</div>

<script>
  $("#forward-normal").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_reverse").addClass("timeline_speed_normal");
  });
  $("#forward-slow").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_normal timeline_speed_reverse").addClass("timeline_speed_slow");
  });
  $("#reverse").click(function() {
    $(".timeline_background").removeClass("timeline_speed_fast timeline_speed_slow timeline_speed_normal").addClass("timeline_speed_reverse");
  });
</script>