Created
July 2, 2016 23:56
-
-
Save Jaskaranbir/eb0f26d0ba47f55e25fde3b63d8b7ef4 to your computer and use it in GitHub Desktop.
Inifinite Continuous Scrolling Marquee in CSS without any explicit framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="maindiv"> | |
<!-- Need to have two divs with same content. | |
One div will translate completely out of parent and other div will fill the void till the transition for first div starts again. --> | |
<div id="div1"> | |
Test-1 Test-2 Test-3 Test-4 Test-5 Test-6 Test-7 Test-8 Test-9 Test-10 Test-11 | |
</div> | |
<div id="div2"> | |
Test-1 Test-2 Test-3 Test-4 Test-5 Test-6 Test-7 Test-8 Test-9 Test-10 Test-11 | |
</div> | |
</div> | |
<style> | |
#maindiv{ | |
border: 2px solid black; | |
/* These are absolutely necessary */ | |
overflow: hidden; | |
white-space: nowrap; | |
} | |
#div1 { | |
display: inline-block; | |
animation: marquee 10s linear infinite; | |
} | |
#div2 { | |
display: inline-block; | |
animation: marquee2 10s linear infinite; | |
/* Must be half the animation duration of both divs so it stats in sync to fill void left by completed transtition of first div */ | |
animation-delay: 5s; | |
} | |
@keyframes marquee { | |
from { | |
transform: translateX(100%); | |
} | |
to { | |
transform: translateX(-100%); | |
} | |
} | |
@keyframes marquee2 { | |
from { | |
transform: translateX(0%); | |
} | |
to { | |
transform: translateX(-200%); | |
} | |
} | |
</style> | |
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> | |
<script> | |
// It is absolute necessity that div should be smaller than individual text content. | |
$('#maindiv').width($('#div1').width()); | |
</script> |
Thank you very much for your detailed answer! I will try my best to tweak your code and work through this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @Markuswindt,
Its actually fairly easy to remove jQuery from this code. You would just need to replace line#54 with something like:
There are various other types of widths (such as the method above doesnt include margin-width, you would need different property for that). Feel free to explore docs about various ways to get element-widths and use as required.
As for starting from the middle, you will have to run the animation in 2-parts: 1st is playing from middle to end once, then from start to end infinitely.
Here's some sample code to achieve this, feel free to tweak/refactor this (for example: hardcoding those durations is a bad idea, see if you can refactor this to use some variables + offsets using SCSS/JavaScript/etc.).
Click to expand code
And there's no need for cash, I mainly do this stuff for fun ;)