Jquery 高亮顯示文本中重要的關(guān)鍵字
一、界面預(yù)覽
鼠標(biāo)放到右邊的Tab按鈕上,文字透明度降低,同時(shí)一段文字高亮顯示,效果如下:
Demo地址:http://5thirtyone.com/sandbox/samples/fadefocus/
很絢麗的效果幺!
二、實(shí)現(xiàn)原理
將要高亮顯示的文字加上<span>段落標(biāo)記, class=”mask”的div 做為遮罩層,使此遮罩層位于文字內(nèi)容之上(z-index屬性,使用Jquery給段落動(dòng)態(tài)添加樣式類。
三、HTML代碼
<div class="wrapper">
<ul class="entity-results">
<li><a class="d1" href="#">Summary</a></li>
<li><a class="d2" href="#">Avatar</a></li>
<li><a class="d3" href="#">Formats</a></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 >
James Cameron</a>, and was released on December 16, 2009 by 20th Century Fox.
The film is co-produced by <a >
Lightstorm Entertainment</a>, and <span class="d1">focuses on an epic conflict on Pandora</span>,
an inhabited Earth-sized moon of Polyphemus, one of three fictional gas giants orbiting
<a >Alpha Centauri A</a>. 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 world</span>.</p>
<p>
<span class="d3">The film was released in 2D and 3D formats</span>, 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 >original
Wikipedia page about Avatar</a></p>
<div class="mask">
</div>
</div>
</div>
</div>
entity-results類中顯示了Tab按鈕,每個(gè)按鈕控制左邊文字的透明度,段落文字的高亮顯示。
entity-source類中有三個(gè)段落span Calss分別為 d1 d2 d3,也就是高亮文字段落。
class=”mask”的空div放到最后,此Div也就是一個(gè)遮罩層。
四、CSS關(guān)鍵代碼
.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 使得<div class=”mask”> 覆蓋在左邊文字內(nèi)容之上。
z-nidex:2又使得span段落覆蓋在<div class=”mask”>之上。從而顯示實(shí)現(xiàn)了段落文字高亮顯示。
五、Jquery代碼
jQuery(document).ready(function($) {
// mask source 控制mask的動(dòng)畫效果
var maskSource = jQuery('.mask');
jQuery('.entity-results').hover(function() {
maskSource.animate({opacity:0.7},1).fadeIn('750');
}, function() {
maskSource.fadeOut('1000');
});
// match hover 控制段落的高亮顯示
var sample1 = jQuery('span.d1');
var sample2 = jQuery('span.d2');
var sample3 = jQuery('span.d3');
jQuery('a.d1').hover(function() {
sample1.addClass('show'); //給段落添加類
}, function() {
sample1.removeClass('show'); //移除段落類
});
jQuery('a.d2').hover(function() {
sample2.addClass('show');
}, function() {
sample2.removeClass('show');
});
jQuery('a.d3').hover(function() {
sample3.addClass('show');
}, function() {
sample3.removeClass('show');
});
});
動(dòng)畫函數(shù)animate(params, [duration], [easing], [callback])
Params:一組包含作為動(dòng)畫屬性和終值的樣式屬性和及其值的集合
duration (可選):種預(yù)定速度之一的字符串("slow", "normal", or "fast")或表示動(dòng)畫時(shí)長的毫秒數(shù)值(如:1000)
easing (可選):要使用的擦除效果的名稱(需要插件支持).默認(rèn)jQuery提供"linear" 和 "swing".
callback (可選):在動(dòng)畫完成時(shí)執(zhí)行的函數(shù)
淡入效果函數(shù):fadeIn(speed, [callback])
Speed:三種預(yù)定速度之一的字符串("slow", "normal", or "fast")或表示動(dòng)畫時(shí)長的毫秒數(shù)值(如:1000)
callback (可選):(Optional) 在動(dòng)畫完成時(shí)執(zhí)行的函數(shù)
淡出效果函數(shù):fadeOut解釋同fadeIn
演示地址:http://demo.jb51.net/html/fadefocus/index.html
打包下載地址: http://xiazai.jb51.net/200912/yuanma/fadefocus.rar
- 比較不錯(cuò)的JS/JQuery顯示或隱藏文本的方法
- 基于JQuery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊文本框顯示隱藏提示文本
- 前端html中jQuery實(shí)現(xiàn)對(duì)文本的搜索功能并把搜索相關(guān)內(nèi)容顯示出來
- JQuery文本框高亮顯示插件代碼
- jQuery實(shí)現(xiàn)鼠標(biāo)單擊網(wǎng)頁文字后在文本框顯示的方法
- jQuery實(shí)現(xiàn)鼠標(biāo)跟隨提示層效果代碼(可顯示文本,Div,Table,Html等)
- jQuery獲取標(biāo)簽文本內(nèi)容和html內(nèi)容的方法
- jQuery取得設(shè)置清空select選擇的文本與值
- 使用Jquery實(shí)現(xiàn)點(diǎn)擊文字后變成文本框且可修改
- jQuery實(shí)現(xiàn)文本顯示一段時(shí)間后隱藏的方法分析
相關(guān)文章
Jquery中的CheckBox、RadioButton、DropDownList的取值賦值實(shí)現(xiàn)代碼
隨著Jquery的作用越來越大,使用的朋友也越來越多。在Web中,由于CheckBox、 Radiobutton 、 DropDownList等控件使用的頻率比較高,就關(guān)系到這些控件在Jquery中的操作問題2011-10-10jquery.tableSort.js表格排序插件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了jquery.tableSort.js表格排序插件使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02jquery 獲取自定義屬性(attr和prop)的實(shí)現(xiàn)代碼
jquery中用attr()方法來獲取和設(shè)置元素屬性,attr是attribute(屬性)的縮寫,在jQuery DOM操作中會(huì)經(jīng)常用到attr(),attr()有4個(gè)表達(dá)式2012-06-06