【问题标题】:Ticker/Marquee Effect with No Gaps无间隙的收录器/选框效果
【发布时间】:2020-12-30 20:26:19
【问题描述】:

我一直在尝试弄清楚如何使用 No Gaps 获得类似字幕/自动收录器的效果。 我找到了一个使用这种技术的网站https://www.artpharmacy.com.au/(页面底部,“购买艾米莉亚的新书”)。

我试图复制他们所拥有的,因为这正是我想要的效果。

我相信他们实现这一点的方式是创建两个 span(每个 span 中的文本相同),然后使用 CSS 动画。如果有人可以解释这是如何工作的,那就太好了——我复制了代码,但我想了解它背后的逻辑。

  1. 我相信它首先会循环遍历第一个跨度的文本。然后我短暂地看到第二个跨度文本出现(我使用背景颜色:橙色来查看第二个跨度何时在动画中运行。)但是我看到在第二个跨度文本出现后,几秒钟,其他跨度标签的文本再次开始运行?两个 span 的动画是否同时发生?还是第一个跨度的动画先发生,然后是第二个跨度的动画?

我遇到的主要问题是:

  1. 我的 iphone (Safari) 上的动画很慢。我不知道为什么会这样?

  2. 有时动画看起来很生涩或“闪光”。我使用了线性和无限属性,不知道还有什么办法可以避免在浏览器之间造成这种不一致?

我还在滚动文本上使用混合混合模式,想知道它们是否也会导致奇怪的行为。 (注意:div 有一个背景图片,但我不知道如何在 sn-p 中添加它,所以只使用了 background-color:purple)

代码如下:

function makeMarquee() {
  const title = 'Event: January 1-2, 2021, Zoom'

  //use Array constructor to create an empty list with a length of 50 that is filled with the title.
  //We can join all the contents of array using dash
  const marqueeText = new Array(10).fill(title).join(' -- ')
  //query Selector same as $ jquery, grab the span tags
  const marquee = document.querySelector('.marquee span')

  //set the text of span to be the marqueeText
  marquee.innerHTML = marqueeText
  // setTimeout(function () {marquee.style.animationName = 'moveLeft';},1000);

  //create a clone of the div
  var clone = marquee.cloneNode(true)
  //add a class to this new div
  clone.classList.add('marquee2')
  //insert this new div after the first one
  marquee.after(clone)
}

makeMarquee()
.section {
  /*each section will take 100% of the height of browser */
  min-height: 100vh;
  /* Will help to vertically align container box */
  display: flex;
  position: relative;
}


/* Provide padding to left and right of section */

.section-pad {
  padding-left: 5vw;
  padding-right: 5vw;
}

.container {
  /* Take the width of widest content box */
  max-width: 780px;
  /* Center our box horizontally and vertically using flex on .section */
  margin: auto;
}

.intro {
  background-image: url("intro-bg-min.png");
  color: #fff;
  background-color: purple;
}

.intro p {
  font-size: 36px;
  line-height: 1.2;
}


/* Utility class to center and cover background images */

.bg-cover {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}


.mix-difference {
  /* our mix blend mode allows us to mix the current layer's style/colors with that of what's behind it to create some really cool effects */
  mix-blend-mode: difference;
}

.marquee {
  position: absolute;
  top: 3vh;
  left: 0;
  width: 100%;
  /*Each letter will be 5% of viewport width */
  font-size: 5vw;
  /* As tall as text */
  line-height: 1;
  text-transform: uppercase;
  /*no scrollbars */
  overflow: hidden;
  white-space: nowrap;
  /* Our span is inline by default, so change it to block */
}

.marquee span {
  display: inline-block;
  -webkit-animation: marquee 90s infinite linear;
  animation: marquee 90s infinite linear;
}

@-webkit-keyframes marquee {
  0% {
    -webkit-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    -webkit-transform: translateX(-100%);
    transform: translateX(-100%);
  }
}

@keyframes marquee {
  0% {
    /*  -webkit-transform: translateX(0);
         transform: translateX(0); */
    transform: translate(0, 0);
  }
  100% {
    /*  -webkit-transform: translateX(-100%);
             transform: translateX(-100%); */
    transform: translate(-100%, 0);
  }
}


/* give the span a margin left equal to one width of letter */

.marquee2 {
  margin-left: 5vw;
  animation-delay: 45s;
  background-color: orange;
}


/* Before the span give it a dash to make it flow with the existing content */

.marquee2::before {
  content: " -- ";
}
<section class="intro section section-pad bg-cover" id="intro">

  <div class="copy container">

    <div class="marquee mix-difference">
      <!-- Here we add the title in multiple repeating times using javascript -->
      <span>Event: January 1-2, 2020, Zoom</span>
    </div>

    <div class="mix-difference ">

      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tristique tellus finibus velit bibendum pulvinar. Pellentesque id congue tellus. Donec a urna at tortor sagittis vulputate sed id turpis. Donec a urna at tortor sagittis vulputate sed id turpis.
      </p>
    </div>
  </div>
  <a href="#day-1" class="js-scroll scroll-to scroll-to-intro"></a>
</section>

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    看看这段代码。我试图让它变得更简单,只是使用 css。

    我添加了另一个循环来解决空白问题。让我们看看你的想法。

    https://codepen.io/bertofern/details/xxEpQGM

    body { margin: 0; }
    
    .ticker-wrap {
      width: 100%;
      height: 80px;
      margin: 0 auto;
      overflow: hidden;
      white-space: nowrap;
      position: fixed;
      bottom: 0;
      height: 3.5rem;
      background-color: #FFCA46; 
    }
    .ticker {
      display: inline-block;
      margin-top: 5px;
      animation: marquee 20s linear infinite;
    }
    .item-collection-1 {
      position: relative;
      left: 0%;
      animation: swap 20s linear infinite;
    }
    
    .item {
      display: inline-block;
      padding: 0 1rem;
      font-size: 2rem;
      color: black;   
      font-weight: 800;
      font-family: sans-serif;
    }
    
    /* Transition */
    @keyframes marquee {
      0% {
        -webkit-transform: translateX(0);
        transform: translateX(0);
      }
      100% {
        -webkit-transform: translateX(-100%);
        transform: translateX(-100%);
      }
    }
    
    @keyframes swap {
      0%, 50% {
        left: 0%;
      }
      50.01%,
      100% {
        left: 100%;
      }
    }
    <div class="ticker-wrap">
      <div class="ticker">
        <span class="item-collection-1">
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
        </span>
        <span class="item-collection-2">
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
          <span class="item">Buy Emilya's New Book - Out Now! ➜</span>
        </span>
      </div>
    <div>

    我不做任何逻辑,我只是​​为每种类型的动画使用代码。为了更好地理解它的工作原理,我建议您查看有关动画和关键帧的页面: https://www.w3schools.com/css/css3_animations.asp

    https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes

    【讨论】:

    • 非常感谢。有没有办法让它在第三次重复文本后没有空格。有一个间隙,直到动画再次开始。
    • 我已编辑帖子以修复空白问题。让我们看看你的想法。
    • 这看起来很棒!太感谢了!如果你不介意,你能解释一下你这样做的逻辑吗?我试图理解这部分:@keyframes swap { 0%, 50% { left: 0%; } 50.01%, 100% { 左:100%; } } 以及如何创建两个 div,每个 div 有三个 span 标签。
    • 也为了可访问性——除了第一个跨度之外,我会隐藏屏幕阅读器的所有跨度吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多