javascript 實用的文字鏈提示框效果
2024-09-06 12:45:33
供稿:網(wǎng)友
效果要基本實現(xiàn)以下功能:
(1)鼠標(biāo)滑過文章中的鏈接文字,要在相應(yīng)位置彈出提示框,提示框的樣式由css來控制,高度自適應(yīng);鼠標(biāo)可以點擊提示框的中的鏈接,滑離提示框時,框自動消失;
(2)把提示框的位置控制在文本域范圍之內(nèi),如果鏈接文字處在文本域左側(cè),提示框要居右顯示,使其不會出離文本域;反之,如果鏈接文字處在文本域右側(cè),提示框要居左顯示;
(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){
//獲取鼠標(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)入后臺數(shù)據(jù)"
}
//設(shè)置當(dāng)前的提示框顯示;
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";}
}
}
}
//獲取對象元素的下一個標(biāo)簽節(jié)點;
function get_nextSibling(n){
var x=n.nextSibling;
while (x.nodeType!=1){
x=x.nextSibling;
}
return x;
}
貼出源文件代碼 感興趣的朋友可以測試一下,有問題可留言 @&@