javascript 實用的文字鏈提示框效果
更新時間:2010年06月30日 15:08:43 投稿:mdxy-dxy
鼠標滑過文章中的鏈接文字,要在相應位置彈出提示框,提示框的樣式由css來控制,高度自適應;鼠標可以點擊提示框的中的鏈接,滑離提示框時,框自動消失
效果要基本實現(xiàn)以下功能:
(1)鼠標滑過文章中的鏈接文字,要在相應位置彈出提示框,提示框的樣式由css來控制,高度自適應;鼠標可以點擊提示框的中的鏈接,滑離提示框時,框自動消失;
(2)把提示框的位置控制在文本域范圍之內(nèi),如果鏈接文字處在文本域左側,提示框要居右顯示,使其不會出離文本域;反之,如果鏈接文字處在文本域右側,提示框要居左顯示;
(3)如果文本域內(nèi)容很多,而鏈接文字恰巧處于瀏覽器底部,為了使提示框不出離于瀏覽器的可視范圍,提示框的位置要自動調(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
復制代碼 代碼如下:
//獲取對象元素的函數(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元素將不會執(zhí)行之后的代碼;
if(article[i].className.indexOf("cur")==-1) continue;
article[i].onmouseover=function(e){
//獲取鼠標指針在瀏覽器可視區(qū)域的坐標,不受文檔內(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")
//設置文本區(qū)域中的span提示框均為隱藏狀態(tài);
for(j=0;j<boxlist.length;j++){
boxlist[j].style.display="none";
boxlist[j].innerHTML="調(diào)入后臺數(shù)據(jù)"
}
//設置當前的提示框顯示;
tipbox.style.display="block";
var w=tipbox.offsetWidth-this.offsetWidth;
/*
以id為article的div添加了相對定位position:relative,所以它已經(jīng)是提示框的父級;
控制彈出框的顯示位置;
*/
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";}
}
}
}
//獲取對象元素的下一個標簽節(jié)點;
function get_nextSibling(n){
var x=n.nextSibling;
while (x.nodeType!=1){
x=x.nextSibling;
}
return x;
}
貼出源文件代碼 感興趣的朋友可以測試一下,有問題可留言 @&@
復制代碼 代碼如下:
<!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)入后臺數(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實現(xiàn)增加表單可用性之提示文字
- jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
- javascript彈出帶文字信息的提示框效果
- 修改js confirm alert 提示框文字的簡單實例
- 基于JS實現(xiàn)密碼框(password)中顯示文字提示功能代碼
- JS實現(xiàn)跟隨鼠標的鏈接文字提示框效果
- js實現(xiàn)鼠標移到鏈接文字彈出一個提示層的方法
- js實現(xiàn)動畫特效的文字鏈接鼠標懸停提示的方法
- js文字鏈接的熱點提示效果代碼
- 仿百度輸入框智能提示的js代碼
- javascript 輸入框提示列表效果
- JS實現(xiàn)輸入框提示文字點擊時消失效果
相關文章
JavaScript數(shù)組應用 可依次讀取的公告欄文字
一個JavaScript數(shù)組的應用例子,我們將文字存儲于數(shù)組中,然后用循環(huán)依次輸出這些文字,像一條一條的公告一樣,不過現(xiàn)在都不這么用了,可以學習JS數(shù)組,算是一個實例參考。2009-10-10