skip to Main Content

I have a paragraph with around 20 lines. I want each of those lines to repeat itself after 1 scroll is completed. For example:

Let’s say I had a line in a paragraph that is as follows:

1,2,3,4,

I would like the line to scroll horizontally on the screen (which I achieved), but I also want the 1 to come after the 4 after a single scroll has completed.

I got the horizontal scroll to work, but the sentences don’t repeat.

I have each sentence in a span which is either bar_content1 or bar_content2. This is so I can have every other sentence go in a different direction.

I’ve tried to take advantage of the ::before function but I can’t seem to get it working.

I included an image below of the logic of my final idea.

Note: HTML included in this question isn’t what is in my project due to privacy reasons. That being said, the code is formatted the same way.

enter image description here

.bar_content2 {
  display: block;
  width: 100%;
  transform: translateX(-100%);
  animation: move2 100s linear infinite;
}

@keyframes move2 {
  0% {
    transform: translateX(-100%);
  }
  50% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(-100%);
  }
}

.bar_content1 {
  display: block;
  width: 100%;
  transform: translateX(100%);
  animation: move1 100s linear infinite;
}

@keyframes move1 {
  0% {
    transform: translateX(100%);
  }
  50% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}


/* Animate each line in the paragraph */

p {
  overflow: hidden;
  position: relative;
  white-space: nowrap;
  /* Prevent line breaks */
}

p::before {
  content: attr(data-text);
  position: absolute;
  top: 0;
  left: 100%;
  /* Start from the right side */
  animation: scrollText 20s linear infinite;
  /* Adjust the duration as needed */
}

@keyframes scrollText {
  0% {
    left: 100%;
  }
  100% {
    left: -100%;
  }
  /* Move to the left, creating the interlacing effect */
}
<p>
  <span class="bar_content1">1,2,3,4,</span>
  <span class="bar_content2"> 5,6,7,8, </span>
</p>

2

Answers


  1. Now I understand a little better after your updates which added the code and the image to describe the effect you want. You basically want a carousel. A quick search online shows lots of options. This page in particular has some good stuff: https://css-tricks.com/css-only-carousel/

    I’ve modified the last example on that page here, which makes use of a JS library called Flickity: https://flickity.metafizzy.co/

    I have never personally used Flickety before, it it seems pretty good from what I can see, and it’s simple, and it requires no other dependencies. It may not be perfect, but I think this gets you like 90% of the way there!

    Note that FLickety has a lot of options you can read about here: https://flickity.metafizzy.co/options.html and I’ve set a few of them for you in the data-flickity HTML attribute. Change those as you need.

    .carousel {
      background: #EEE;
    }
    
    .carousel-cell {
      margin-right: 10px;
      width: 50%;
      background: #8C8;
      padding: 15px;
    }
    <link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
    <script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
    
    <div class="carousel"
      data-flickity='{
        "autoPlay": 1500,
        "selectedAttraction": 0.01,
        "pauseAutoPlayOnHover": false,
        "friction": 0.15,
        "wrapAround": true,
        "draggable": false,
        "prevNextButtons": false,
        "pageDots": false,
        "contain": true
      }'>
      <div class="carousel-cell">1, 2, 3, 4</div>
      <div class="carousel-cell">5, 6, 7, 8</div>
      <div class="carousel-cell">9, 10, 11, 12</div>
    </div>
    Login or Signup to reply.
  2. Rotating Marquee

    Objective

    I’m not 100% sure, but I assume the question is as follows:

    "How do you create a marquee1 that scrolls horizontally and brings the text back to the starting edge as soon as it passes the ending edge?"

    1Since <marquee> is deprecated, you should customize one using alternative elements and CSS animation.

    Solution – (Not Perfect) ¯_(ツ)_/¯

    HTML

    1. First, use a non-inline2 element as the container/parent. It should have a defined width.
    2. Next, use a non-inline element as the content/child — place it within the previously mentioned element (container/parent).
    3. Add the text within the content/child element from step #2. Then after the text add a number of spaces equivalent to the width of the text3. Finally, add the same text at the end.4

    2A non-inline element is basically any element that doesn’t have display: inline assigned to it explicitly or by default. Some common inline elements are: <a>, <b>, <i>, and <u>. If you prefer to use an inline element, be sure to assign it an alternative display value.

    3There are a variety of spaces that can be utilized as HTML entities — see Figure I.

    4The pattern: text spaces text is not perfect, and will need adjusting according to the width of the text. Use a monospace font-family since each character is the same size, matching spaces and text widths will be easier — see Figure II.

    Figure I – Spaces

    Name HTML Entity
    space
    &#32;
    no-break space
    &nbsp; or &#160;
    en space
    &emsp; or &#8194;
    em space
    &emsp; or &#8195;
    three-per-em space
    &emsp13; or &#8196;
    four-per-em space
    &emsp14; or &#8197;
    six-per-em space
    &#8198;
    figure space
    &numsp; or &#8199;
    punctuation space
    &puncsp; or &#8200;
    thin space
    &thinsp; or &#8201;
    hair space
    &VeryThinSpace; or &#8202;
    narrow no-break space
    &#8239;

    Figure II – Text Spaces Text Pattern

    <a>1, 2, 3, 4 &numsp; &numsp; &numsp; &numsp; &numsp; &numsp; 1, 2, 3, 4</a>
    

    CSS

    1. Assign a width and overflow: hidden to the container/parent element.
    /* example */ .marquee { width: 60vw; overflow: hidden; }
    
    1. Assign the animation shorthand property to the content/child element.

      The space delimited list of values should consist of the following:

      1. The name of the @keyframes at-rule — ex. leftToRight
      2. animation-duration — ex. 3s
      3. animation-timing-function — ex. linear
      4. animation-iteration-count — ex. infinite
    /* example */ a { animation: leftToRight 3s linear infinite; }
    
    1. Define a @keyframes at-rule.
     /* example */
     @keyframes leftToRight {
       /** 
        * Start position will be 2.25 times the length of `<a>` to the 
        * left of the beginning of the container/parent element.
        * This value was determined by trial and error so it may vary if
        * the text is a different length. 
        */
        0% { transform: translateX(-225%); }
       /**
        * End position will be the length of `<a>` to the end of the
        * container/parent element.
        */
        100% { transform: translateX(100%); }
      }        
    

    Example

    View in Full page mode — Initial display within <iframe> is not rendered correctly

    .marquee {
      width: 60vw;
      margin: 20px auto;
      overflow: hidden;
      font: 15vmin/1.5 Consolas;
    }
    
    a,
    b {
      display: block;
      font-weight: 700;
      white-space: nowrap;
    }
    
    a {
      animation: leftToRight 3s linear infinite;
    }
    
    b {
      animation: rightToLeft 3s linear infinite;
    }
    
    @keyframes leftToRight {
      0% {
        transform: translateX(-225%);
      }
      100% {
        transform: translateX(100%);
      }
    }
    
    @keyframes rightToLeft {
      0% {
        transform: translateX(100%);
      }
      100% {
        transform: translateX(-225%);
      }
    }
    <div class='marquee'>
      <a>1, 2, 3, 4 &numsp; &numsp; &numsp; &numsp; &numsp; &numsp; 1, 2, 3, 4</a>
    </div>
    
    <div class='marquee'>
      <b>5, 6, 7, 8 &numsp; &numsp; &numsp; &numsp; &numsp; &numsp; 5, 6, 7, 8</b>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search