纯CSS箭头,气泡

来源:互联网 发布:mac口红专柜多少钱一支 编辑:程序博客网 时间:2024/05/01 12:51

原文地址:  CSS Triangles 

演示地址:CSS Triangles Demo 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 本文两种实现方式: 使用或不使用 before 和 :after 伪元素(伪类,pseudo-elements)  
最近重新设计了我的网站,准备添加tooltips提示信息效果.实现很容易,但我想要让提示功能具有三角形的指示图标。
当我重新思考想要所设计的每个图标颜色都随心所欲的时候,采用图片那就是一场灾难。

幸运的是, MooTools 的核心开发者 Darren Waddell介绍了一个强大的技巧给我:CSS三角形.只使用纯CSS语言,你就能创建兼容各个浏览器的三角形,用很少的代码。

最终效果如下图所示:


效果图

不使用伪类的 CSS 代码如下:

[css] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 向上的箭头,类似于A,只有三个边,不能指定上边框 */  
  2. div.arrow-up {  
  3.     width0;   
  4.     height0;   
  5.     border-left5px solid transparent;  /* 左边框的宽 */  
  6.     border-right5px solid transparent/* 右边框的宽 */  
  7.     border-bottom5px solid #2f2f2f/* 下边框的长度|高,以及背景色 */  
  8.     font-size0;  
  9.     line-height0;  
  10. }  
  11.   
  12. /* 向下的箭头 类似于 V */  
  13. div.arrow-down {  
  14.     width0;   
  15.     height0;   
  16.     border-left20px solid transparent;  
  17.     border-right20px solid transparent;  
  18.     border-top20px solid #f00;  
  19.     font-size0;  
  20.     line-height0;  
  21. }  
  22.   
  23. /* 向左的箭头: 只有三个边:上、下、右。而 <| 总体来看,向左三角形的高=上+下边框的长度。 宽=右边框的长度 */  
  24. div.arrow-left {  
  25.     width0;   
  26.     height0;   
  27.     border-bottom15px solid transparent;  /* 下边框的高 */  
  28.     border-top15px solid transparent/* 上方边框的高 */  
  29.     border-right10px solid yellow; /* 右边框的长度|宽度,以及背景色 */  
  30.     font-size0;  
  31.     line-height0;  
  32. }  
  33.   
  34. /* 向右的箭头: 只有三个边:上、下、左。而 |> 总体来看,向右三角形的高=上+下边框的长度。 宽=左边框的长度 */  
  35. div.arrow-right {  
  36.     width0;   
  37.     height0;   
  38.     border-bottom15px solid transparent;  /* 下边框的高 */  
  39.     border-top15px solid transparent/* 上方边框的高 */  
  40.     border-left60px solid green/* 左边框的长度|宽度,以及背景色 */  
  41.     font-size0;  
  42.     line-height0;  
  43. }  

其中的秘密,就是这些三角形在你要指向的方向垂直的两边, 有巨大的边框,而让背面的边框设置为你喜欢的颜色即可。
边框越大,三角形就越大。调整三个边框的长度,就可以构建出各种不同的三角形。如果加上旋转,不知道似的否可以指定各种不同方向的图形?
当然,这个处理方法优越的地方就在于代码量非常少,同时非常灵活。

带有 :before 和 :after 的CSS三角形
前面的例子可以很好的工作,但是如果你想要不只是一个三角形怎么办?比如气泡对话框,那么可以使用伪类来实现CSS三角形箭头,对于弹出的提示信息来说非常完美,代码如下:
[css] view plaincopy在CODE上查看代码片派生到我的代码片
  1. div.tooltip {  
  2.     /* tooltip content styling in here; nothing to do with arrows */  
  3. }  
  4.   
  5. /* shared with before and after */  
  6. div.tooltip:before, div.tooltip:after {  
  7.     content' ';  
  8.     height0;  
  9.     positionabsolute;  
  10.     width0;  
  11.     border10px solid transparent/* arrow size */  
  12. }  
  13.   
  14. /* 向上的箭头 */  
  15. /* top-stacked, smaller arrow */  
  16. div.tooltip:before {  
  17.     border-bottom-color#fff;  /* arrow color */  
  18.   
  19.     /* positioning */  
  20.     positionabsolute;  
  21.     top: -19px;  
  22.     left: 255px;  
  23.     z-index2;  
  24. }  
  25.   
  26. /* arrow which acts as a background shadow */  
  27. div.tooltip:after {  
  28.     border-bottom-color#333;  /* arrow color */  
  29.   
  30.     /* positioning */  
  31.     positionabsolute;  
  32.     top: -24px;  
  33.     left: 255px;  
  34.     z-index1;  
  35. }  

一般来说在箭头的背面边框指定颜色,也可以只使用 :before 或者 :after 之中的一个。而第二个箭头,可以被当作背景边框,或者作为第一个的阴影。
我想问自己为什么不早点知道这种技术。这个优雅的技巧肯定会让我在将来大大的提高制作tooltip元素的水平,同时也为我打开了一个广阔的视野。

