Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="contet">
  <div class="inner">
    <a class="ticker js-ticker" href="https://tone-design.net/" target="_blank">
      <span class="ticker__text js-ticker-text">自動無限ループで一定方向にスクロールするコンテンツの実装のメモ</span>
    </a>
  </div>
</div>
              
            
!

CSS

              
                :root {
  --black: #1b1b1b;
  --white: #fff;
  --yellow: #ffd500;
}

body {
  background-color: var(--black);
  font-family: arial, sans-serif;
}

.contet {
  display: grid;
  place-items: center;
  height: 100vh;
}
.contet .ticker {
  margin: 20px 0;
}
.contet .ticker:nth-child(3) {
  width: 400px;
}

.ticker {
  position: relative;
  display: block;
  width: 300px;
  max-width: 100vw;
  height: 60px;
  background-color: #ffd500;
  border-radius: 30px;
  color: var(--black);
  text-transform: uppercase;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
}
.ticker::before {
  content: 'new';
  position: absolute;
  display: grid;
  place-items: center;
  width: 40px;
  height: 30px;
  margin: auto;
  font-size: 10px;
  border-radius: 15px;
  background-color: var(--black);
  color: var(--white);
  z-index: 10;
  top: 0;
  bottom: 0;
  left: 15px;
}
.ticker::after {
  content: '';
  position: absolute;
  display: block;
  width: 115px;
  height: inherit;
  top: 0;
  background: linear-gradient(to right, rgb(255, 213, 0) 50%, rgba(255, 213, 0, 0) 100%);
}

.ticker__text {
  display: inline-block;
  height: inherit;
  padding: calc((47px) / 2) 0;
  padding-left: 0.5em;
  font-size: 13px;
  line-height: 1;
  letter-spacing: 0.05em;

  animation-name: tickerAnimation;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

@keyframes tickerAnimation {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-100%);
  }
}
              
            
!

JS

              
                // 要素の複製
const cloneElements = (element, numberOfCopies) => {
  for (let i = 0; i < numberOfCopies - 1; i++) {
    const clone = element.cloneNode(true);
    element.after(clone);
  }
};

// アニメーションの時間設定
const setDuration = (element, setSpeed) => {
  const tickerText = element;
  const textWidth = tickerText.offsetWidth;
  const speed = setSpeed || 50;
  const animationDuration = textWidth / speed;

  tickerText.style.animationDuration = `${animationDuration}s`;
};

// 複数の要素に対応
document.querySelectorAll('.js-ticker').forEach((element) => {
  const numberOfCopies = Number(element.getAttribute('data-clone')) || 5;
  const speed = Number(element.getAttribute('data-speed'));
  const textElement = element.querySelector('.js-ticker-text');

  cloneElements(textElement, numberOfCopies);

  const textElements = element.querySelectorAll('.js-ticker-text');
  textElements.forEach((el) => {
    setDuration(el, speed);
  });
});
              
            
!
999px

Console