亲们,第一次写技术博客,只为和大家共同分享技术,共同进步。有写得不周到、考虑不全面的地方,大家提出来,咱们一起进步。

    今天跟大家分享一下带箭头的浮层提示框。效果图如下:

效果图

    这种效果在各大网站上经常见到,比方说百度主页实时热点、谷歌注册表单提示。一般用作表单、链接、头像的弹出提示,带个小箭头,用户体验很好。

    浮层的制作很简单,不过是盒子模型,有点基础都能制作出来,关键是箭头的制作。很多人用图片式的箭头。这当然可以,而且图片可以附带很多例如外阴影、半透明之类的效果,很炫。不过对于一个PV超过百万的网站,哪怕减少一次客户机与服务器的交互所带来的提速效果都是很明显的。网页中能通过计算得到的效果,尽量不要用图片代替。下面讲解下如何实现箭头。

    看下面代码:

   


  
  1. <!DOCTYPE HTML> 
  2. <html lang="en-US"> 
  3. <head> 
  4.     <meta charset="UTF-8"> 
  5.     <title>网页测试</title> 
  6. </head> 
  7. <body> 
  8.     <div style="width:0px;height:0px;border-width:50px;border-color:red blue yellow black;border-style:solid;">
  9. </div> 
  10. </body> 
  11. </html> 

    我们得到这样一个效果:

   

    可以看到对于宽高都为0的块元素,四个边框分别是四个三角形。那么我们现在要做的就是屏蔽其它三个方向的三角,只取其中一个三角。

 


  
  1. <!--    方法一    --> 
  2. <div class="arrow"></div> 
  3. <style> 
  4.     .arrow{ 
  5.         border-width:10px; 
  6.         border-style:solid; 
  7.         border-color:transparent; 
  8.         border-left-color:#bbb; 
  9.         height:0; 
  10.     } 
  11. </style> 
  12. <!--    方法二    --> 
  13. <div class="arrow"></div> 
  14. <style> 
  15.     .arrow{ 
  16.         border-width:10px 0 10px 10px; 
  17.         border-style:solid; 
  18.         border-color:transparent #bbb; 
  19.         height:0; 
  20.     } 
  21. </style> 

    方法一很明显先将border-color都设置为transparent;再将要显示的三角设置为指定颜色。方法二是谷歌注册页面采用的样式方式。原理都一样,不解释。

    现在我们得到了一个颜色值为#bbb的左宽右窄的三角。但我们要做的是一个中间挖空的三角。那么若是在这个三角上面覆盖一个白色的小三角,是不是就得到了想要的效果了。当然啦。。。注:上面height:0;的设置是为了解决IE6下兼容性问题。

    现在要做的是,用绝对定位将一个小的白三角覆盖到颜色值为#bbb的三角上。

    代码如下:

   


  
  1. <div class="arrow"> 
  2.     <div class="arrow-before"></div> 
  3.     <div class="arrow-after"></div> 
  4. </div> 
  5. <style> 
  6.     .arrow{ 
  7.         position:absolute; 
  8.     } 
  9.     .arrow-before{ 
  10.         position:absolute; 
  11.         border-width:10px; 
  12.         border-style:solid; 
  13.         border-color:transparent; 
  14.         border-left-color:#bbb; 
  15.         height:0; 
  16.     } 
  17.     .arrow-after{ 
  18.         position:absolute; 
  19.         top:2px; 
  20.         border-width:8px; 
  21.         border-style:solid; 
  22.         border-color:transparent; 
  23.         border-left-color:#fff; 
  24.         height:0; 
  25.     } 
  26. </style> 

    .arrow块做成了绝对定位,可以定位到网页中任何方向。

    那么如果想要一个倾斜的三角呢?也很简单。把上下左右四个border-width设置不一样就行了,具体效果自己调节。

    下面是最开始效果图的代码。

 


  
  1. <!DOCTYPE HTML> 
  2. <html lang="en-US">  
  3. <head> 
  4.     <meta charset="UTF-8"> 
  5.     <title>谷歌表单验证</title> 
  6.     <script type="text/javascript" src="../common/jquery-1.7.2.min.js"></script> 
  7.     <script type="text/javascript" src="js/common.js"></script> 
  8.     <link rel="stylesheet" href="css/index.css" /> 
  9.     <style type="text/css"> 
  10.         html{color:#333;direction:ltr;font:81.25%/1 arial,helvetica,sans-serif;} 
  11.     </style> 
  12.     <script type="text/javascript"> 
  13. //      $(function(){ 
  14. //          $rl.tools.test(); 
  15. //      }) 
  16.     </script> 
  17. </head> 
  18. <body> 
  19.     <div id="rl-zc"> 
  20.         <div id="rl-zc-box"> 
  21.             <form action="#"> 
  22.                 <!--    姓名  --> 
  23.                 <div class="form-element"> 
  24.                     <span class="form-element-title">姓名</span> 
  25.                     <div class="form-clear"> 
  26.                         <div class="form-div-float"> 
  27.                             <input type="text" /> 
  28.                             <span class="placeholder-text">姓氏</span> 
  29.                             <div class="infomsg"> 
  30.                             您可以使用字母、数字和英文句点。 
  31.                             <div class="arrow"> 
  32.                                 <div class="arrow-before"></div> 
  33.                                 <div class="arrow-after"></div> 
  34.                             </div> 
  35.                         </div> 
  36.                         </div> 
  37.                         <div class="form-div-margin-left"> 
  38.                             <input type="text" /> 
  39.                             <span class="placeholder-text">名字</span> 
  40.                             <div class="infomsg"> 
  41.                                 您可以使用字母、数字和英文句点。 
  42.                                 <div class="arrow"> 
  43.                                     <div class="arrow-before"></div> 
  44.                                     <div class="arrow-after"></div> 
  45.                                 </div> 
  46.                             </div> 
  47.                         </div> 
  48.                     </div> 
  49.                     <span id="rl-lastname" class="errormsg"></span> 
  50.                     <span id="rl-firstname" class="errormsg"></span> 
  51.                 </div> 
  52.                 <!--    用户名 --> 
  53.                 <div class="form-element"> 
  54.                     <span class="form-element-title">请选择您的用户名</span> 
  55.                     <div class="form-clear"> 
  56.                         <input type="text" style="width:300px;"/> 
  57.                         <span class="placeholder-text" style="left:auto;right:0;">@gmail.com</span> 
  58.                         <div class="infomsg"> 
  59.                             您可以使用字母、数字和英文句点。 
  60.                             <div class="arrow"> 
  61.                                 <div class="arrow-before"></div> 
  62.                                 <div class="arrow-after"></div> 
  63.                             </div> 
  64.                         </div> 
  65.                     </div> 
  66.                 </div> 
  67.             </form> 
  68.         </div> 
  69.     </div> 
  70.     <script type="text/javascript"> 
  71.     /*  $("input[type='text']").bind('focus',function(){ 
  72.             $(this).addClass("input-hover"); 
  73.         }).bind('blur',function(){ 
  74.             $(this).removeClass("input-hover"); 
  75.         }); 
  76.         $(".placeholder-text").click(function(){ 
  77.             $(this).prev("input[type='text']").focus().addClass("input-hover"); 
  78.         }).hover(function(){ 
  79.             $(this).prev("input[type='text']").focus().addClass("input-hover"); 
  80.         },function(){ 
  81.             $(this).prev("input[type='text']").focus().removeClass("input-hover"); 
  82.         });*/ 
  83.         $(".infomsg").parent().find("input").focus(function(){ 
  84.             var thisDom = $(this).parent().find(".infomsg"); 
  85.             var width = thisDom.width(); 
  86.             var height = thisDom.height(); 
  87.             var height_input = $(this).height() + 2; 
  88.             thisDom.css({"left":-width-45   /*,"top": -(height-height_input)/2*/}); 
  89.             thisDom.find(".arrow").css({"top":"20px"}); 
  90.             thisDom.show().animate({opacity:1},100); 
  91.         }).blur(function(){ 
  92.             var thisDom = $(this).parent().find(".infomsg"); 
  93.             thisDom.animate({opacity:0},100,function(){ 
  94.                 thisDom.hide(); 
  95.         }); 
  96.     }); 
  97.     </script> 
  98. </body> 
  99. </html> 

   

index.css文件

  
  1. input,button,select,textarea{outline:none;} 
  2. textarea{font-size:13px;resize:none;} 
  3.  
  4.  
  5. #rl-zc{width:362px;margin:0 auto;} 
  6. #rl-zc-box{background:none repeat scroll 0 0 #f1f1f1;border:1px solid #E5E5E5;margin:0;padding:25px;} 
  7. #rl-zc-box .form-element{margin:0 0 1.5em;position:relative;} 
  8. #rl-zc-box .form-element span.form-element-title{font-weight:bold;display:block;margin:0 0 0.6em;} 
  9.  
  10.  
  11. .form-div-margin-left{margin:0 5px 0 155px;position:relative;} 
  12. .form-div-float{float:left;width:150px;margin-right:5px;position:relative;} 
  13. .form-clear{position:relative;} 
  14. .form-clear:after{clear:both;content:"";height:0;display:block;} 
  15. input[type='text']{height:27px;padding:0 8px;margin:0;border-width:1px
  16.     border-style:solid;border-color:#c0c0c0 #d9d9d9 #d9d9d9;width:130px
  17.     background:none repeat scroll 0 0 #fff;*line-height:27px;border-radius:1px;} 
  18. input[type='text']:hover{box-shadow:0 1px 2px rgba(0,0,0,0.1inset;border-width:1px
  19.     border-style:solid;border-color:#a0a0a0 #b9b9b9 #b9b9b9
  20.     border-radius:2px;} 
  21. .input-hover{box-shadow:0 1px 2px rgba(0,0,0,0.1inset;border-width:1px
  22.     border-style:solid;border-color:#a0a0a0 #b9b9b9 #b9b9b9
  23.     border-radius:2px;}  
  24. .placeholder-text{position:absolute;top:8px;left:10px;color:#999;} 
  25. .errormsg{color:#dd4b39;display:block;line-height:17px;margin:0.5em 0 0;}    
  26. .infomsg{z-index:9999;position:absolute;padding:15px;background:none repeat scroll 0 0 #fff
  27.         color:#797979;margin:0.5em 0 0;line-height:17px;display:none
  28.         width:255px;border-style:solid;border-width:1px;border-color:#bbb #bbb #a8a8a8
  29.         box-shadow:0 1px 3px rgba(0,0,0,0.2);top:-20px;opacity:0
  30.         } 
  31. .arrow{position:absolute;right:0;} 
  32. .arrow-before{position:absolute;border-style:solid;border-color:transparent #bbb
  33.     border-width:9px 0 9px 9px;}         
  34. .arrow-after{position:absolute;border-style:solid;border-color:transparent #fff
  35.     border-width:8px 0 8px 8px;top:1px;} 

    上面只是实现了简单的应用,扩展性不大,只适合特定的HTML结构和特定的网页。我会试着把它做成组件。可以很好的适应html结构的变化。

    PS:前端很缺人,至少北京如此。大家加油认真学吧。新的技术如HTML5、CSS3.0也给web带来了新的用户体验。桌面应用开发已经逐渐式微,未来的互联网必然是web应用和移动终端应用开发的世界。不过,新的渲染交互技术也让前端逐渐繁琐、复杂。不过,没关系,谁让我们喜欢它呢?就让它淘气一点吧!!!