-1

How can I dynamically change the content of a <span> tag in an infinite loop.

I have tried using the below method. Is it possible to do this with pure CSS animations? If it is not possible with pure CSS, please suggest an alternate method.

HTML:

<p>Lorem ipsum <span></span> sit amet.</p>

CSS:

span {
    content: '';
    animation: flip 2s infinite;
}
@keyframes flip {
    10% {
        content: 'Example no1';
    }
    40% {
        content: '';
    }
    80% {
        content: 'Example no2';
    }
    100% {
        content: '';
    }
}

Demo Fiddle

Harry
  • 87,580
  • 25
  • 202
  • 214
karbz0ne
  • 11
  • 4
  • Sorry, the question is unclear. Do you have any illustrations to show us what you are looking for? If all you need is for a CSS3 animation to execute in an infinite loop then use `animation-iteration-count` property with `infinite` as value. – Harry Jun 30 '14 at 11:11
  • http://jsfiddle.net/CU2JB/ didnt work before with content, tell me how its work and how to make animation. – karbz0ne Jun 30 '14 at 11:18
  • Are you ok to use JS like [this](http://jsfiddle.net/CU2JB/2/)? – Harry Jun 30 '14 at 11:29
  • Not really no. I need value as string. – karbz0ne Jun 30 '14 at 12:30
  • You mean you have an array and the values should be displayed in an iterative manner? – Harry Jun 30 '14 at 12:34
  • I guess yes. Just want to add value = "lorem" which JS will change to "ipsum" and make it interval. – karbz0ne Jun 30 '14 at 12:40
  • If u can, i agree with that. Im newbie here. – karbz0ne Jun 30 '14 at 12:49

1 Answers1

1

Changing the content of a span dynamically using the content property is not possible. It was originally designed to be used only with pseudo-elements (like :before or :after).

However, this can be achieved with just HTML and Javascript like shown below:

var i = 0; // The counter variable
var arrayLength = 5; // The max length of the data array 
var dataArray = ['Example 1', 'Example 2', 'Example 3', 'Example 4', 'Example 5']; // The actual data which have to be shown in a loop

function changeval() {
    if (i == arrayLength) i = 0; // If counter reaches max length, reset it
    document.getElementById('dataSpan').innerHTML = dataArray[i]; // Set the value to the span
    i++; // Increment counter
}

setInterval(changeval, 500); // Call the function to change value at defined intervals
<!-- Just add an ID to your span to which the content must be set -->
<p>Lorem ipsum <span id='dataSpan'></span> sit amet.</p>

Though CSS isn't the best for your case, the below can be achieved using a few CSS tricks/hacks if the no. of items in your array is small/finite.

body, html{
    font-size: 16px;
}
#dataSpan {
    height: 1.25em;
    overflow: hidden;
    display: inline-block;
    vertical-align: top;
    padding: 0px;
    line-height: 1.25em;
    top:0;
}
#dataSpan span {
    position: relative;
    -webkit-animation: changeContent 5s steps(4) infinite;
    -moz-animation: changeContent 5s steps(4) infinite;
    animation: changeContent 5s steps(4) infinite;
}
@-webkit-keyframes changeContent {
    from{top:0;}
    to {top:-5em;}
}
@-moz-keyframes changeContent {
    from{top:0;}
    to {top:-5em;}
}
@keyframes changeContent {
    from{top:0;}
    to {top:-5em;}
}
<p>Lorem ipsum <span id="dataSpan">
        <span>Example 1<br>Example 2<br>Example 3<br>Example 4<br>Example 5</span>
</span> sit amet.</p>
Harry
  • 87,580
  • 25
  • 202
  • 214