javascript - Applying rainbow text to all text in class - Stack Overflow

admin2025-04-09  0

I've found a really nice rainbow text animation that I like:

I'm trying to apply the effects of this to all text within a class.
<span class="rainbow">some text here...</span>

The problem is that if I have two different pieces of text on a page:

<span class="rainbow">Text #1</span>
<span class="rainbow">Text #2</span>

The rainbow affect applies to both pieces of text but the text contents of each span changes to Text #2.

The text isn't static on the page so I can't use id.

Is there a way I can change the class (or id) of each rainbow text span to rainbow-1, rainbow-2 etc and have the javascript code execute on each span independently? Maybe a loop that iterates over id's beginning with rainbow- and applies the effect to it independently?

I've found a really nice rainbow text animation that I like:
https://github./xoxco/Rainbow-Text

I'm trying to apply the effects of this to all text within a class.
<span class="rainbow">some text here...</span>

The problem is that if I have two different pieces of text on a page:

<span class="rainbow">Text #1</span>
<span class="rainbow">Text #2</span>

The rainbow affect applies to both pieces of text but the text contents of each span changes to Text #2.

The text isn't static on the page so I can't use id.

Is there a way I can change the class (or id) of each rainbow text span to rainbow-1, rainbow-2 etc and have the javascript code execute on each span independently? Maybe a loop that iterates over id's beginning with rainbow- and applies the effect to it independently?

Share Improve this question asked Feb 1, 2016 at 18:40 Mo BeigiMo Beigi 1,7755 gold badges30 silver badges52 bronze badges 1
  • Can you create a JSFiddle so we can see the code run? – Seano666 Commented Feb 1, 2016 at 18:48
Add a ment  | 

4 Answers 4

Reset to default 5

You suspected correctly. By using jQuery's .each over the .rainbow elements, the text displays properly.

Here is the working code

$('.rainbow').each(function() {
  $(this).rainbow({
    colors: [
      '#FF0000',
      '#f26522',
      '#fff200',
      '#00a651',
      '#28abe2',
      '#2e3192',
      '#6868ff'
    ],
    animate: true,
    animateInterval: 100,
    pad: false,
    pauseLength: 100
  });
});

Here's a GIF of it in action!

Sure, looks like the plugin expects you only declare one element at once.

You can put it inside an each loop, have your callback use an i variable to represent the current index, and use $(this) to instantiate it for each item with the class.

The end result will include your .rainbow-1, .rainbow-2, etc. classes as well.

$('.rainbow').each(function(i) {
  $(this).addClass('rainbow-' + i).rainbow({
    colors: [
      '#FF0000',
      '#f26522',
      '#fff200',
      '#00a651',
      '#28abe2',
      '#2e3192',
      '#6868ff'
    ],
    animate: true,
    animateInterval: 100,
    pad: false,
    pauseLength: 100
  });
});

http://jsfiddle/ajVzR/683/

you can use jQuery "each" for solving your problem, like bellow

<div class="foo">123456789</div>
<div class="foo">123456789</div>
<div class="foo">123456789</div>
<div class="foo">123456789</div>


    $(".foo").each(function(){
        $(this).rainbow({animate:true,animateInterval:10,colors:['#FF0000','#f26522','#fff200','#00a651','#28abe2','#2e3192','#6868ff']});
    });

You can work around this like this:

$('.rainbow').each(function() {
$(this).rainbow({
    colors: [
        '#FF0000',
        '#f26522',
        '#fff200',
        '#00a651',
        '#28abe2',
        '#2e3192',
        '#6868ff'
    ],
    animate: true,
    animateInterval: 100,
    pad: false,
    pauseLength: 100
});
});

demo

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744201950a235840.html

最新回复(0)