【问题标题】:Maintaining a CSS translate3d position when swapping classes交换类时保持 CSS translate3d 位置
【发布时间】:2018-11-24 00:23:00
【问题描述】:

我在 div 中有一个无限滚动的背景图像,如下面的 sn-p 所示。

这背后的想法是,它是一个我可以改变速度的时间线,即。正常速度,快,慢,甚至倒车。使用 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>

【问题讨论】:

    标签: css css-animations translate3d


    【解决方案1】:

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

    .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>

    【讨论】:

    • 我喜欢它,我将如何“无限期地”或重复地模拟这样的事情,以便无论选择哪个速度/方向,它看起来都很流畅?例如:1. 正向正常速度 - 无限(平滑重放过渡) 2. 切换到正向慢速或反向正常(保持无限滚动),平滑过渡
    • @Aidan 是的,重复部分在过渡时有点棘手,我正在考虑一种方法
    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2020-09-24
    • 2021-04-17
    相关资源
    最近更新 更多