javascript 實(shí)用的文字鏈提示框效果
效果要基本實(shí)現(xiàn)以下功能:
(1)鼠標(biāo)滑過(guò)文章中的鏈接文字,要在相應(yīng)位置彈出提示框,提示框的樣式由css來(lái)控制,高度自適應(yīng);鼠標(biāo)可以點(diǎn)擊提示框的中的鏈接,滑離提示框時(shí),框自動(dòng)消失;
(2)把提示框的位置控制在文本域范圍之內(nèi),如果鏈接文字處在文本域左側(cè),提示框要居右顯示,使其不會(huì)出離文本域;反之,如果鏈接文字處在文本域右側(cè),提示框要居左顯示;
(3)如果文本域內(nèi)容很多,而鏈接文字恰巧處于瀏覽器底部,為了使提示框不出離于瀏覽器的可視范圍,提示框的位置要自動(dòng)調(diào)整到鏈接文字的上面;
1.css
.main{width:950px; border:#9C3 1px solid; margin:0 auto; padding:15px; background-color:#fff; line-height:25px;font-size:14px; position:relative;}
span{border:#70bce4 2px solid; display:block; position:absolute; background-color:#FFF; padding:5px 10px; font-size:12px; width:200px; display:none;}
.cur{color:#900;}
2.js
//獲取對(duì)象元素的函數(shù);
function $a(id,tag){var re=(id&&typeof id!="string")?id:document.getElementById(id);if(!tag){return re;}else{return re.getElementsByTagName(tag);}}
function tips(){
//獲取文本域中的a元素列表;
var article=$a("article","a")
for(i=0;i<article.length;i++){
//遍歷a元素,不包含類"cur"的a元素將不會(huì)執(zhí)行之后的代碼;
if(article[i].className.indexOf("cur")==-1) continue;
article[i].onmouseover=function(e){
//獲取鼠標(biāo)指針在瀏覽器可視區(qū)域的坐標(biāo),不受文檔內(nèi)容影響;
var e=e||event;
posX = e.clientX;
posY = e.clientY;
//獲取瀏覽器可視區(qū)域高度;
var bodyhe=document.documentElement.clientHeight;
var parwidth=$a("article").offsetWidth;
var tipbox=get_nextSibling(this);
var boxlist=$a("article","span")
//設(shè)置文本區(qū)域中的span提示框均為隱藏狀態(tài);
for(j=0;j<boxlist.length;j++){
boxlist[j].style.display="none";
boxlist[j].innerHTML="調(diào)入后臺(tái)數(shù)據(jù)"
}
//設(shè)置當(dāng)前的提示框顯示;
tipbox.style.display="block";
var w=tipbox.offsetWidth-this.offsetWidth;
/*
以id為article的div添加了相對(duì)定位position:relative,所以它已經(jīng)是提示框的父級(jí);
控制彈出框的顯示位置;
*/
tipbox.style.left=(this.offsetLeft>parwidth/2?this.offsetLeft-w:this.offsetLeft)+"px";
tipbox.style.top=(posY+tipbox.offsetHeight>bodyhe?this.offsetTop-tipbox.offsetHeight:document.all?this.offsetTop+15:this.offsetTop+this.offsetHeight)+"px";
tipbox.onmouseover=function(){this.style.display="block";}
tipbox.onmouseout=this.onmouseout=function(){tipbox.style.display="none";}
}
}
}
//獲取對(duì)象元素的下一個(gè)標(biāo)簽節(jié)點(diǎn);
function get_nextSibling(n){
var x=n.nextSibling;
while (x.nodeType!=1){
x=x.nextSibling;
}
return x;
}
貼出源文件代碼 感興趣的朋友可以測(cè)試一下,有問(wèn)題可留言 @&@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>提示框效果</title>
<style type="text/css">
.main{width:950px; border:#9C3 1px solid; margin:0 auto; padding:15px; background-color:#fff; line-height:25px;font-size:14px; position:relative;}
span{border:#70bce4 2px solid; display:block; position:absolute; background-color:#FFF; padding:5px 10px; font-size:12px; width:200px; display:none;}
.cur{color:#900;}
</style>
</head>
<body onload="tips();">
<br />
<br />
<br />
<br />
<div class="main" id="article">
本人的曖昧態(tài)度也為此事件更增添了一份迷霧。
</div>
<script type="text/javascript">
function $a(id,tag){var re=(id&&typeof id!="string")?id:document.getElementById(id);if(!tag){return re;}else{return re.getElementsByTagName(tag);}}
function tips(){
var article=$a("article","a")
for(i=0;i<article.length;i++){
if(article[i].className.indexOf("cur")==-1) continue;
article[i].onmouseover=function(e){
var e=e||event;
posX = e.clientX;
posY = e.clientY;
var bodyhe=document.documentElement.clientHeight;
var parwidth=$a("article").offsetWidth;
var tipbox=get_nextSibling(this);
var boxlist=$a("article","span")
for(j=0;j<boxlist.length;j++){
boxlist[j].style.display="none";
boxlist[j].innerHTML="調(diào)入后臺(tái)數(shù)據(jù)"
}
tipbox.style.display="block";
var w=tipbox.offsetWidth-this.offsetWidth;
tipbox.style.left=(this.offsetLeft>parwidth/2?this.offsetLeft-w:this.offsetLeft)+"px";
tipbox.style.top=(posY+tipbox.offsetHeight>bodyhe?this.offsetTop-tipbox.offsetHeight:document.all?this.offsetTop+15:this.offsetTop+this.offsetHeight)+"px";
tipbox.onmouseover=function(){this.style.display="block";}
tipbox.onmouseout=this.onmouseout=function(){tipbox.style.display="none";}
}
}
}
function get_nextSibling(n){
var x=n.nextSibling;
while (x.nodeType!=1){
x=x.nextSibling;
}
return x;
}
</script>
</body>
</html>
- js顯示文本框提示文字的方法
- js+css實(shí)現(xiàn)增加表單可用性之提示文字
- jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
- javascript彈出帶文字信息的提示框效果
- 修改js confirm alert 提示框文字的簡(jiǎn)單實(shí)例
- 基于JS實(shí)現(xiàn)密碼框(password)中顯示文字提示功能代碼
- JS實(shí)現(xiàn)跟隨鼠標(biāo)的鏈接文字提示框效果
- js實(shí)現(xiàn)鼠標(biāo)移到鏈接文字彈出一個(gè)提示層的方法
- js實(shí)現(xiàn)動(dòng)畫特效的文字鏈接鼠標(biāo)懸停提示的方法
- js文字鏈接的熱點(diǎn)提示效果代碼
- 仿百度輸入框智能提示的js代碼
- javascript 輸入框提示列表效果
- JS實(shí)現(xiàn)輸入框提示文字點(diǎn)擊時(shí)消失效果
相關(guān)文章
Marquee配合DIV實(shí)現(xiàn)的文字滾動(dòng)效果代碼
Marquee走馬燈標(biāo)簽,用它能實(shí)現(xiàn)各種各樣的滾動(dòng),而只需修改個(gè)別參數(shù)就行了,在WEB標(biāo)準(zhǔn)時(shí)代,Marquee的作用當(dāng)然也應(yīng)該與時(shí)俱進(jìn)了.2009-11-11JS、CSS文字切換,定時(shí)交替,代碼精簡(jiǎn)
圖片切換很流行,文字切換也很實(shí)用哦,而且用CSS與JavaScript配合,避免純JS實(shí)現(xiàn)時(shí)把文字寫進(jìn)JavaScript代碼里,修改不方便,本款可以說(shuō)是方便,簡(jiǎn)單,效果實(shí)用,兼容性好.2009-10-10JavaScript數(shù)組應(yīng)用 可依次讀取的公告欄文字
一個(gè)JavaScript數(shù)組的應(yīng)用例子,我們將文字存儲(chǔ)于數(shù)組中,然后用循環(huán)依次輸出這些文字,像一條一條的公告一樣,不過(guò)現(xiàn)在都不這么用了,可以學(xué)習(xí)JS數(shù)組,算是一個(gè)實(shí)例參考。2009-10-10javascript跑馬燈懸停放大效果實(shí)現(xiàn)代碼
用過(guò)qq空間的朋友應(yīng)該對(duì)這個(gè)很熟悉吧,效果蠻炫的,不過(guò)它們是用flash實(shí)現(xiàn)的,那么javascript可不可以呢,需要的朋友可以了解下2012-12-12