jQuery高亮显示文本中重要的关键字

一、界面预览

  鼠标放到右边的Tab按钮上,文字透明度降低,同时一段文字高亮显示,效果如下:

 

  Demo地址:http://5thirtyone.com/sandbox/samples/fadefocus/

  很绚丽的效果幺!

二、实现原理

  将要高亮显示的文字加上段落标记, class=”mask”div 做为遮罩层,使此遮罩层位于文字内容之上(z-index属性,使用Jquery给段落动态添加样式类。

三、HTML代码

<div class="wrapper">

    <ul class="entity-results">

        <li><a class="d1" href="#">Summarya>li>

        <li><a class="d2" href="#">Avatara>li>

        <li><a class="d3" href="#">Formatsa>li>

    ul>

    <div class="content">

        <h2>

            Avatar (2009 film)h2>

        <div class="entity-source">

            <img src="images/avatar.jpg" alt="Avatar poster" />

            <p>

                Avatar, also known as James Cameron's Avatar, is an American 3-D science fiction

                epic film written and directed by <a href="http://en.wikipedia.org/wiki/James_Cameron">

                    James Camerona>, and was released on December 16, 2009 by 20th Century Fox.

                The film is co-produced by <a href="http://en.wikipedia.org/wiki/Lightstorm_Entertainment">

                    Lightstorm Entertainmenta>, and <span class="d1">focuses on an epic conflict on Pandoraspan>,

                an inhabited Earth-sized moon of Polyphemus, one of three fictional gas giants orbiting

                <a href="http://en.wikipedia.org/wiki/Alpha_Centauri_A">Alpha Centauri Aa>. On

                Pandora, human colonists and the sentient humanoid indigenous inhabitants of Pandora,

                the Na'vi, engage in a war over the plaNET's resources and the latter's continued

                existence. The film's title refers to <span class="d2">an avatar, a representation of

                    a real person in a virtual worldspan>.p>

            <p>

                <span class="d3">The film was released in 2D and 3D formatsspan>, along with an

                IMAX 3D release in selected theaters. The film is being touted as a breakthrough

                in terms of filmmaking technology, for its development of 3D viewing and stereoscopic

                filmmaking with cameras that were specially designed for the film's production.p>

            <p>

                Read the rest of the <a href="http://en.wikipedia.org/wiki/Avatar_(2009_film)">original

                    Wikipedia page about Avatara>p>

            <div class="mask">

            div>

        div>

    div>

div>

 

entity-results类中显示了Tab按钮,每个按钮控制左边文字的透明度,段落文字的高亮显示。

entity-source类中有三个段落span Calss分别为 d1 d2 d3,也就是高亮文字段落。

class=mask的空div放到最后,此Div也就是一个遮罩层。

四、CSS关键代码

.entity-source, .entity-source span.show

{

    position: relative;

}

.entity-source .mask

{

    display: none;

    position: absolute;

    top: 0;

    left: 0;

    height: 100%;

    width: 100%;

    z-index: 1;

}

.entity-source span

{

    z-index: 2;

}

.entity-source span.show

{

    background: #ffc;

    color: #000;

}

 

 

mask中的z-index:1 使得mask> 覆盖在左边文字内容之上。

it知识库jQuery高亮显示文本中重要的关键字,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。