I need javascript to detect each browser wrapped line of text and wrap it into <span class="line">
.
I came across articles talking about measuring the y axis of each word, but have not seen a solid solution.
Here's what I have so far. See it on Jsfiddle.
HTML
<div class="inline-bg">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus condimentum metus, et placerat augue rutrum vitae. Donec arcu lorem, eleifend at elementum eget, consectetur vel lacus. Nam euismod iaculis varius. Phasellus sed dui diam. Nullam facilisis, diam sit amet sagittis cursus, metus tortor gravida erat, ut fringilla risus ipsum eu nisl.</div>?
JS/jQuery
(function($){
$.fn.inlinebackground = function() {
$.each(this, function(i,t) {
var split = $(t).html().split(' ');
var output = '';
$.each(split, function(i,o){
output += '<span class="line">'+o+'</span>';
if (i < (split.length - 1)) {
output += '<br>';
}
});
$(t).html(output);
});
}
})(jQuery);
/* End Plugin Code */
// Run the plugin on .news-caption
$(function(){
$('.inline-bg').inlinebackground();
});
CSS
.inline-bg { width: 200px; line-height:3em; }
.inline-bg span.line { background-color: black; color: white; padding: 15px; }
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…