javascript - How to target alternate odd/even text lines -


say i've p element or div element having text 10-15 lines, client has weird call on this, needs odd/even lines having different text color. line 1 - black, line 2 should grey, line 3 should again black , on...

so decided using span's , changed color variable resolution killing things here, aware of :first-line selector (which won't useful in case), selectors :odd & :even ruled out here not using tables, there way can achieve using css or need use jquery?

tl; dr : want target odd/even lines in paragraph or div

i need css solution, if not, jquery , javascript welcome

demo 1

a rather ugly little solution, compounded fact it's 3:30 am. still, works on plain text blocks , allows each line individually styled.

fiddle: http://jsfiddle.net/fptq2/2/
chrome 26, ff 20, safari 5.1.7, ie 8/9/10 (7 made functional)

essentially it:

  1. splits source individual words once
  2. wraps each word in span (ugly effective-any style can applied span)
  3. uses simple position calculation determine if element lower previous
  4. changes colors based on index change
  5. performs #3-5 on resize (this should throttled!)
$(".stripe").each(function(){   var obj = $(this);   var html = obj.html().replace(/(\s+\s*)/g, "<span>$1</span>");   obj.html(html); });  function highlight(){     var offset = 0;     var colorindex = 0;     var colors = ["#eee","#000"];     var spans = $(".stripe span");      // note direct dom usage here (no jquery) performance     for(var = 0; < spans.length; i++){         var newoffset = spans[i].offsettop;            if(newoffset !== offset){             colorindex = colorindex === 0 ? 1 : 0;             offset = newoffset;        }         spans[i].style.color = colors[colorindex];     } }  highlight(); $(window).on("resize", highlight); 

demo 2

fiddle: http://jsfiddle.net/fptq2/4/

  • uses larger block of text
  • shows effect applied multiple elements
  • caches "all spans" selector
  • adds resize throttling
(function () {     $(".stripe").each(function () {         var obj = $(this);         var html = obj.html().replace(/(\s+\s*)/g, "<span>$1</span>");         obj.html(html);     });      var offset = 0;     var colorindex = 0;     var colors = ["#ccc", "#000"];     var spans = $(".stripe span");      function highlight() {         (var = 0; < spans.length; i++) {              var newoffset = spans[i].offsettop;             if (newoffset !== offset) {                 colorindex = colorindex === 0 ? 1 : 0;                 offset = newoffset;             }              spans[i].style.color = colors[colorindex];         }     }      highlight(); // initial highlighting      var timeout;     function throttle(){         window.cleartimeout(timeout);         timeout = window.settimeout(highlight, 100);     }      $(window).on("resize", throttle); })(); 

output

enter image description here


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -