为文本块上色,使其看起来像法国国旗
Color a bloc text to make it look like the French flag
我在一个网页中有一个给定的文本块,我想让它看起来像法国国旗(蓝/白/红)使用javascript和JQuery。
现在我尝试了以下代码:
或者如果你更喜欢Stackoverflow片段:
jQuery(function ($) {
// Calculates the text width in <p>
var html_cur = $('.loremipsum').html();
var html_calc = '<span>' + html_cur + '</span>';
$('.loremipsum').html(html_calc);
var width = $('.loremipsum').find('span:first').width();
var color_width = Math.floor(width / 3), // Width of each color
text = $( ".loremipsum" ).text(), // Text of <p>
array = [], // Array containing each sentences
sentence = '', // Current sentence
sentence_width = 0; // Current sentence width
for(var i = 0; i < text.length; i++) {
// Gets the char in the text
var char = text[i];
// Calulates actual char's width in pixels
var html_calc = '<span>' + char + '</span>';
$('.loremipsum').html(html_calc);
var width = $('.loremipsum').find('span:first').width();
// Apply to variables
sentence_width += width;
sentence += char;
// If sentence is long enough,
// resets the variables to start a new sentence and add it to the array
// Added the -10 to make sure sentences never overflow the color_width
// but looks like it's not enough
if(sentence_width >= color_width - 10) {
array.push(sentence);
sentence = '';
sentence_width = 0;
}
}
// Variables for final html
var final_html = '',
color = 0;
// Loops through the sentences and add them in between the correct color
final_html = '<span class="color-0">';
for (var i = 0, j = array.length; i < j; i++) {
final_html += array[i];
color = (color == 2) ? 0 : color + 1;
final_html += '</span><span class="color-'+color+'">';
}
final_html += '</span>';
// Display text on screen
$('.loremipsum').html(final_html);
});
.loremipsum {
border-radius: 5px;
font-size: 12px;
width: 300px;
}
.color-0 {
color : blue;
}
.color-1 {
color : white;
}
.color-2 {
color : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="loremipsum">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
所以基本例程如下:
- 计算p元素中文本的宽度,并将其除以3得到每种颜色的宽度。
- 将文本分割成句子,这些句子将存储到一个数组中。每个句子的宽度应该等于或略低于颜色的宽度。这是通过迭代文本中的字符来完成的,并计算每个字符的宽度,以知道何时将文本分割成一个新句子。
- 生成p元素的最终文本。意思是用适当的颜色(蓝色、白色、红色、重复)将每个句子封装在一个span元素中。
问题是,句子往往比它们应该有的长一点,所以颜色变化得太迟,导致每新一行的偏移量增加。
有没有人有任何想法让这个看起来像一个真正的蓝/白/红文本块?
谢谢你的帮助!:)
使用@DaniP的注释
他使用css设置背景渐变,然后通过-webkit-background-clip
将其应用于文本body { background-color: lightgray; } /* Just for easier viewing */
.loremipsum {
font-size: 15px;
background: -webkit-linear-gradient(0, mediumblue 33.3%, white 33.3%, white 66.6%, red 66.6%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
<p class="loremipsum">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
相关文章:
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 如何使删除线看起来像x
- Twitter Bootstrap 2.1.0子菜单在Responsive上看起来很混乱
- 用javascript创建一个看起来正常分布的模式
- PHP函数的返回看起来如何对AJAX和PHP有效
- 如何使Codeschools棱角分明的一面看起来相似
- HTML/JavaScript拖动&Drop-是否可以使重影图像*NOT*看起来'冲洗掉'
- 未捕获的语法错误:意外的令牌非法?看起来不错
- NodeJS readline有时会打印回第一个输入行(但并不总是,看起来是随机的)
- 如何检查html是否看起来'空'
- 看起来第一个console.log是'It’不太对
- 将文本区域样式化,使其看起来像单独的输入框
- jQuery代码看起来效率低下,你能帮忙吗?替换导航中的文本
- 如何使textarea元素看起来像普通文本
- 为什么jQuery修改的文本在Safari中悬停时看起来更轻?
- 为文本块上色,使其看起来像法国国旗
- 使文本看起来像正在用打字机打字一样
- HTML CSS下拉图标在IE中-下拉图标看起来在文本框&有空格
- 我想要一个jquery文本选择器插件,它看起来像文本选择器
- 如何使输入字段看起来像普通文本