Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to recreate marquee using CSS only?

I have a requirement that can be solved using a marquee

.ticker {
  white-space: no-wrap;
}
.item {
  display: inline-block;
  cursor: pointer;
}
<marquee class="ticker" onmouseover="this.stop()" onmouseout="this.start()">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
    <div class="item">Item 4</div>
 </marquee>

How do we make this compliant with html5 since marquee is deprecated?

I have seen a few examples, but most of them rely on fixed width. In my example, the items are received from the server so there can be a lot of them. Also, I will need that stop on hover since the items are links to something else.

Thank you very much for your help,

PS: I want to make sure we can't do this in CSS only before I start exploring javascript.

like image 353
herme 0 Avatar asked Dec 03 '22 12:12

herme 0


2 Answers

This codepen has a great example of what you're looking for.

To make it pause on hover, I added a hover state to pause the animation: https://codepen.io/anon/pen/zzpZyg

   .marquee:hover div {
      -webkit-animation-play-state: paused; /* Safari 4.0 - 8.0 */
        animation-play-state: paused;
    }

body { margin: 20px; }

.marquee {
  height: 25px;
  width: 420px;

  overflow: hidden;
  position: relative;
}

.marquee div {
  display: block;
  width: 200%;
  height: 30px;

  position: absolute;
  overflow: hidden;

  animation: marquee 5s linear infinite;
}

.marquee span {
  float: left;
  width: 50%;
}

@keyframes marquee {
  0% { left: 0; }
  100% { left: -100%; }
}

.marquee:hover div {
  -webkit-animation-play-state: paused; /* Safari 4.0 - 8.0 */
    animation-play-state: paused;
}
<div class="marquee">
  <div>
    <span>You spin me right round, baby. Like a record, baby.</span>
    <span>You spin me right round, baby. Like a record, baby.</span>
  </div>
</div>
like image 52
Meggg Avatar answered Dec 27 '22 04:12

Meggg


Sorry I know I am late. However, I have a simple solution to create a marquee with CSS.

.marquee-container{
  overflow:hidden;
}
.marquee{
  min-width:100%;
  animation: marquee 15s linear infinite;
}
.marquee:hover{
  animation-play-state: paused;
}
@keyframes marquee {
  from{margin-left : 120%;}
    to{margin-left: -20%;}
} 
<div class="marquee-container">
    <p class="marquee">
      <span>Item 1</span>
      <span>Item 2</span>
      <span>Item 3</span>
      <span>Item 4</span>
    </p>
</div>
like image 33
TayabRaza Avatar answered Dec 27 '22 03:12

TayabRaza