css制作对话框
当你发现好多图都能用css画出来的时候,你就会觉得css很有魅力了。//我是这么觉得的,先不考虑什么兼容问题
像漫画里出现的对话框,往往都是一个对话框然后就加入一个箭头指向说话的那一方,来表示这个内容是谁述说的。
今天认真学了一下:相关资料
首先,要知道一个对话框无非就是一个矩形和一个小三角的契合,想起小学时候教的七巧板;
所以简单来说,只要能制作出矩形和小三角即可;
矩形,一个普通块都可以算得上是矩形了,只要有足够的width和height;
那么三角形呢,这才是难点,起初完全没去研究过原来三角形可以是这么回事,在刚开始接触border的时候,只知道给块画个边框然后就知道了块的大小;
但是border的世界真的是博大精深,当给足了它颜色与宽度,它就有了改变世界的能力。

对于上图,实际的实现也是很简单
.div-border{
width: 0px;
height: 0px;
border-top-color: #ccFF99;
border-right-color: #ff0099;
border-bottom-color: #00ff99;
border-left-color: #9900ff;
border-width: 50px;
border-style: solid;
}
.div-trangle{
width: 0px;
height: 0px;
border-color: transparent #000;
border-width: 50px;
border-style: solid;
}
以上的css,两个类,一个就是实现上图的四种颜色,之所以会这样,颜色由border-color来去定义,而border-width则是边框宽度,针对top边框来说,border-width或者是border-top-width可以说就是最上面那个三角形的高,即直角顶点到水平边的垂直距离;//哎呀,我不想说的那么数学化- -
之所以会变成三角形,也就是因为实际的内容width和高度都变成0了,这个其实可以自行在chrome控制台边控制样式,边查看页面效果。
另外一个类主要区别在于border-color里取了transparent,这个值表示透明,效果自行演示咯。
<body>
<div style="height:200px;">
<div class="div-border"> </div>
</div>
<div style="height:200px;"> <div class="div-trangle"> </div>
</div>
</body>
既然能得到三角形,那么怎么让矩形跟三角形位置上的组合呢?这个就要看html的一个文档流和position概念了,其实我也不是很熟。
大概原理是让矩形具有position:relative的属性, 而让三角形归属与矩形块中,并且拥有position:absolute的属性,此后,三角形就可以通过top\right\left\bottom属性值的设置来移动三角形对于其父元素矩形的位置,从而达到完美契合。
<html>
<head>
<title>对话框</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.div-mid{
margin: 0 auto;
width: 800px;
height: 600px;
}
.div-diabox{
width: 200px;
border-style: solid;
border-width: 1px;
border-radius: 10px;
border-color: #CCCC33;
background-color: #FFFF99;
position: relative;
margin: 0 auto;
padding:30px;
top: 30px;
}
.div-diabox .arrow-bottom-out{
width: 0px;
height: 0px;
border-style: solid;
border-color: #CCCC33 transparent transparent transparent;
border-width: 10px;
position: absolute;
top: 79px;
left: 10px;
}
.div-diabox .arrow-bottom-in{
width: 0px;
height: 0px;
border-style: solid;
border-color: #FFFF99 transparent transparent transparent;
border-width: 10px;
position: absolute;
top: 78px;
left: 10px;
}
.div-diabox .arrow-top-out{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent transparent #CCCC33 transparent;
border-width: 10px;
position: absolute;
top: -20px;
right: 10px;
}
.div-diabox .arrow-top-in{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent transparent #FFFF99 transparent;
border-width: 10px;
position: absolute;
top: -19px;
right: 10px;
}
.div-diabox .arrow-right-out{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent transparent transparent #CCCC33;
border-width: 10px;
position: absolute;
top: 10px;
right: -20px;
}
.div-diabox .arrow-right-in{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent transparent transparent #FFFF99;
border-width: 10px;
position: absolute;
top: 10px;
right: -19px;
}
.div-diabox .arrow-left-out{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent #CCCC33 transparent transparent;
border-width: 10px;
position: absolute;
top: 10px;
left: -20px;
}
.div-diabox .arrow-left-in{
width: 0px;
height: 0px;
border-style: solid;
border-color: transparent #FFFF99 transparent transparent;
border-width: 10px;
position: absolute;
top: 10px;
left: -19px;
}
</style>
</head> <body>
<div class="div-mid">
<div class="div-diabox">
<span class="arrow-bottom-out"></span>
<span class="arrow-bottom-in"></span>
雷猴码
</div>
<br/>
<br />
<div class="div-diabox">
<span class="arrow-top-out"></span>
<span class="arrow-top-in"></span>
雷猴码
</div>
<br/>
<br />
<div class="div-diabox">
<span class="arrow-right-out"></span>
<span class="arrow-right-in"></span>
雷猴码
</div>
<br/>
<br />
<div class="div-diabox">
<span class="arrow-left-out"></span>
<span class="arrow-left-in"></span>
雷猴码
</div> </div>
</body>
</html>
效果图:

