简体   繁体   English

在html5中突出显示文字

[英]Text highlighting in html5

I am trying to write a telugu text using uni codes so that I can highlight the text by changing into multiple colours according to sound. 我正在尝试使用uni代码编写telugu文本,以便我可以根据声音更改为多种颜色来突出显示文本。 for that I am using html5 and js 因为我使用的是html5和js

<div  class="pa">&#3114;</div>
<div  class="paa">&#3114;&#3134;</div>

by using 通过使用

$("#paa").animate({ color: "blue" }, 500); 

I can change the entire colour of the letter but now I want to highlight some part of that letter . 我可以改变字母的整个颜色,但现在我要强调那封信的某些部分。 Please suggest how it can be done.??? 请建议如何做到。???

there is a few css tricks to fake the background-clip:text; 有一些css技巧来伪造background-clip:text; or something alike SVG. 或类似的东西SVG。

 span { position:relative; color:green; } span:before { position:absolute; top:0; left:0; overflow:hidden; content:attr(data-text); color:blue; height:0.7em; overflow:hidden; animation: txtclr 4s infinite; } p { background:yellow; color:rgba(128, 0, 128 , 0.5); font-size:2em; font-weight:bold; background:linear-gradient(to bottom,lightgray 50%,yellow 50%); display:table;/* demo purpose to shrink to text size*/ box-shadow:inset -3em 0 rgba(100,100,100,0.5); } @keyframes txtclr { 0% { height:50%; } 25%,75%{ height:0; width:0; } 50%{ height:100%; } } 
 <p><span data-text="my text">my text</span> and some other text </p> 

run code snippet and see green text filling with blue from top to bottom. 运行代码段并查看从上到下填充蓝色的绿色文本。

no prefix added, later browser do not need it :) 没有添加前缀,以后的浏览器不需要它:)

Do you mean something like this? 你的意思是这样的吗? uses only css to flow the text over it, no need for javascript in this example ;-) but you could always switch out classes with desired effect and such. 仅使用css在其上传输文本,在此示例中不需要javascript ;-)但是您可以随时切换具有所需效果的类等。

What it does is uses the transition property that allows it to animate inbetween different css states after an event like hover, click active and such. 它的作用是使用transition属性,允许它在悬停,点击活动等事件之后在不同的css状态之间制作动画。

To highlight a part of a letter, you need to add that HTML element twice. 要突出显示字母的一部分,您需要将该HTML元素添加两次。 What I have done with coloured and plain. 我用彩色和平原做了什么。 This also counts for individual letters. 这也适用于个别字母。 You need to set them to an absolute position and wrap them in a relative div. 您需要将它们设置为绝对位置并将它们包装在相对div中。 By doing that they will position themselves absolutely to the top left location(in my example) of the the relative wrapper div. 通过这样做,他们将自己绝对定位到相对包装div的左上角位置(在我的例子中)。 This way you can keep text flow if you use a <span> for a wrapper fo example 这样,如果使用<span>作为包装器示例,则可以保留文本流

Then you set one to width 0 or to whatever width you want it to cover up half the other letter 然后你将一个设置为宽度0或任何你希望它覆盖另一半字母的宽度

In my example only half of the Y is red, the other half(half ish) is not red. 在我的例子中,只有Y的一半是红色,另一半(半ish)不是红色。

If you wish to cover a top half you can play with height. 如果你想覆盖上半部分你可以玩高度。

http://jsfiddle.net/L9L8L966/1/ http://jsfiddle.net/L9L8L966/1/

  #container { font-size:40px; position:relative; background-color:#CCC; width:450px; height:40px; } #coloured { position:absolute; z-index:2; top:0px; left:0px; display:block; width:0%; -webkit-transition: width 1s ease-in-out; -moz-transition: width 1s ease-in-out; -o-transition: width 1s ease-in-out; transition: width 1s ease-in-out; color:red; overflow:hidden; } #container:hover #coloured { width:100%; } #plain { position:absolute; z-index:1; width:100%; display:block; top:0px; left:0px; color:black; } 
 <div id="container"> <div id="coloured"> abcdefghijklmnopqrstuvwxyz </div> <div id="plain"> abcdefghijklmnopqrstuvwxyz </div> </div> 

It is feasible, but not easy with all languages. 这是可行的,但并不容易使用所有语言。 In the following example, I colored accents on certain letters. 在下面的示例中,我在某些字母上添加了重音符号。 http://jsfiddle.net/07hh3z2n/5/ http://jsfiddle.net/07hh3z2n/5/

The hint is to use a CSS to get an inline-block with no width. 提示是使用CSS来获取没有宽度的inline-block

.phantom {
    display: inline-block;
    color: red;
    width: 0;
    overflow: visible;
}

If the part of the letter you want to highlight is an existing unicode char, this will work fine. 如果要突出显示的字母部分是现有的unicode字符,则可以正常工作。 Otherwise, you can try to use (or create) a special font with the parts of letters to highlight. 否则,您可以尝试使用(或创建)带有要突出显示的字母部分的特殊字体。

If you want to highlight on the fly in your code, I would use a javascript frame work Highlight.js . 如果你想在你的代码中动态突出显示,我会使用一个javascript框架工作Highlight.js it is very light and easy to work with. 它非常轻便,易于使用。 Here is a fiddle with jquery and knockout sample: $('.searchme').highlight('high'); 这是jquery和knockout示例的小提琴: $('.searchme').highlight('high'); sample 样品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM