Trigger an animation on one element by hovering over another element - html

I am new to this and I'm stuck on a project.
I am trying to get an animation to cause an image to appear on the page when you hover over another element.
I already have the animation working, but what I can't figure out is how first to make the image invisible and the cause the animation to start when the said element is hovered in this case a h4 element.
I've tried everything I can think of and read several other posts relating to this but no luck. I think it might be something with me having to add position (absolute or relative) but I'm not sure.
This is the css for that section without any hovering function:
.timeline-img {
visibility: hidden;
}
.hovered:hover + p + .timeline-img {
animation: 7s alternate slideIn;
transition: all .2s;
visibility: visible;
}
.hovered:hover:after + p + .timeline-img {
visibility: visible;
}
#keyframes slideIn {
from {
margin-left: 100%;
width: 300%;
opacity: 0;
}
to {
margin-left: 0%;
width: 100%;
opacity: 1;
}
}
<div class="content">
<h4 class="hovered">Shepherds discovered coffee in Ethiopia circa 800 A.D.</h4>
<p>Legend has it that 9th century goat herders noticed the effect caffeine had on their goats, who appeared to "dance" after eating coffee berries. A local monk then made a drink with coffee berries and found that it kept him awake at night, thus the original cup of coffee was born.</p>
<div class="timeline-img">
<img src="shepherd.jpg" alt="Shepherd" class="fact-img">
</div>
</div>
Thanks to whoever helps me out and please explain as I'm still very new to this and learning.

You can do something like this:
The syntax is straightforward enough: just two or more selectors separated by a plus (+) symbol. The simplest construction is two elements with a plus between them. read more: Adjacent-Sibling Selector
.content h4:hover + p + .timeline-img{
animation: 7s alternate slideIn;
transition: all .2s;
}
#keyframes slideIn {
from {
margin-left: 100%;
width: 300%;
opacity: 0;
}
to {
margin-left: 0%;
width: 100%;
opacity: 1;
}
}
<div class="content">
<h4 class="hovered">Shepherds discovered coffee in Ethiopia circa 800 A.D.</h4>
<p>Legend has it that 9th century goat herders noticed the effect caffeine had on their goats, who appeared to "dance" after eating coffee berries. A local monk then made a drink with coffee berries and found that it kept him awake at night, thus the original cup of coffee was born.</p>
<div class="timeline-img">
<img src="shepherd.jpg" alt="Shepherd" class="fact-img">
</div>
</div>

In this example,
We set a :hover pseudo-class on the target sibling (~) class.
i.e. .hovered:hover ~ .timeline-img
We then use the animation-play-state property on the standard + hover state items.
animation-play-state: paused
animation-play-state: running
https://developer.mozilla.org/en-US/docs/Web/CSS/animation-play-state
.timeline-img {
-webkit-transition: all .2s;
-o-transition: all .2s;
transition: all .2s;
/* necessary */
-webkit-animation: 2s alternate slideIn;
animation: 2s alternate slideIn;
-webkit-animation-play-state: paused;
animation-play-state: paused;
/* Optional */
-webkit-animation-iteration-count: 2;
animation-iteration-count: 2;
-webkit-animation-direction: alternate;
animation-direction: alternate;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.hovered:hover ~ .timeline-img {
-webkit-animation-play-state: running;
animation-play-state: running;
}
#-webkit-keyframes slideIn {
from {
margin-left: 100%;
width: 300%;
opacity: 0;
}
to {
margin-left: 0%;
width: 100%;
opacity: 1;
}
}
#keyframes slideIn {
from {
margin-left: 100%;
width: 300%;
opacity: 0;
}
to {
margin-left: 0%;
width: 100%;
opacity: 1;
}
}
.red {
background: red;
height: 50px;
width: 50px;
border-radius: 100%
}
<div class="content">
<h4 class="hovered">Shepherds discovered coffee in Ethiopia circa 800 A.D.
</h4>
<p>
Legend has it that 9th century goat herders noticed the effect caffeine had on their goats, who appeared to "dance" after eating coffee berries. A local monk then made a drink with coffee berries and found that it kept him awake at night.
</p>
<div class="timeline-img">
<div class="red"></div>
</div>
</div>

