代码之家  ›  专栏  ›  技术社区  ›  mipurale

CSS关键帧在100%translate tx设置为-100%的情况下没有完全离开屏幕

  •  0
  • mipurale  · 技术社区  · 6 年前

    // Sample items to add to the news-ticker
    let sampleNewsItems = [
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
      "Vivamus enim magna, elementum in consectetur sit amet, porta ut eros"
    ];
    
    // Iterate through the sample news items and process accordingly
    let nextTimeoutDur;
    for (let x = 0; x < sampleNewsItems.length; x++) {
      let screenWidth = $(window).innerWidth();
      $('.ticker-wrap').width(screenWidth);
    
      $(".ticker-wrap").append('<div class="ticker-item" data-index="' + x + '"><span class="tickerContent"></span></div>');
      $('.ticker-item[data-index="' + x + '"] .tickerContent').html(sampleNewsItems[x]);
    
      let newsItemLength = 500;
      $('.ticker-item[data-index="' + (x - 1) + '"]').outerWidth(500);
    
      let tickerDur = 7;
      nextTimeoutDur = x > 0 ? (tickerDur * newsItemLength) / screenWidth : 0;
    
      $(".ticker-item[data-index=" + x + "]").css("animation", "ticker " + tickerDur + "s  linear " + nextTimeoutDur + "s  infinite");
    }
    @keyframes ticker {
      0% {
        transform: translate3d(100vw, 0, 0);
        visibility: visible;
      }
      100% {
        transform: translate3d(-100%, 0, 0);
      }
    }
    
    .ticker-wrap {
      width: 100%;
    }
    
    .ticker-wrap .ticker-item {
      display: inline-block;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="ticker-wrap"></div>

    我希望在第二个ticker项消失之前将其完全从视窗中移除,这就是为什么我设置 transform: translate3d(-100%, 0, 0);

    Here is a CodePen with my code.

    https://codepen.io/prizzi13/pen/JwGmGE

    1 回复  |  直到 6 年前
        1
  •  0
  •   mipurale    6 年前

    position 属于 .ticker-item absolute