当时看到这个效果的时候,还没自己实现之前,就有一个疑问,那就是三角形的颜色问题,因为三角形就是一个border,一条边而不是一个块,不能拥有多种颜色,所以实际上一个border只是一个实心颜色的图形,那怎么办呢?
解决方案是复制多一个border三角形,当然颜色要不同,只要让其位置上重叠,底部着漏出一点边,从而达到有边效果的三角形(实际是重叠的两个三角形)。
关于位置移动问题,我觉得自己研究还是挺好玩的,不过就是没拿到诀窍的感觉,还是要自己去算一下。
css制作对话框的更多相关文章
- 【2048小游戏】——CSS/原生js爬坑之纯CSS模态对话框&游戏结束
引言:2048小游戏的结束界面,使用纯CSS制作模态对话框,一般做模态对话框都会使用BootStrap自带的模态对话框组件方便使用,但在制作要运行在移动端的小项目时,就不能使用BootStrap,因为 ...
- CSS制作三角形和按钮
CSS制作三角形和按钮 用上一篇博文中关于边框样式的知识点,能制作出三角形和按钮. 我先说如何制作三角形吧,相信大家在平时逛网站的时候都会看到一些导航栏中的三角形吧,比如说: 网易首页的头部菜单栏中, ...
- 前端开发css实战:使用css制作网页中的多级菜单
前端开发css实战:使用css制作网页中的多级菜单 在日常工作中,大家都会遇到一些显示隐藏类菜单,比如页头导航.二维码显示隐藏.文本提示等等......而这些效果都是可以使用纯css实现的(而且非常简 ...
- css制作漂亮彩带导航条菜单
点击这里查看效果:http://keleyi.com/keleyi/phtml/divcss/17.htm 效果图: 以下是源代码: <!DOCTYPE html PUBLIC "-/ ...
- CSS 制作三角形原理剖析
使用css制作三角形其实原理很简单,下面一步步解析. 1.html代码如下 <div class="triangle"> </div> 2.CSS代码 .t ...
- 纯CSS制作水平垂直居中“十字架”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery & CSS 制作金属质感的选择按钮
如果能把 CSS 运用好,我们创作出好的交互和效果的可能性大大增加.这篇文章中,我想与大家分享一组结合 jQuery & CSS 制作的充满金属质感的选择框效果,绝对是超级精美的效果. 在线演 ...
- DIV+CSS制作二级横向弹出菜单,略简单
没有使用JavaScript控制二级菜单的显示,结果如上图所示. 代码如下: <!DOCTYPE html> <html> <head> <meta char ...
- 纯CSS制作三角(转)
原原文地址:http://www.w3cplus.com/code/303.html 原文地址:http://blog.csdn.net/dyllove98/article/details/89670 ...
随机推荐
- js词法分析
JavaScript的高级知识---词法分析 词法分析 词法分析方法: js运行前有一个类似编译的过程即词法分析,词法分析主要有三个步骤: 分析参数 再分析变量的声明 分析函数说明 函数在运行的瞬间, ...
- Ansible Ubuntu 安装部署
一.安装: $ sudo apt-get install ansible 二.配置: a.基本配置 $ cd /etc/ansible/ $ sudo cp hosts hosts_back 备份一个 ...
- java动态调用webservice
cxf方式 public static Object[] invokeRemoteMethod(String url, String method, Object[] parameters) { Ja ...
- cxf WebService设置wsdl中soapAction的值
用cxf开发一个WebService很简单,只需要下面几步: 1.定义接口 public interface HelloService { String hello(); } 2.实现 public ...
- width:100%;与width:auto;的区别
<div> <p>1111</p> </div> div{ width:980px; background-color: #ccc; height:30 ...
- October 31st Week 45th Monday 2016
While there is life there is hope. 一息若存,希望不灭. Go on living even if there is no hope. Knowing is not ...
- PHP+JQUEY+AJAX实现分页
HTML <div id="list"> <ul></ul> </div> <div id="pagec ...
- Riemann映射定理
单复变函数几何理论最高的成就我想应该属于Riemann映射定理吧! Riemann映射定理:$\mathbb C$中任意边界多余一个点的单连通域$D$都与单位圆盘$B(0,1)$等价,即存在着$D$上 ...
- Linux正则表达式
正则表达示的组成: 一般字符:没有特殊意义的字符 特殊字符(meta字符):元字符,有在正则表达式中有特殊意义 正则表达式中常见的meta字符 POSIX BRE与ERE中都有的meta字符 \ 通常 ...
- 18.Java泛型
1.为什么需要泛型 List list1=new ArrayList(Arrays.asList(new String("string"),new Integer(20))); S ...