教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript文字链提示框效果的实现代码

javascript文字链提示框效果的实现代码

发布时间:2014-12-09   编辑:jiaochengji.com
本文介绍下,用javascript实现文字链提示框效果的例子,有需要的朋友,参考下吧。

要求实现如下的文字链效果:
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>文字提示框效果_www.jiaochengji.com</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">
教程集,www.jiaochengji.com,欢迎大家的光临!教程集,www.jiaochengji.com!而<a href="#" class="cur">胡兵</a><span><a href="#">双响炮上海开机</a><br /><a href="#">双响炮上海开机</a><br /><a href="#">双响炮上海开机</a></span>本人的暧昧态度也为此事件更增添了一份迷雾。教程集,www.jiaochengji.com,欢迎大家的光临!教程集,www.jiaochengji.com!而<a href="#" class="cur">胡兵</a><span><a href="#">双响炮上海开机</a><br /><a href="#">双响炮上海开机</a><br /><a href="#">双响炮上海开机</a></span>本人的暧昧态度也为此事件更增添了一份迷雾。 教程集,www.jiaochengji.com,欢迎大家的光临!教程集,www.jiaochengji.com!而<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>

您可能感兴趣的文章:
js特效带滚动提示的链接
js弹出层之1:JQuery.Boxy (二)
js控制文本框自动适应文本长度的例子
js点击超链接弹出提示框
弹出对话框实现重定向(php js)
网页特效滚动链接效果
JavaScript编程基础(13)-框架编程
input 输入框获得/失去焦点时隐藏/显示文字(jquery版)
js实现带按钮的提示框
javascript文字无间断滚动效果(文字上下无缝滚动)

[关闭]
~ ~