|
(1)鼠標滑過文章中的鏈接文字,要在相應位置彈出提示框,提示框的樣式由css來控制,高度自適應;鼠標可以點擊提示框的中的鏈接,滑離提示框時,框自動消失;
(2)把提示框的位置控制在文本域范圍之內,如果鏈接文字處在文本域左側,提示框要居右顯示,使其不會出離文本域;反之,如果鏈接文字處在文本域右側,提示框要居左顯示;
(3)如果文本域內容很多,而鏈接文字恰巧處于瀏覽器底部,為了使提示框不出離于瀏覽器的可視范圍,提示框的位置要自動調整到鏈接文字的上面;
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
復制代碼 代碼如下:
//獲取對象元素的函數;
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元素將不會執行之后的代碼;
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")
//設置文本區域中的span提示框均為隱藏狀態;
for(j=0;j<boxlist.length;j++){
boxlist[j].style.display="none";
boxlist[j].innerHTML="調入后臺數據"
}
//設置當前的提示框顯示;
tipbox.style.display="block";
var w=tipbox.offsetWidth-this.offsetWidth;
/*
以id為article的div添加了相對定位position:relative,所以它已經是提示框的父級;
控制彈出框的顯示位置;
*/
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;
}
貼出源文件代碼 感興趣的朋友可以測試一下,有問題可留言 @&@
復制代碼 代碼如下:
<!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">
早在陳冠希“艷照門”事件爆發之前,陳冠希老爸就曾被傳包養內地男星胡兵。所謂無風不起浪,兩人曾被媒體拍到一起吃飯逛街的鏡頭。而<a href="#" class="cur">胡兵</a><span><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a></span>本人的曖昧態度也為此事件更增添了一份迷霧。早在陳冠希“艷照門”事件爆發之前,陳冠希老爸就曾被傳包養內地男星胡兵。所謂無風不起浪,兩人曾被媒體拍到一起吃飯逛街的鏡頭。而<a href="#" class="cur">胡兵</a><span><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a></span>本人的曖昧態度也為此事件更增添了一份迷霧。 早在陳冠希“艷照門”事件爆發之前,陳冠希老爸就曾被傳包養內地男星胡兵。所謂無風不起浪,兩人曾被媒體拍到一起吃飯逛街的鏡頭。而<a href="#" class="cur">胡兵</a><span><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a><br /><a href="#">雙響炮上海開機</a></span>本人的曖昧態度也為此事件更增添了一份迷霧。
</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="調入后臺數據"
}
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>
JavaScript技術:javascript 實用的文字鏈提示框效果,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。