Smooth continuous scrolling text marquee CSS/width problem

Struggling with creating a seamless HTML/CSS scrolling text feature. The scrolling starts on the right and moves across correctly, but then vanishes for 6-7 seconds when it reaches the left side due to width: 100%; on #marqueeText, causing it to continue off screen for about 1020px (container width).

If I eliminate width: 100%;, the #marqueeText is only approximately 150px wide, scrolls in from the right across the screen about 300px (-100%), and repeats.

const timePerPixel = 5;
const containerWidth = 1120;
const text = document.getElementById('marqueeText');
const textWidth = text.offsetWidth;
const distance = textWidth + containerWidth;
const duration = timePerPixel * distance;
text.style.animationDuration = `${duration}ms`;
@keyframes marquee-animation {
  from {
    /* Start right out of view */
    transform: translate3d(100%, 0, 0);
  }
  to {
    /* Animate to the left of the container width */
    transform: translate3d(-100%, 0, 0);
  }
}

.topmarquee {
  width: 100%;
  border: 1px solid #000;
}

.topmarquee .marquee {
  width: calc(100% - 200px);
  position: relative;
  overflow: hidden;
  height: 31px;
}

.topmarquee .marquee #marqueeText {
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  white-space: nowrap;
  transform: translate3d(100%, 0, 0);
  animation-name: marquee-animation;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  line-height: 31px;
  width: 100%;
}
<div class="d-none d-lg-flex row topmarquee mt-4">
  <div class="marquee">
    <p id="marqueeText" class="marqueeText mb-0">
      This is a test website
    </p>
  </div>
</div>

(The animation properties are separate due to JS that adjusts animation-duration based on text length for consistent speed, referenced from: Marquee text effect. Same scrolling speed no matter the length of the text)

Want it to reappear on the right once it disappears off the left for a seamless appearance.

Answer №1

To ensure a seamless animation, the system must have knowledge of the actual text width. Currently, the innermost element has the same width as the outer one.

This code snippet adjusts the width to fit the content dynamically.

Due to dependencies on the width for translations, we position the animated element 100% left initially to hide it. Then, during the animation, it moves back to the left and translates by -100% (relative to its own width).

const timePerPixel = 5;
const containerWidth = 1120;
const text = document.getElementById('marqueeText');
const textWidth = text.offsetWidth;
const distance = textWidth + containerWidth;
const duration = timePerPixel * distance;
text.style.animationDuration = `${duration}ms`;
@keyframes marquee-animation {
  from {
    /* Start right out of view */
  }
  to {
    /* Animate to the left of the container width */
    left: 0;
    transform: translateX(-100%);
  }
}

.topmarquee {
  width: 100%;
  border: 1px solid #000;
}

.topmarquee .marquee {
  width: calc(100% - 200px);
  position: relative;
  overflow: hidden;
  height: 31px;
}

.topmarquee .marquee #marqueeText {
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  white-space: nowrap;
  left: 100%;
  transform: translateX(0);
  animation-name: marquee-animation;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  line-height: 31px;
  width: 100%;
  width: fit-content;
}
<div class="d-none d-lg-flex row topmarquee mt-4">
  <div class="marquee">
    <p id="marqueeText" class="marqueeText mb-0">
      This is a test website
    </p>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Value attribute in an HTML input

Can you help me with an HTML question? I want to create a form that displays one value but posts a different value. <form action="" method="post"> <input type="submit" name="action" value="Buy"/> </form> For instance, I want t ...

What is the process for aligning rows with the selected option from the drop-down menu

Alright, so here's the scenario: I have created a table along with two drop-down filters. The first filter is for selecting the Year, and it offers options like "All", "2023", "2022", and "2021". When I pick a specific year, let's say "2022", onl ...

Struggling to modify the page width using CSS and HTML on Wordpress, facing unexpected limitations

I've exhausted all my options. I've attempted to add a certain % page width globally using CSS in the editor, page template, and customizer but it's just not working. It's frustrating because I'm using the Twenty Twenty Two theme, ...

Tips for invoking a function to automatically load elements on a jQuery mobile website

I am looking to keep my navbar HTML markup in a centralized location for easy editing. Currently, the body content of my index.html file appears like this: <div data-role="page" id="SomePage"> <div data-role="header"> <h1>Thi ...

