iamx iamx - 3 months ago 5
jQuery Question

Change CSS dynamically based on a value from array

I have this part of html:

<h1 class="gr-title uppercase">
<span class="title-red">GET MORE</span>
<span id="wordSwap" class="title-black">PROFITS</span>

...and this jquery code that changes the value of the span with ID wordSwap:


var words = [
], i = 0;

// 2 second interval
}, 2000);


I'm trying to change the
for each value in the array based on what value/word comes next, but I am stuck and can't get it to work. For example I'd like the word "Time" to have letter-spacing = 30px, and the word "Value" 20px, and so on. How can I get this done?

Here is a jsfiddle of what I have up to now: https://jsfiddle.net/vgum6jn6/


Try something like this:

var baseWidth = 100; //some value
          .css('letter-spacing','') //reset spacing
          .css('letter-spacing', function(){
                var ratio = baseWidth / $(this).prop('offsetWidth');
                return ratio + 'px';
   // 2 second interval
}, 2000);