Introduction

In modern web design, animations can enhance user experience and engagement. One such animation effect is a horizontally scrolling text, which can be particularly useful for displaying news tickers, announcements, or promotional messages. In this article, we’ll explore how to create a smooth horizontal scrolling text animation with a play/pause toggle button using HTML, CSS, and JavaScript.

HTML

HTML / XML
<div class="container">
  <div class="horizontal-scrolling-items">
    <div class="horizontal-scrolling-items__item">
      Here is some horizontally scrolling text used for a tutorial. It will loop smoothly.
    </div>
  </div>
  <button id="toggleButton" class="fixed-icon">▶️</button>
</div>

CSS

CSS
@keyframes infiniteScroll {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-50%);
  }
}

.container {
  position: relative;
  width: 100%;
  overflow-x: hidden;
}

.horizontal-scrolling-items {
  display: flex;
  font-size: 40px;
  width: 2600px;
  animation-name: infiniteScroll;
  animation-duration: 20s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

.horizontal-scrolling-items__item {
  white-space: nowrap;
}

.fixed-icon {
  position: absolute;
  right: 10px; /* Adjust as needed */
  bottom: 10px; /* Adjust as needed */
  background: transparent;
  border: none;
  font-size: 24px; /* Adjust as needed */
  cursor: pointer;
}

Script

JavaScript
const button = document.getElementById('toggleButton');
const scrollingItems = document.querySelector('.horizontal-scrolling-items');
let isPlaying = true;

button.addEventListener('click', () => {
  if (isPlaying) {
    scrollingItems.style.animationPlayState = 'paused';
    button.textContent = '▶️'; // Play icon
  } else {
    scrollingItems.style.animationPlayState = 'running';
    button.textContent = '⏸️'; // Pause icon
  }
  isPlaying = !isPlaying;
});

Overview

The goal is to create a horizontal scrolling text that loops continuously. We’ll use CSS animations for the scrolling effect and JavaScript to add interactivity with a toggle button. Here's a breakdown of the implementation.

  1. HTML Structure: Defines the container and the scrolling text.
  2. CSS Styling and Animation: Handles the visual appearance and scrolling effect.
  3. JavaScript Interaction: Manages the play and pause functionality of the animation.

HTML Structure

The HTML structure consists of a container with horizontally scrolling text and a button to control the animation.

CSS Styling and Animation

The CSS rules manage the appearance of the elements and define the scrolling animation.

JavaScript Interaction

The JavaScript code toggles the animation between play and pause states when the button is clicked.

Conclusion

With this implementation, you’ve created a horizontally scrolling text animation that loops smoothly and can be toggled on and off with a simple button click. This approach not only adds a dynamic element to your web page but also provides users with control over the animation, enhancing interactivity.