简体   繁体   English

如何在CSS3 / Javascript中创建“突出显示”效果?

[英]How to create a “highlighting” effect in CSS3/Javascript?

I have a span of text, which when clicked gets highlighted by changing its background color. 我有一段文本,点击后通过更改其背景颜色突出显示。 I would like to animate the highlighting, so that the background color changes progressively from left to right, as if someone was actually highlighting the text. 我想为突出显示设置动画,以便背景颜色从左向右逐渐变化,就像有人实际上突出显示文本一样。 Any thoughts on how that would be achievable with CSS3 and/or Javascript/jQuery? 关于如何用CSS3和/或Javascript / jQuery实现这一点的任何想法?

If you're okay with some CSS3-only features, you can use transitions, gradients and background-size : 如果您对某些仅限CSS3的功能感到满意,则可以使用过渡,渐变和background-size

.highlightable {
    background-size: 0 100%;
    background-repeat: no-repeat;
    -webkit-transition: background-size 0.5s ease-out;
    -moz-transition: background-size 0.5s ease-out;
    -ms-transition: background-size 0.5s ease-out;
    -o-transition: background-size 0.5s ease-out;
    transition: background-size 0.5s ease-out;
}

.highlightable.highlight {
    background-image: -webkit-linear-gradient(yellow, yellow);
    background-image: -moz-linear-gradient(yellow, yellow);
    background-image: -ms-linear-gradient(yellow, yellow);
    background-image: -o-linear-gradient(yellow, yellow);
    background-image: linear-gradient(yellow, yellow);
    background-size: 100% 100%;
}​

Here's a demo. 这是一个演示。

Lots of JavaScript libraries make this pretty easy for example my favourite: YUI 很多JavaScript库使这个很容易,例如我最喜欢的: YUI

Otherwise without a library I would set an array with hexadecimal intermediary colour values that you want you background to have at each time slice; 否则,如果没有库,我会设置一个数组,其中包含十六进制中间颜色值,您希望在每个时间片中具有背景; and use the setTimeout() method to change the background colour several times in quick succession; 并使用setTimeout()方法快速连续多次更改背景颜色; eg 例如

setTimeout("document.getElementById('mySpan').style.backgroundColor = myColors[i]; i++;", 50); 

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

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