How to Create a Marquee Effect

How to create a marquee using CSS or Javascript

Here are a few ways you can achieve the result, you can choose the one u like the best.

  • HTML marquee tag
  • CSS animation and text-indent
  • CSS animation and position relative
  • JS vanilla (no libs)
  • JS Jquery animate

/* Vanilla JS */
var rightJS = { init: function(){ rightJS.Tags = document.querySelectorAll('.rightJS'); for(var i = 0; i < rightJS.Tags.length; i++){ rightJS.Tags[i].style.overflow = 'hidden'; } rightJS.Tags = document.querySelectorAll('.rightJS div'); for(var i = 0; i < rightJS.Tags.length; i++){ rightJS.Tags[i].style.position = 'relative'; rightJS.Tags[i].style.right = '-'+rightJS.Tags[i].parentElement.offsetWidth+'px'; } rightJS.loop(); }, loop: function(){ for(var i = 0; i < rightJS.Tags.length; i++){ var x = parseFloat(rightJS.Tags[i].style.right); x ++; var W = rightJS.Tags[i].parentElement.offsetWidth; var w = rightJS.Tags[i].offsetWidth; if((x/100) * W > w) x = -W; if (rightJS.Tags[i].parentElement.parentElement.querySelector(':hover') !== rightJS.Tags[i].parentElement) rightJS.Tags[i].style.right = x + 'px'; } requestAnimationFrame(this.loop.bind(this)); }};window.addEventListener('load',rightJS.init);
/* JQUERY */
$(function(){ var rightJQ = { init: function(){ $('.rightJQ').css({ overflow: 'hidden' }); $('.rightJQ').on('mouseover',function(){ $('div', this).stop(); }); $('.rightJQ').on('mouseout',function(){ $('div', this).animate({ right: '100%' }, 14000, 'linear' ); }); rightJQ.loop(); }, loop: function(){ $('.rightJQ div').css({ position: 'relative', right: '-100%' }).animate({ right: '100%' }, 14000, 'linear', rightJQ.loop); } }; rightJQ.init();});
marquee { background: #0089fa; }
.rightTI { background: #ff002b; white-space: nowrap; overflow: hidden; animation: marquee 18s linear infinite;}.rightTI:hover { animation-play-state: paused;}@-webkit-keyframes marquee { 0% {text-indent: 100%;} 100% {text-indent: -100%;}}
.rightCSS { background: #a35dc1; overflow: hidden;} .rightCSS div { position: relative; animation: CSSright linear 18s infinite;} @keyframes CSSright { 0% { right: -100% } 100% { right: 100% }}.rightCSS:hover div { animation-play-state: paused;}
.rightJS { background: #ffa900; }
.rightJQ { background: #00a753; }
.li { float: left; width: 80%; padding: 1%; margin: 1% 10%; height: 20px; border-radius: 0.5em; box-shadow: 0 0.1em 0.5em;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><marquee class="li" direction=”right” onmouseover="stop()" onmouseout="start()">★ HTML tag <marquee> ★</marquee><div class="rightTI li">★ CSS animation and text-indent ★</div><div class="rightCSS li"><div>★ CSS animation and position relative ★</div></div><div class="rightJS li"><div>★ pure javascript ★</div></div><div class="rightJQ li"><div>★ Jquery animate ★</div></div>

How can I create a marquee effect?

With a small change of the markup, here's my approach (I've just inserted a span inside the paragraph):

.marquee {
width: 450px;
margin: 0 auto;
overflow: hidden;
box-sizing: border-box;
}

.marquee span {
display: inline-block;
width: max-content;

padding-left: 100%;
/* show the marquee just outside the paragraph */
will-change: transform;
animation: marquee 15s linear infinite;
}

.marquee span:hover {
animation-play-state: paused
}


@keyframes marquee {
0% { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}


/* Respect user preferences about animations */

@media (prefers-reduced-motion: reduce) {
.marquee span {
animation-iteration-count: 1;
animation-duration: 0.01;
/* instead of animation: none, so an animationend event is
* still available, if previously attached.
*/
width: auto;
padding-left: 0;
}
}
<p class="marquee">
<span>
When I had journeyed half of our life's way, I found myself
within a shadowed forest, for I had lost the path that
does not stray. – (Dante Alighieri, <i>Divine Comedy</i>.
1265-1321)
</span>
</p>

How to create a marquee that appears infinite using CSS or Javascript

You are so close. Hopefully the demo below is self explainable but, basically you need to start your key frames at -100% your marquee's width then end at 100% so it's off screen before it restarts.

Hope this helps!

[edit] added continuous scrolling

body {   margin: 0;  font-family: "UniversLTPro-Ex";  font-size: 30px;}
a { text-decoration: none; color: #000;}
.marquee { height: 35px; width: 100%;
overflow: hidden; position: relative; background-color: #e9e5fb; border-top: 1px solid black; border-bottom: 1px solid black; padding: 8px 0 4px 0;}

/* would need to be adjusted depending on time */.marquee .marqueeone{ animation: marquee 10s linear infinite}
.marquee .marqueetwo{ animation: marquee 10s linear 2.5s infinite }
.marquee .marqueethree{ animation: marquee 10s linear 5s infinite}
.marquee .marqueefour{ animation: marquee 10s linear 7.5s infinite}
/* even out the elements */.marquee div { position: absolute; width: 100%; left: 100%; height: 40px; display: flex; justify-content: space-between;}
.marquee:hover div { animation-play-state: paused;}
/* add delay at the end of animation so you dont start while another div is going */@keyframes marquee { 0% { left: 100%; } 50% { left: -100%; } 100% {left: -100%}}
/* @import must be at top of file, otherwise CSS will not work */@import url("//hello.myfonts.net/count/3909a7");
@font-face {font-family: 'UniversLTPro-Ex';src: url('webfonts/3909A7_0_0.eot');src: url('webfonts/3909A7_0_0.eot?#iefix') format('embedded-opentype'),url('webfonts/3909A7_0_0.woff2') format('woff2'),url('webfonts/3909A7_0_0.woff') format('woff'),url('webfonts/3909A7_0_0.ttf') format('truetype');}
<div class="marquee">    <div class="marqueeone"><a href="#">twitter</a>                   <a href="#">instagram</a>                    <a href="#">pinterest</a>                   <a href="#">spotify</a>                   <a href="#">magazine</a>                  </div>    <div class="marqueetwo"><a href="#">twitter</a>                   <a href="#">instagram</a>                    <a href="#">pinterest</a>                   <a href="#">spotify</a>                   <a href="#">magazine</a>   </div>         <div class="marqueethree"><a href="#">twitter</a>                   <a href="#">instagram</a>                    <a href="#">pinterest</a>                   <a href="#">spotify</a>                   <a href="#">magazine</a>   </div>                   <div class="marqueefour"><a href="#">twitter</a>                   <a href="#">instagram</a>                    <a href="#">pinterest</a>                   <a href="#">spotify</a>                   <a href="#">magazine</a>         </div></div>

Is it possible to recreate marquee using CSS only?

Finally I found one that works, and here is the finally product https://fiddle.sencha.com/#view/editor&fiddle/228u

Here is the original one, https://codepen.io/lewismcarey/pen/GJZVoG

<div class="wrapper">
<div class="container">
<span>Span Me 1</span>
<span>Span Me 2</span>
<span>Span Me 3</span>
<span>Span Me 4</span>
</div>
</div>

The trick was to "left-pad" the wrapper to hide the container initially. Then, "right-pad" the container so that the animation only stops/restarts once the container has gone off screen. Both padding are sized relatively. display: block; is added to the container so that the right padding uses the wrapper's size. And finally, we add an animation on the wrapper's transform attribute.

Thank you all,

How to use CSS to achieve the marquee effect?

We can achieve this with "Slick Carousel" here is an example.

.marquee-item {
padding: .5em 1em;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.css" integrity="sha512-yHknP1/AwR+yx26cB1y0cjvQUMvEa2PFzt1c9LlS4pRQ5NOTZFWbhBig+X9G9eYW/8m0/4OXNx8pxJ6z57x0dw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<div class="marquee">
<div class="marquee-item">your content</div>
<div class="marquee-item">your content</div>
<div class="marquee-item">your content</div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js" integrity="sha512-XtmMtDEcNz2j7ekrtHvOVR4iwwaD6o/FUJe6+Zq+HgcCsk3kj4uSQQR8weQ2QVj1o0Pk6PwYLohm206ZzNfubg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
$(document).ready(function(){
$('.marquee').slick({
arrows: false,
autoplay: true,
autoplaySpeed: 0,
cssEase: 'linear',
centerMode: true,
dots: false,
infinite: true,
speed: 3000,
variableWidth: true,
});
});
</script>

CSS3 Marquee Effect, Without Empty Space

Here is a sample how you can do, and by setting the delay and duration you control the space between the texts

.marquee {  background-color: #ddd;  width: 500px;  margin: 0 auto;  overflow: hidden;  white-space: nowrap;}.marquee span {  display: inline-block;  font-size: 20px;  position: relative;  left: 100%;  animation: marquee 8s linear infinite;}.marquee:hover span {  animation-play-state: paused;}
.marquee span:nth-child(1) { animation-delay: 0s;}.marquee span:nth-child(2) { animation-delay: 0.8s;}.marquee span:nth-child(3) { animation-delay: 1.6s;}.marquee span:nth-child(4) { animation-delay: 2.4s;}.marquee span:nth-child(5) { animation-delay: 3.2s;}
@keyframes marquee { 0% { left: 100%; } 100% { left: -100%; }}
<p class="marquee">  <span>this is a</span>  <span>simple marquee</span>  <span>using css</span>  <span>only tech</span>  <span>with a delay</span></p>

How to create a marquee effect for a text of smaller length not exceeding the screen size in Android?

I used simple light weight of ticker like animation which I developed in my early Android days.

below is complete code.

Hope this helps.

Works for all API levels of Android

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setticker("Hello", this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public void setticker(String text, Context contx) {
if (text != "") {
LinearLayout parent_layout = (LinearLayout) ((Activity) contx)
.findViewById(R.id.ticker_area);

TextView view = new TextView(contx);
view.setText(text);

view.setTextColor(Color.BLACK);
view.setTextSize(25.0F);
Context context = view.getContext(); // gets the context of the view

// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);

// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();

// gets the screen width
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();

view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
(int) height, 1f));

System.out.println("width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());

// performs the calculation
float toXDelta = width - (screenWidth - 0);

// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// Animation parameters
Animation mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
parent_layout.addView(view);
}
}


}

in activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/ticker_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9CB1DD"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>

How to make marquee text only when it's overflowing?

The easiest way to determine if an element is overflowing is to compare its scroll height/width to its offset height/width. If any of the scroll values are larger than their offset pairs, your element's contents are overflowing.

function isElementOverflowing(element) {
var overflowX = element.offsetWidth < element.scrollWidth,
overflowY = element.offsetHeight < element.scrollHeight;

return (overflowX || overflowY);
}

From here it's a simple question of checking the return value of this function and adding a marquee effect if true. To achieve this, you can wrap your div's contents in a <marquee>, or achieve the same visual effect using the prefixed marquee CSS rules or simulating it via a CSS animation.

NB: while the <marquee> tag still works as expected in most browsers, it is considered deprecated hence not futureproof.

  • Here is a quick fiddle on wrapping in a marquee tag, play around with text length to see how it works. (alternatively, you can set the marquee's behavior to alternate from side to side: here's how )
  • here is a tutorial on CSS marquee
  • and here is a thread on visually simulating a marquee with animations

Good luck!



Related Topics



Leave a reply



Submit