ASP.NET自定义控件之Tips提示信息控件 (一)

来源:互联网 发布:中国联通云计算公司 编辑:程序博客网 时间:2024/05/22 07:44
 

ASP.NET自定义控件之Tips提示信息控件(一)

2008年3月2日 163 Views
评论发表评论

经常在网页上看到这样的效果:
将鼠标放到某个链接或某个区域,就会在鼠标附近弹出一个提示框,里边显示相关提示信息。

在网上找了大量相关类似代码,但是普遍存在浏览器不兼容的问题。没办法,只好修改。

现在这个控件终于调试得差不多了,贡献出来,大家分享。
控件名称:PageTitleTips

[[控件下载(dll文件)]]

[注:下载包含本人全部控件,全部免费使用,如果您需要额外的服务,请给我发电子邮件:bossma@yeah.net]

先看看效果图:
pagetitletips_view.gif

这个控件的特点:
1、兼容IE和FireFox
2、使用的时候只需从工具箱拖动到页面中一次,页面中具有title属性的可见标签都会有这个效果

主要源代码javascript,如果你不使用本控件,也可以把这个javascript添加到页面中达到同样的目的:

  1. var pltsPop=null;
  2.  var pltsoffsetX = 10;   // 弹出窗口位于鼠标左侧或者右侧的距离;3-12 合适
  3.  var pltsoffsetY = 15// 弹出窗口位于鼠标下方的距离;3-12 合适
  4.  var pltsPopbg="#FFFFEE"; //背景色
  5.  var pltsPopfg="#111111"; //前景色
  6.  var pltsTitle="";
  7. document.write('<div id="pltsTipLayer" style="display: none;position: absolute; z-index:10001"></div>');
  8.  
  9. function pltsinits()
  10.  {
  11.      document.onmouseover = plts;
  12.      document.onmousemove = moveToMouseLoc;
  13.  }
  14.  
  15. function plts(evt)
  16.  { 
  17.      if(evt==null){
  18.         evt=window.event;
  19.        }
  20.       var pltsTipLayer=document.getElementById("pltsTipLayer");
  21.       var o = evt.srcElement?evt.srcElement:evt.target;
  22.       var getClientWidth=(document.documentElement.clientWidth == 0) ? document.body.clientWidth : document.documentElement.clientWidth;
  23.        
  24.      if(o.alt!=null && o.alt!=""){o.dypop=o.alt;o.alt=""};
  25.      if(o.title!=null && o.title!=""){o.dypop=o.title;o.title=""};
  26.      pltsPop=o.dypop;
  27.      if(pltsPop!=null&&pltsPop!=""&&typeof(pltsPop)!="undefined")
  28.      {
  29.         pltsTipLayer.style.left=-1000+"px";
  30.         pltsTipLayer.style.display='';
  31.         var Msg=pltsPop.replace(//n/g,"<br/>");
  32.         Msg=Msg.replace(//0x13/g,"<br/>");
  33.         var re=//{(.[^/{]*)/}/ig;
  34.         if(!re.test(Msg))pltsTitle="简介";
  35.         else{
  36.           re=//{(.[^/{]*)/}(.*)/ig;
  37.             pltsTitle=Msg.replace(re,"$1")+" ";
  38.           re=//{(.[^/{]*)/}/ig;
  39.           Msg=Msg.replace(re,"");
  40.           Msg=Msg.replace("<br/>","");}
  41.                var content =
  42.               '<table id="toolTipTalbe"><tr><td>' +
  43.               '<div id="pltsPoptop" class="tipstitle">' +
  44.               '<div id="topleft"><div style="text-align:left;">↖'+pltsTitle+'</div></div><div id="topright" style="display:none;"><div style="text-align:right;">'+pltsTitle+'↗</div></div>'+
  45.               '</div>'+
  46.               '<div class="tipsbody">'+Msg+'</div>'+
  47.               '<div id="pltsPopbot" class="tipstitle" style="display:none;">'+
  48.               '<div id="botleft"><div style="text-align:left;">↙'+pltsTitle+'</div></div><div id="botright" style="display:none;"><div style="text-align:right;">'+pltsTitle+'↘</div></div>'+
  49.               '</div></td></tr></table>';
  50.                document.getElementById("pltsTipLayer").innerHTML=content;
  51.                document.getElementById("toolTipTalbe").style.width=Math.min(pltsTipLayer.clientWidth,getClientWidth/2.2)+"px";
  52.                moveToMouseLoc(evt);
  53.                return true;
  54.         }
  55.      else
  56.      {
  57.             pltsTipLayer.innerHTML='';
  58.             pltsTipLayer.style.display='none';
  59.             return true;
  60.      }
  61. }
  62.  
  63. function moveToMouseLoc(evt)
  64. {
  65.      if(evt==null){
  66.         evt=window.event;
  67.        }
  68.          var pltsTipLayer=document.getElementById("pltsTipLayer");
  69.         if(pltsTipLayer.innerHTML=='')return true;
  70.         var MouseX = evt.x ? evt.x : evt.pageX;
  71.         var MouseY = evt.y ? evt.y : evt.pageY;
  72.         //window.status=event.y;
  73.         var popHeight=pltsTipLayer.clientHeight;
  74.         var popWidth=pltsTipLayer.clientWidth;
  75.         var popTopAdjust=0;
  76.         var popLeftAdjust=0;
  77.        
  78.         vargetClientHeight=(document.documentElement.clientHeight == 0) ?document.body.clientHeight : document.documentElement.clientHeight;
  79.         vargetClientWidth=(document.documentElement.clientWidth == 0) ?document.body.clientWidth : document.documentElement.clientWidth;
  80.        
  81.         if(MouseY+pltsoffsetY+popHeight>getClientHeight)
  82.         {
  83.                  popTopAdjust=-popHeight-pltsoffsetY*1.5;
  84.                  document.getElementById("pltsPoptop").style.display="none";
  85.                  document.getElementById("pltsPopbot").style.display="";
  86.         }
  87.          else
  88.         {
  89.                  document.getElementById("pltsPoptop").style.display="";
  90.                  document.getElementById("pltsPopbot").style.display="none";
  91.         }
  92.         if(MouseX+pltsoffsetX+popWidth>getClientWidth)
  93.         {
  94.                popLeftAdjust=-popWidth-pltsoffsetX*2;
  95.                document.getElementById("topleft").style.display="none";
  96.                document.getElementById("botleft").style.display="none";
  97.                document.getElementById("topright").style.display="";
  98.                document.getElementById("botright").style.display="";
  99.         }
  100.         else
  101.         {
  102.                document.getElementById("topleft").style.display="";
  103.                document.getElementById("botleft").style.display="";
  104.                document.getElementById("topright").style.display="none";
  105.                document.getElementById("botright").style.display="none";
  106.         }
  107.         var ptleft=0;
  108.         var pttop=0;
  109.         if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
  110.           ptleft = MouseX + pltsoffsetX + popLeftAdjust;
  111.           pttop = MouseY + pltsoffsetY + popTopAdjust;
  112.         } else{
  113.           ptleft = MouseX + pltsoffsetX + document.body.scrollLeft + document.documentElement.scrollLeft + popLeftAdjust;
  114.           pttop = MouseY + pltsoffsetY + document.body.scrollTop + document.documentElement.scrollTop + popTopAdjust;
  115.         }
  116.         pltsTipLayer.style.left = ptleft + "px";
  117.         pltsTipLayer.style.top = pttop + "px";
  118.        return true;
  119. }
  120. pltsinits();

还有一个样式表文件:

  1. #toolTipTalbe{
  2. font-family:宋体;
  3. font-size:12px;
  4. line-height:15px;
  5. FILTER:alpha(opacity=90) shadow(color=#bbbbbb,direction=135);
  6. -moz-opacity:0.9;
  7. opacity:0.9;
  8. border-width:0px;
  9. width:260px;
  10. }
  11. .tipsbody{
  12. background-color: #B1EA45;
  13. padding-left:14px;
  14. padding-right:14px;
  15. padding-top:6px;
  16. padding-bottom:6px;
  17. line-height:135%;
  18. }
  19. .tipstitle{
  20. background-color: #333333;
  21. font-weight:bold;
  22. color:#ffffff;
  23. width:100%;
  24. height:19px;
  25. line-height:19px;
  26. border-width:0px;
  27. }
原创粉丝点击