Try this once.
.timeline-img{
transition: all .2s;
animation: 7s alternate slideIn;
animation-play-state: paused;
}
.hovered:hover ~ .timeline-img{
animation-play-state:running;
}
#keyframes slideIn {
from {
margin-left: 100%;
width: 300%;
opacity: 0;
}
to {
margin-left: 0%;
width: 100%;
opacity: 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
<h4 class="hovered">Shepherds discovered coffee in Ethiopia circa 800 A.D.</h4>
<p>Legend has it that 9th century goat herders noticed the effect caffeine had on their goats, who appeared to "dance" after eating coffee berries. A local monk then made a drink with coffee berries and found that it kept him awake at night, thus the original cup of coffee was born.</p>
<div class='timeline-img'>
<img src="shepherd.jpg" alt="Shepherd" class="fact-img">
</div>
</div>

Related

Creating a marquee that appears infinite with CSS and is responsive

I am trying to have a marquee that appears to be infinite looping while I have the css that makes this look good on desktop when on mobile or adjusting the window the spacing becomes jumbled and some elements disappear.
I have used the css I found here How to create a marquee that appears infinite using CSS or Javascript and this a codepen of what I have so far. https://codepen.io/zjolley/pen/gOWXqee
<div class="bannerone">
<span>Dragon</span>
<span>Element</span>
<span>Charger</span>
<span>ZipDrive</span>
<span>LightHouse</span>
<span>Exxtra</span>
<span>Hub</span> <span>iOS15</span>
<span>File Drive</span>
<span>Netherverse</span>
<span>CLpool</span>
</div>
<div class="bannertwo">
<span>Dragon</span>
<span>Element</span>
<span>Charger</span>
<span>ZipDrive</span>
<span>LightHouse</span>
<span>Exxtra</span>
<span>Hub</span> <span>iOS15</span>
<span>File Drive</span>
<span>Netherverse</span>
<span>CLpool</span>
</div>
<div class="bannerthree">
<span>Dragon</span>
<span>Element</span>
<span>Charger</span>
<span>ZipDrive</span>
<span>LightHouse</span>
<span>Exxtra</span>
<span>Hub</span> <span>iOS15</span>
<span>File Drive</span>
<span>Netherverse</span>
<span>CLpool</span>
</div>
<div class="bannerfour">
<span>Dragon</span>
<span>Element</span>
<span>Charger</span>
<span>ZipDrive</span>
<span>LightHouse</span>
<span>Exxtra</span>
<span>Hub</span> <span>iOS15</span>
<span>File Drive</span>
<span>Netherverse</span>
<span>CLpool</span>
</div>
</div>
//CSS CODE
.banner {
height: 40px;
position: relative;
overflow: hidden;
font-family: Roobert;
font-style: normal;
font-weight: normal;
font-size: 24px;
line-height: 36px;
width: 100%;
}
.banner .bannerone {
animation: bannermove 40s linear infinite;
}
.banner .bannertwo {
animation: bannermove 40s linear 10s infinite;
}
.banner .bannerthree {
animation: bannermove 40s linear 20s infinite;
}
.banner .bannerfour {
animation: bannermove 40s linear 30s infinite;
}
.banner div {
position: absolute;
width: 100%;
right: 100%;
height: 40px;
display: flex;
justify-content: space-between;
}
.banner:hover div {
animation-play-state: paused;
}
#keyframes bannermove {
0% {
right: 100%;
}
50% {
right: -100%;
}
100% {
right: -100%;
}
} ```
You are on the right track having more than one copy of the texts.
But you can simplify things - think of all the text stretched out in a line in just one banner. You have an even number of copies, so if you translate the whole banner to the left (negative X direction) by 50% on each iteration of the animation then start back again, where second half had got to (x=0) will be overwritten by the first half and it will all appear continuous.
You can probably get away with just two copies, it depends on how spread out you want things to be on really wide screens, but I've left the 4 copies in there just in case you need them.
.banner {
height: 40px;
position: relative;
overflow: hidden;
font-family: Roobert;
font-style: normal;
font-weight: normal;
font-size: 24px;
line-height: 36px;
min-width: 200vw;
/* ADDED */
animation: bannermove 40s linear infinite;
display: flex;
justify-content: space-between;
}
.banner:hover {
animation-play-state: paused;
}
#keyframes bannermove {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
.banner div {
padding: 10px;
/* just to ensure there is space between even on small devices */
}
<div class="banner">
<div>Dragon</div>
<div>Element</div>
<div>Charger</div>
<div>ZipDrive</div>
<div>LightHouse</div>
<div>Exxtra</div>
<div>Hub</div>
<div>iOS15</div>
<div>File Drive</div>
<div>Netherverse</div>
<div>CLpool</div>
<div>Dragon</div>
<div>Element</div>
<div>Charger</div>
<div>ZipDrive</div>
<div>LightHouse</div>
<div>Exxtra</div>
<div>Hub</div>
<div>iOS15</div>
<div>File Drive</div>
<div>Netherverse</div>
<div>CLpool</div>
<div>Dragon</div>
<div>Element</div>
<div>Charger</div>
<div>ZipDrive</div>
<div>LightHouse</div>
<div>Exxtra</div>
<div>Hub</div>
<div>iOS15</div>
<div>File Drive</div>
<div>Netherverse</div>
<div>CLpool</div>
<div>Dragon</div>
<div>Element</div>
<div>Charger</div>
<div>ZipDrive</div>
<div>LightHouse</div>
<div>Exxtra</div>
<div>Hub</div>
<div>iOS15</div>
<div>File Drive</div>
<div>Netherverse</div>
<div>CLpool</div>
</div>
Obviously you'll want to adjust the timing to suit you purposes.

Delay in infinite fade in & out CSS3 animation

I am working on the below:
Fiddle Code
Here is HTML:
<div id="animation">
<ul>
<li>this is</li>
<li>CSS3 looped</li>
<li>animation</li>
</ul>
</div>
This is the CSS:
#animation {
height: 100%;
width: 100%;
}
#animation ul {
position: relative;
text-align: center;
width: 100%;
}
#animation li {
position: absolute;
left:0;
top:0;
width: 100%;
opacity: 0;
padding: 10px;
}
#animation li:nth-of-type(1) {
-webkit-animation: fadein 6s ease-in-out -4s infinite alternate;
-moz-animation: fadein 6s ease-in-out -4s infinite alternate;
animation:fadein 6s ease-in-out -4s infinite alternate;
}
#animation li:nth-of-type(2) {
-webkit-animation: fadein 6s ease-in-out 0s infinite alternate;
-moz-animation: fadein 6s ease-in-out 0s infinite alternate;
animation: fadein 6s ease-in-out 0s infinite alternate;
}
#animation li:nth-of-type(3) {
-webkit-animation: fadein 6s ease-in-out 4s infinite alternate;
-moz-animation: fadein 6s ease-in-out 4s infinite alternate;
animation: fadein 6s ease-in-out 4s infinite alternate;
}
#-webkit-keyframes fadein {
0% {
opacity: 0;
}
66% {
opacity: 0;
}
76% {
opacity: 1;
}
100% {
opacity: 1;
}
}
#-moz-keyframes fadein {
0% {
opacity: 0;
}
66% {
opacity: 0;
}
76% {
opacity: 1;
}
100% {
opacity: 1;
}
}
#keyframes fadein {
0% {
opacity: 0;
}
66% {
opacity: 0;
}
76% {
opacity: 1;
}
100% {
opacity: 1;
}
}
I am new to CSS3 and with the code I want to stick paragraphs in instead of a couple of words. My question is, when the text fades in, how can you keep it on the screen for eg 10 seconds so someone can read it and the fade out into the next paragraph.
I have used duration and delay, doesn't really seem to work the way I wanted. Any help will be great.
The approach is really simple but you would need to do math as mentioned in Paulie_D's comment. I would leave the choice on whether to use it or not to you. Personally, I don't see anything wrong with this approach or any complexity provided the no. of elements to be faded in/out is static.
The overall approach is as follows:
We have 3 elements/paragraphs and for the example purpose I am going to make them fade-in for the first 3 seconds, stay as-is for the next 10 seconds and fade out for the last. So, for each element we need a total of 16 seconds in animation time.
While the first element has completed its animation and the second or third is being animated, the previous ones should hold the final state (that is faded out). To achieve this, the following need to be done:
Set the animation-duration for all elements such that it is the sum total of animation times for all elements. Here it would be 3*16s = 48s.
Set the keyframes such that each element would remain idle for 32s of the total duration because during this 32s gap the other two elements would be doing their animation. This is achieved by completing the fade-in, the stay and the fade-out all together within 33% of the animation's total duration.
Set animation-delay of second element to be 16s (because it has to start after the first one is completed) and that for the third to be 32s (because first two should complete).
Coming to the keyframes rule itself, as I said earlier the whole animation for one element should complete within 33% of the full duration. So at 6.25% (roughly 3s mark), we fade the element in and then till 26.75% (which is till 13s mark) we make it be at opacity: 1 and then at 33% (that is 16s mark) we completely fade it out.
#animation {
height: 100%;
width: 100%;
}
#animation ul {
position: relative;
text-align: center;
width: 100%;
}
#animation li {
position: absolute;
left: 0;
top: 0;
width: 100%;
opacity: 0;
padding: 10px;
}
#animation li:nth-of-type(1) {
animation: fadein 48s ease-in-out infinite;
}
#animation li:nth-of-type(2) {
animation: fadein 48s ease-in-out 16s infinite;
}
#animation li:nth-of-type(3) {
animation: fadein 48s ease-in-out 32s infinite;
}
#keyframes fadein {
0% {
opacity: 0;
}
6.25% { /* 3s for fade in */
opacity: 1;
}
26.75% { /* roughly 10s for stay as-is */
opacity: 1;
}
33% { /* 3s for fade out */
opacity: 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="animation">
<ul>
<li>This is</li>
<li>CSS3 looped</li>
<li>animation</li>
</ul>
</div>
The basic CSS code for this example looks like this:
.visible {
visibility: visible;
opacity: 1;
transition: opacity 2s linear;
}
.hidden {
visibility: hidden;
opacity: 0;
transition: visibility 0s 2s, opacity 2s linear;
}
When showing the element (by switching to the visible class), we want the visibility:visible to kick in instantly, so it’s ok to transition only the opacity property. And when hiding the element (by switching to the hidden class), we want to delay the visibility:hidden declaration, so that we can see the fade-out transition first. We’re doing this by declaring a transition on the visibility property, with a 0s duration and a delay.
At the end of the fade-out transition, we want to remove the hidden element from the flow, so that it does not leave a blank space in the middle of the page. Sadly we don’t have many options here:
display:none doesn’t work because it will be applied instantly, and
trying to delay it like we did with visibility won’t work;
position:absolute has the exact same issue;
It’s not ideal, but we can use margin-top (it can be transitioned and
thus delayed).
In order to use margin-top to hide the element, we need to have a slightly richer HTML structure:
<div class="visible">
<div>…</div>
</div>
And our CSS code becomes more complex:
.visible,
.hidden {
overflow: hidden;
/* This container should not have padding, borders, etc. */
}
.visible {
visibility: visible;
opacity: 1;
transition: opacity 2s linear;
}
.hidden {
visibility: hidden;
opacity: 0;
transition: visibility 0s 2s, opacity 2s linear;
}
.visible > div,
.hidden > div {
/* Put any padding, border, min-height, etc. here. */
}
.hidden > div {
margin-top: -10000px;
transition: margin-top 0s 2s;
}

Dynamic width for dynamic text

I have a div, 'marqueeContainer', that is acting as a scrolling marquee. In this div I have another div with the id of 'marquee'. In THIS div, I have three divs, LineA, LineB and LineC. The contents of these lines will be dynamic, loaded in from a database.
I am currently using CSS animation to scroll the 'marquee' from right to left, with it's parent 'marqueeContainer' having a fixed width, and the overflow set to hidden.
The problem that I'm running into is that when the amount of text in each LINE div is quite lengthy, the text will overlap.
INCLUDED JFIDDLE
How do I modify the CSS so that each line stays a single line, each div stays inline with the rest and does NOT overlap, and the animation is a seamless loop, no matter the length of the text inside each LINE div?
HTML:
<div id="marqueeContainer">
<div id="marquee" class="loaded">
<span class="line" id="lineA"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
<span class="line" id="lineB"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
<span class="line" id="lineC"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
</div>
</div>
CSS:
body{
font-family: Arial, sans-serif;
}
#marqueeContainer{
width: 570px;
height: 30px;
position: relative;
overflow: hidden;
padding: 0;
margin: 0;
top: 20px;
left: 47px;
pointer-events: none;
}
#marquee{
visibility:visible;
display: block;
position: absolute;
overflow: hidden;
width: 300%;
height: 30px;
pointer-events: none;
}
#marquee > span{
pointer-events: none;
}
#marquee.loaded{
-webkit-animation: marquee 10s linear infinite;
-moz-animation: marquee 10s linear infinite;
-ms-animation: marquee 10s linear infinite;
-o-animation: marquee 10s linear infinite;
animation: marquee 10s linear infinite;
pointer-events: none;
}
#marquee span.line{
display: none;
}
#marquee.loaded span.line{
display: inline-block;
float: left;
width: 33.333%;
padding-top: 6px;
color: rgba(0,0,0, 0.6);
font-size: 9pt;
white-space: nowrap;
}
#-webkit-keyframes marquee {
0% { left: -50%; }
100% { left: -150%; }
}
#-moz-keyframes marquee {
0% { left: -50%; }
100% { left: -150%; }
}
#-ms-keyframes marquee {
0% { left: -50%; }
100% { left: -150%; }
}
#-o-keyframes marquee {
0% { left: -50%; }
100% { left: -150%; }
}
#keyframes marquee {
0% { left: -50%; }
100% { left: -150%; }
}
add width: 100%; in place of width:33.33%; to #marquee.loaded span.line
add height: 100%; in place of height:30px; to #marquee
add height:30px; in place of height: 30px; to #marqueeContainer
edit: not the best solution since the marquee does not execute a perfect loop due to the text elements no longer being inline.
So after doing some research, I found a potential solution:
If you are okay with using jQuery in your project, there is a jQuery plugin you can use that might help:
<script type='text/javascript' src='//cdn.jsdelivr.net/jquery.marquee/1.3.1/jquery.marquee.min.js'></script>
I sketched out an example of something similar to what you are looking for:
Codepen Example
HTML:
<div class="marquee">
<span class="line" id="lineA"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
<span class="line" id="lineB"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
<span class="line" id="lineC"><span class="title">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio</span> - <span class="artist">Happy to Keep His Dinner Warm from "How to Succeed in Business without Really Trying" (accompaniment)</span> | <span class="album">Ruben Piirainen, piano</span></span>
</div>
CSS:
.marquee {
width: 570px;
overflow: hidden;
}
.marquee span.line{
padding-top: 6px;
color: rgba(0,0,0, 0.6);
font-size: 9pt;
}
JS:
$('.marquee').marquee({
//speed in milliseconds of the marquee
duration: 15000,
//gap in pixels between the tickers
gap: 50,
//time in milliseconds before the marquee will start animating
delayBeforeStart: 0,
//'left' or 'right'
direction: 'left',
//true or false - should the marquee be duplicated to show an effect of continues flow
duplicated: true
});
reference: http://aamirafridi.com/jquery/jquery-marquee-plugin
rather than add more external code into something I wish to keep as lightweight as possible. I have decided to keep this in the HTML and CSS. The problem I was having was BOTH in the CSS and HTML, not just the CSS. Rather than have three (3) spans, I consolidated everything into one (1) span, but repeated the content three times to ensure everything would stay on one line.
The #marquee's width is set to auto. where the span.line's width is set to 100%. No white-space: nowrap needed.
And so far, I have an ever expandable marquee that loops quite seamlessly by using only HTML and CSS
HERE'S MY FIDDLE
HTML:
<div id="marqueeContainer">
<div id="marquee" class="loaded">
<span class="line">The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio - Diamonds Are a Girl's Best Friend from "Gentlemen Prefer Blondes" (accompaniment) | Ruben Piirainen, piano The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio - Diamonds Are a Girl's Best Friend from "Gentlemen Prefer Blondes" (accompaniment) | Ruben Piirainen, piano The Singer's Musical Theatre Anthology: Mezzo-Soprano/Belter, Volume 1 Book/Audio - Diamonds Are a Girl's Best Friend from "Gentlemen Prefer Blondes" (accompaniment) | Ruben Piirainen, piano </span>
</div>
</div>
CSS:
body{
font-family: Arial, sans-serif;
}
#marqueeContainer{
width: 570px;
height: 25px;
position: relative;
overflow: hidden;
padding: 0;
margin: 0;
top: 20px;
left: 47px;
}
#marquee{
visibility:visible;
display: block;
position: absolute;
overflow: hidden;
width: auto;
height: 100%;
top: 0;
left: 0;
}
#marquee.loaded span.line{
-webkit-animation: marquee 10s linear infinite;
-moz-animation: marquee 10s linear infinite;
-ms-animation: marquee 10s linear infinite;
-o-animation: marquee 10s linear infinite;
animation: marquee 10s linear infinite;
}
#marquee.loaded span.line{
display: block;
width: auto;
padding-top: 6px;
color: rgba(0,0,0, 0.6);
font-size: 9pt;
white-space: nowrap;
position: relative;
top: 0;
}
#-webkit-keyframes marquee {
0% { left: -17%; }
100% { left: -50.4%; }
}
#-moz-keyframes marquee {
0% { left: -17%; }
100% { left: -50.4%; }
}
#-ms-keyframes marquee {
0% { left: -17%; }
100% { left: -50.4%; }
}
#-o-keyframes marquee {
0% { left: -17%; }
100% { left: -50.4%; }
}
#keyframes marquee {
0% { left: -17%; }
100% { left: -50.4%; }
}

How can I create a marquee effect?

I'm creating a marquee effect with CSS3 animation.
#caption {
position: fixed;
bottom: 0;
left: 0;
font-size: 20px;
line-height: 30px;
height:30px;
width: 100%;
white-space: nowrap;
-moz-animation: caption 50s linear 0s infinite;
-webkit-animation: caption 50s linear 0s infinite;
}
#-moz-keyframes caption {
0% { margin-left:120%; } 100% { margin-left:-4200px; }
}
#-webkit-keyframes caption {
0% { margin-left:120%; } 100% { margin-left:-4200px; }
}
<div id="caption">
The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.
</div>
Now I can get the basic marquee effect, but the code is too specific for this demo.
Is there a way to avoid using specific values like margin-left:-4200px;, so that it can adapt text in any length?
Here is a similar demo: http://jsfiddle.net/jonathansampson/XxUXD/ that uses text-indent but still with specific values.
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>
No hardcoded values — dependent on paragraph width — have been inserted.
The animation applies the CSS3 transform property (use prefixes where needed) so it performs well.
If you need to insert a delay just once at the beginning then also set an animation-delay. If you need instead to insert a small delay at every loop then try to play with an higher padding-left (e.g. 150%)
Based on the previous reply, mainly #fcalderan, this marquee scrolls when hovered, with the advantage that the animation scrolls completely even if the text is shorter than the space within it scrolls, also any text length takes the same amount of time (this may be a pros or a cons) when not hovered the text return in the initial position.
No hardcoded value other than the scroll time, best suited for small scroll spaces
.marquee {
width: 100%;
margin: 0 auto;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
display: inline-flex;
}
.marquee span {
display: flex;
flex-basis: 100%;
animation: marquee-reset;
animation-play-state: paused;
}
.marquee:hover> span {
animation: marquee 2s linear infinite;
animation-play-state: running;
}
#keyframes marquee {
0% {
transform: translate(0%, 0);
}
50% {
transform: translate(-100%, 0);
}
50.001% {
transform: translate(100%, 0);
}
100% {
transform: translate(0%, 0);
}
}
#keyframes marquee-reset {
0% {
transform: translate(0%, 0);
}
}
<span class="marquee">
<span>This is the marquee text (hover the mouse here)</span>
</span>
The accepted answers animation does not work on Safari, I've updated it using translate instead of padding-left which makes for a smoother, bulletproof animation.
Also, the accepted answers demo fiddle has a lot of unnecessary styles.
So I created a simple version if you just want to cut and paste the useful code and not spend 5 mins clearing through the demo.
http://jsfiddle.net/e8ws12pt/
.marquee {
margin: 0 auto;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
padding: 0;
height: 16px;
display: block;
}
.marquee span {
display: inline-block;
text-indent: 0;
overflow: hidden;
-webkit-transition: 15s;
transition: 15s;
-webkit-animation: marquee 15s linear infinite;
animation: marquee 15s linear infinite;
}
#keyframes marquee {
0% { transform: translate(100%, 0); -webkit-transform: translateX(100%); }
100% { transform: translate(-100%, 0); -webkit-transform: translateX(-100%); }
}
<p class="marquee"><span>Simple CSS Marquee - Lorem ipsum dolor amet tattooed squid microdosing taiyaki cardigan polaroid single-origin coffee iPhone. Edison bulb blue bottle neutra shabby chic. Kitsch affogato you probably haven't heard of them, keytar forage plaid occupy pitchfork. Enamel pin crucifix tilde fingerstache, lomo unicorn chartreuse plaid XOXO yr VHS shabby chic meggings pinterest kickstarter.</span></p>
The following should do what you want.
#keyframes marquee {
from { text-indent: 100% }
to { text-indent: -100% }
}

Hover Animation won't disappear when not hovering

I have a div I've animated on hover. However when I am not hovering the div won't disappear
This is what the entire thing looks like in action: http://jsfiddle.net/Vbxtc/
This is the html:
<nav>
<div id="controls">
<button id="playButton">Play</button>
<div id="defaultBar">
<div id="progressBar"></div>
</div>
<button id="vol" onclick="level()">Vol</button>
<button id="mute">Mute</button>
<button id="full" onclick="toggleFullScreen()">Full</button>
</div>
<div id="playlist" class="animated fadeInRight">
<div>cats</div>
<div>cats</div>
<div>cats</div>
</div>
</nav>
This is the CSS i've made:
#playlist{
position:absolute;
display:block;
border:1px solid red;
height: 82%;
width: 25%;
top: 20px;
right: 0px;
z-index: 2;
float:right;
padding: 0px;
margin: 0px;
color:white;
background-color:#999999;
opacity: 0;
}
#playlist:hover {
opacity: 1;
}
This is the animation im trying
.animated:hover {
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-ms-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation-duration: 1s;
-moz-animation-duration: 1s;
-ms-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
}
.fadeInRight {
-webkit-animation-name: fadeInRight;
-moz-animation-name: fadeInRight;
-o-animation-name: fadeInRight;
animation-name: fadeInRight;
}
#-webkit-keyframes fadeOutRight {
0% {
opacity: 1;
-webkit-transform: translateX(0);
}
100% {
opacity: 0;
-webkit-transform: translateX(20px);
}
}
#-webkit-keyframes fadeInRight {
0% {
opacity: 0;
-webkit-transform: translateX(20px);
}
100% {
opacity: 1;
-webkit-transform: translateX(0);
}
}
I noticed that when you time the mouse over exactly right (hover for about 1 second and move mouse up top), it DOES fade out nicely.
The other thing is, if you add the class fadeOutRight as follows:
<div id="playlist" class="animated fadeInRight fadeOutRight">
It fades out too quickly.
I know I didn't help much but the answer lies in the timing.
Also, if you had the fadeOutRight class on, for example, the sidebar, it works nicely!
<aside id="sidebar" class="fadeOutRight">
Perhaps, put the class of fadeOutRight on everything EXCEPT the fadeInRight div.
It's not a good idea to play with an element position in the hover state.
Even if you get to program it right (that is not easy), most of the time the user won't understand what's happening.
You can get flickering scenarios where, without the user moving the cursor, your div leaves the cursor position, canceling the hover, the div re-entering the cursor, the hover triggering , and so on.
I would recomend to trigger the hover on another div that covers the full area where the moving div will be.