栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

纯CSS实现箭头、气泡让提示功能具有三角形图标

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

纯CSS实现箭头、气泡让提示功能具有三角形图标

演示地址:CSS Triangles Demo
本文两种实现方式: 使用或不使用 before 和 :after 伪元素(伪类,pseudo-elements)

最近重新设计了我的网站,准备添加tooltips提示信息效果.实现很容易,但我想要让提示功能具有三角形的指示图标。
当我重新思考想要所设计的每个图标颜色都随心所欲的时候,采用图片那就是一场灾难。
幸运的是, MooTools 的核心开发者 Darren Waddell介绍了一个强大的技巧给我:CSS三角形.只使用纯CSS语言,你就能创建兼容各个浏览器的三角形,用很少的代码。

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

复制代码代码如下:

div.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid #2f2f2f;
font-size: 0;
line-height: 0;}

div.arrow-down {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-top: 20px solid #f00;
font-size: 0;
line-height: 0;
}

div.arrow-left {
width: 0;
height: 0;
border-bottom: 15px solid transparent;
border-top: 15px solid transparent;
border-right: 10px solid yellow;
font-size: 0;
line-height: 0;
}

div.arrow-right {
width: 0;
height: 0;
border-bottom: 15px solid transparent;
border-top: 15px solid transparent;
border-left: 60px solid green;
font-size: 0;
line-height: 0;
}

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

带有 :before 和 :after 的CSS三角形
前面的例子可以很好的工作,但是如果你想要不只是一个三角形怎么办?比如气泡对话框,那么可以使用伪类来实现CSS三角形箭头,对于弹出的提示信息来说非常完美,代码如下:

复制代码代码如下:
div.tooltip {

}

div.tooltip:before, div.tooltip:after {
content: ' ';
height: 0;
position: absolute;
width: 0;
border: 10px solid transparent;
}


div.tooltip:before {
border-bottom-color: #fff;

position: absolute;
top: -19px;
left: 255px;
z-index: 2;
}

div.tooltip:after {
border-bottom-color: #333;

position: absolute;
top: -24px;
left: 255px;
z-index: 1;
}

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

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

复制代码代码如下:


CSS 箭头Demo


div.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid #2f2f2f;
font-size: 0;
line-height: 0;
}

div.arrow-down {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-top: 20px solid #f00;
font-size: 0;
line-height: 0;
}

div.arrow-left {
width: 0;
height: 0;
border-bottom: 15px solid transparent;
border-top: 15px solid transparent;
border-right: 10px solid yellow;
font-size: 0;
line-height: 0;
}

div.arrow-right {
width: 0;
height: 0;
border-bottom: 15px solid transparent;
border-top: 15px solid transparent;
border-left: 60px solid green;
font-size: 0;
line-height: 0;
}

.tip {
background: #eee;
border: 1px solid #ccc;
padding: 10px;
border-radius: 8px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
position: relative;
width: 200px;
}

.tip:before {
position: absolute;
display: inline-block;
border-top: 7px solid transparent;
border-right: 7px solid #eee;
border-bottom: 7px solid transparent;
border-right-color: rgba(0, 0, 0, 0.2);
left: -8px;
top: 20px;
content: '';
}

.tip:after {
position: absolute;
display: inline-block;
border-top: 6px solid transparent;
border-right: 6px solid #eee;
border-bottom: 6px solid transparent;
left: -6px;
top: 21px;
content: '';
}




CSS 箭头Demo

以下代码.是极好的纯 CSS 箭头样式,不使用背景图!



向上的箭头

向下的箭头

向左的箭头

向右的箭头

CSS 箭头气泡 ,使用 伪类(Pseudo-Element)


企业级开发首选技术是什么?JavaEE和.Net哪个技术更好?在JavaEE开发中主要用哪些框架?另外在移动大热的趋势下如何开发出一个成功的Android产品?




向左的箭头: 只有三个边:上、下、右。而 < | 总体来看,向左三角形的高=上+下边框的长度。 宽=右边框的长度

向右的箭头: 只有三个边:上、下、左。而 |> 总体来看,向右三角形的高=上+下边框的长度。 宽=左边框的长度

向上的箭头,类似于A,只有三个边,不能指定上边框





转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/217550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号