display or conceal a div when a radio button is selected

I need a way to display a specific div containing unique text once a radio button is selected, while hiding all other div sections. <?php foreach ($items as $item){ $i=1; echo ' <div class="form-check"> <input class="fo ...

Expandable tool tip box

I've specified the tooltip's width and height as 100% .tool-tip, .tool-tip.top{ width: 100%; height: 100%; margin-left: -43px; } However, it still fails to expand based on the content within. The issue can be seen here: http://jsfi ...

The scroller's height is displayed at 100%

I'm experiencing an issue with the scrolling of a division. I've set the overflow to "scroll" for the division, but it's displaying at 100% height. Please refer to the screenshot provided. Can you advise me on which properties I should adjus ...

Determine whether a div contains any child elements

I am working on a piece of code that checks for the presence of the class "wrong" in any of my divs, and if found, displays a jQuery UI dialog box. My goal now is to enhance this code by adding a condition where it also checks for empty divs before showing ...

Is there a way in Jquery to remove a class?

I have created a code for a single page website where clicking on the toggle bar will display the 'ul' and clicking on one of the 'a' links will take me to the corresponding div. I want the toggle bar to automatically close back after c ...

Data from the session is not displayed when a redirect occurs

On the main page, I include the following code: <?php $required = array('post_question'); // Checking if post field is not empty $error = false; foreach($required as $field) { if (empty($_POST[$field])) { $error = true; } } if(isset($_POST[&a ...

jQuery does not support CSS pseudo-code

I'm having trouble with my CSS pseudo-code not being recognized by jQuery. Here is the code I'm using: CSS: h1 { background: red; width: 100px; display: inline-block; } h1:after { content:" | "; background:blue; display ...

Is it feasible to navigate through submenus using only the [Tab] key in CSS?

Is there a way to activate a submenu in a styled menu using only the [Tab] key so that the submenu becomes visible? I attempted to use the :focus pseudo-class ul li a:focus + ul li a ... and when t ...

There is a button on my website that uses a small piece of Javascript to post a tweet, but unfortunately, it is not functional on mobile devices

Check out this link to view the demonstration - http://codepen.io/illpill/pen/VbeVEq function sendTweet(message, author) { window.open('https://twitter.com/intent/tweet?hashtags=thequotemachine&text=' + encodeURIComponent('"' + m ...

Detect IE 9 and IE 10 with jQuery

My jQuery code works well in Chrome and Mozilla but not in IE. if ($("html").hasClass("ie")) { $(function(){ $('.green-column, .red-column, .grey-column').click(function() { alert($(this).attr("data-type")); ...

Tips on concealing the header on the AngularJS login page

I need assistance with hiding the header on my login page while displaying the logged-in user's name on other pages. Can anyone provide guidance? <!DOCTYPE html> <html ng-app="myApp"> <head> <script src="angular.js"></scr ...

Utilize the display: flex property to position a div in the bottom right corner of its containing element

I'm trying to showcase a yellow div "icon" at the bottom right corner of its red container "content". Can someone help me figure out what's incorrect in my code? .root { display: inline-flex; background-color: red; } .content { d ...

Preventing the negative consequences of being colorblind through programming techniques

My experience as a web developer has taught me that poor color contrast on a website can severely impact revenue. I am determined to change this, but have encountered an issue. On my site, there is a section where users can select a color and see it displa ...

What could be causing the svg to not be visible inside the div?

I am struggling to display an SVG element within a div as it is not visible at all. Can someone help me make the SVG visible in the div? Here is a link to the code: http://codepen.io/helloworld/pen/HjkhK <div style="height:100px;background:yellow;"> ...

Steps to modify the border width upon gaining focus

I am struggling to adjust the border width when my input box is focused. Currently, it has a 1px solid border which changes to a 2px different color solid border upon focus. However, this change in border width is causing the containing div to shift by 1px ...

Guidelines for placing an HTML element in relation to another HTML element using CSS or JavaScript

Is there a way to position an HTML element in relation to another using CSS or JavaScript? I have attempted to copy the inner HTML of the "second-element" and append it inside the "first-element", but this approach is causing issues with other functional ...