完整的页面示例代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr">  
  3. <head>    <title>CSS 箭头Demo</title>  
  4.   
  5.     <style type="text/css">  
  6.         /* 向上的箭头,类似于A,只有三个边,不能指定上边框 */  
  7.         div.arrow-up {  
  8.             width: 0;   
  9.             height: 0;   
  10.             border-left: 5px solid transparent;  /* 左边框的宽 */  
  11.             border-right: 5px solid transparent; /* 右边框的宽 */  
  12.             border-bottom: 5px solid #2f2f2f; /* 下边框的长度|高,以及背景色 */  
  13.             font-size: 0;  
  14.             line-height: 0;  
  15.         }  
  16.           
  17.         /* 向下的箭头 类似于 V */  
  18.         div.arrow-down {  
  19.             width: 0;   
  20.             height: 0;   
  21.             border-left: 20px solid transparent;  
  22.             border-right: 20px solid transparent;  
  23.             border-top: 20px solid #f00;  
  24.             font-size: 0;  
  25.             line-height: 0;  
  26.         }  
  27.           
  28.         /* 向左的箭头: 只有三个边:上、下、右。而 <| 总体来看,向左三角形的高=上+下边框的长度。 宽=右边框的长度 */  
  29.         div.arrow-left {  
  30.             width: 0;   
  31.             height: 0;   
  32.             border-bottom: 15px solid transparent;  /* 下边框的高 */  
  33.             border-top: 15px solid transparent; /* 上方边框的高 */  
  34.             border-right: 10px solid yellow; /* 右边框的长度|宽度,以及背景色 */  
  35.             font-size: 0;  
  36.             line-height: 0;  
  37.         }  
  38.           
  39.         /* 向右的箭头: 只有三个边:上、下、左。而 |> 总体来看,向右三角形的高=上+下边框的长度。 宽=左边框的长度 */  
  40.         div.arrow-right {  
  41.             width: 0;   
  42.             height: 0;   
  43.             border-bottom: 15px solid transparent;  /* 下边框的高 */  
  44.             border-top: 15px solid transparent; /* 上方边框的高 */  
  45.             border-left: 60px solid green; /* 左边框的长度|宽度,以及背景色 */  
  46.             font-size: 0;  
  47.             line-height: 0;  
  48.         }  
  49.   
  50.         /* 基本样式 */  
  51.         .tip {  
  52.             background: #eee;  
  53.             border: 1px solid #ccc;  
  54.             margin-left: 30px;  
  55.             padding: 30px;  
  56.             border-radius: 8px;  
  57.             box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);  
  58.             position: relative;  
  59.             width: 200px;  
  60.         }  
  61.   
  62.         /* 箭头 - :before and :after, 一起组成了好看的气泡小箭头 */  
  63.         .tip:before {  
  64.             position: absolute;  
  65.             display: inline-block;  
  66.             border-top: 17px solid transparent;  
  67.             border-right: 17px solid #eee;  
  68.             border-bottom: 17px solid transparent;  
  69.             border-right-color: rgba(0, 0, 0, 0.2);  
  70.             left: -18px;  
  71.             top: 40px;  
  72.             content: '';  
  73.         }  
  74.   
  75.         /* 背景阴影*/  
  76.         .tip:after {  
  77.             position: absolute;  
  78.             display: inline-block;  
  79.             border-top: 16px solid transparent;  
  80.             border-right: 16px solid #eee;  
  81.             border-bottom: 16px solid transparent;  
  82.             left: -16px;  
  83.             top: 41px;  
  84.             content: '';  
  85.         }  
  86.     </style>  
  87. </head>  
  88. <body>  
  89.   
  90. <div id="contentHolder">  
  91.     <h1>CSS 箭头Demo</h1>  
  92.     <p>以下代码.是极好的纯 CSS 箭头样式,不使用背景图!</p>  
  93.     <div id="position:relative;">  
  94.         <div class="arrow-up">向上的箭头</div>  
  95.         <br />  
  96.         <div class="arrow-down">向下的箭头</div>  
  97.         <br />  
  98.         <div class="arrow-left">向左的箭头</div>  
  99.         <br />  
  100.         <div class="arrow-right">向右的箭头</div>  
  101.     </div>  
  102.       
  103.     <h2>CSS 箭头气泡 ,使用 伪类(Pseudo-Element)</h2>  
  104.     <div style="position:relative;">  
  105.         <div class="tip">  
  106.             企业级开发首选技术是什么?JavaEE和.Net哪个技术更好?在JavaEE开发中主要用哪些框架?另外在移动大热的趋势下如何开发出一个成功的Android产品?  
  107.         </div>  
  108.         <br/>  
  109.         <div class="tip">  
  110.             向左的箭头: 只有三个边:上、下、右。而 < | 总体来看,向左三角形的高=上+下边框的长度。 宽=右边框的长度<br />  
  111.             向右的箭头: 只有三个边:上、下、左。而 |> 总体来看,向右三角形的高=上+下边框的长度。 宽=左边框的长度<br />  
  112.             向上的箭头,类似于A,只有三个边,不能指定上边框   
  113.         </div>  
  114.     </div>  
  115. </div>  
  116. </body>  
  117. </html>  

0 0
原创粉丝点击