巧妙使用 CSS3 的褪色和动画效果制作消息提醒框
现代Web设计技术允许开发者快速实现大多数浏览器支持的动画。我想警告消息是很常见的,因为默认的JavaScript警告框的样式往往(与你自己设计的漂亮样式)很不协调很囧。这使开发者步入找出哪种解决方案能更好地实现更友好的用户界面的道路。
在这个教程中我想解释一下我们如何能把几个将要出现在网页上方的CSS3通知框放在一起。用户可以点击这些通知框使它们逐渐淡出消失,最终将他们从DOM中移除。作为一个有趣的附加功能,我还包括了一个按钮,你可以点击它来添加一个新的警告框到页面顶部。你可以下载查看一下我的示例演示,以对我们将做的事情有一个更好的了解。
建立页面
首先, 我们需要创建两个文件命名为“index.html” 和 “style.css”. 我将从Google CDN上调用最新的jQuery库. HTML是非常简单的,因为我们只需要在警告框里加入一些文本. 所有的JavaScript代码被加在了页面的底部,这样可以节省HTTP请求时间.
| 1 2 3 4 5 6 7 8 9 10 | <!doctype html><htmllang="en-US"><head>  <metahttp-equiv="Content-Type"content="text/html;charset=utf-8">  <title>CSS3 Notification Boxes Demo</title>  <linkrel="stylesheet"type="text/css"media="all"href="style.css">  <scripttype="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head> | 
头部代码设置了外部调用文件和 HTML5文档规范 . 不是很复杂因为我们只是建立一个简单的示例. 对于提示窗口我定义两个不同的样式 – 成功的和错误的. 还有一些其它风格的例如警告框和信息框, 但是我没有创建更多的,因为我更关注的是效果.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <divid="content">  <!-- Icons source http://dribbble.com/shots/913555-Flat-Web-Elements -->  <divclass="notify successbox">    <h1>Success!</h1>    <spanclass="alerticon"><imgsrc="images/check.png"alt="checkmark"/></span>    <p>Thanks so much for your message. We check e-mail frequently and will try our best to respond to your inquiry.</p>  </div>    <divclass="notify errorbox">    <h1>Warning!</h1>    <spanclass="alerticon"><imgsrc="images/error.png"alt="error"/></span>    <p>You did not set the proper return e-mail address. Please fill out the fields and then submit the form.</p>  </div>    <p>Click the error boxes to dismiss with a fading effect.</p>    <p>Add more by appending dynamic HTML into the page via jQuery. Plus the notifications are super easy to customize.</p>    <divclass="btns clearfix">    <ahref="#"id="newSuccessBox"class="flatbtn">New Success Box</a>    <ahref="#"id="newAlertBox"class="flatbtn">New Alert Box</a>  </div></div><!-- @end #content --> | 
每个图标文件来自 免费的PSD 和UI作品. 这些图标被我调整为适当的大小.如何你需要警告/信息图标你可以变变颜色创建成你自己的. 这个类名 .notify 被添加到每一个消息DIV上. 它定义了DIV的阴影和字体类型.
其它的类例如 .successbox 和 .errorbox 充许我们改变颜色和alert窗口里的细节. 你可以看到在我的测试页里加载了两个alert窗口. 每个页面底部的按钮点击后可以在页上下方追加一个新的alert窗口.
CSS 盒子样式
我不会将太多 CSS 重置的细节,那些在我之前的教程中很明了了。我创建了一个默认的排版,并将内置 #content 的div居中。这样创建了一个允许jQuery在页面最上面添加新警告元素的盒子区域。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | /** typography **/h1{  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;  font-size: 2.5em;  line-height: 1.5em;  letter-spacing: -0.05em;  margin-bottom: 20px;  padding: .1em0;  color: #444;    position: relative;    overflow: hidden;    white-space: nowrap;    text-align: center;}h1:before,h1:after {  content: "";  position: relative;  display: inline-block;  width: 50%;  height: 1px;  vertical-align: middle;  background: #f0f0f0;}h1:before {      left: -.5em;  margin: 000-50%;}h1:after {      left: .5em;  margin: 0-50%00;}h1> span {  display: inline-block;  vertical-align: middle;  white-space: normal;}p {  display: block;  font-size: 1.35em;  line-height: 1.5em;  margin-bottom: 22px;}/** page structure **/#w {  display: block;  width: 750px;  margin: 0auto;  padding-top: 30px;}#content {  display: block;  width: 100%;  background: #fff;  padding: 25px20px;  padding-bottom: 35px;  -webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px1px2px0px;  -moz-box-shadow: rgba(0, 0, 0, 0.1) 0px1px2px0px;  box-shadow: rgba(0, 0, 0, 0.1) 0px1px2px0px;}.flatbtn {  -webkit-box-sizing: border-box;  -moz-box-sizing: border-box;  box-sizing: border-box;  display: inline-block;  outline: 0;  border: 0;  color: #f9f8ed;  text-decoration: none;  background-color: #b6a742;  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);  font-size: 1.2em;  font-weight: bold;  padding: 12px22px12px22px;  line-height: normal;  text-align: center;  vertical-align: middle;  cursor: pointer;  text-transform: uppercase;  text-shadow: 01px0rgba(0,0,0,0.3);  -webkit-border-radius: 3px;  -moz-border-radius: 3px;  border-radius: 3px;  -webkit-box-shadow: 01px0rgba(15, 15, 15, 0.3);  -moz-box-shadow: 01px0rgba(15, 15, 15, 0.3);  box-shadow: 01px0rgba(15, 15, 15, 0.3);}.flatbtn:hover {  color: #fff;  background-color: #c4b237;}.flatbtn:active {  -webkit-box-shadow: inset01px5pxrgba(0, 0, 0, 0.1);  -moz-box-shadow:inset01px5pxrgba(0, 0, 0, 0.1);  box-shadow:inset01px5pxrgba(0, 0, 0, 0.1);} | 
让效果更醒目的网页布局非常简单。任何熟悉前端网页开发的人都应该能够将其移植到自己的样式表中。我在这个扁平按钮中使用了特殊的样好似,并生成新的警告窗口。同样的,我更新了每个 .notify类元素的内部样式。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** notifications **/.notify {  display: block;  background: #fff;  padding: 12px18px;  max-width: 400px;  margin: 0auto;  cursor: pointer;  -webkit-border-radius: 3px;  -moz-border-radius: 3px;  border-radius: 3px;  margin-bottom: 20px;  box-shadow: rgba(0, 0, 0, 0.3) 0px1px2px0px;}.notify h1{ margin-bottom: 6px; }.successbox h1{ color: #678361; }.errorbox h1{ color: #6f423b; }.successbox h1:before, .successbox h1:after { background: #cad8a9; }.errorbox h1:before, .errorbox h1:after { background: #d6b8b7; }.notify .alerticon {   display: block;  text-align: center;  margin-bottom: 10px;} | 
我设置了一些在我的布局示例中运行良好的默认假设。所有消息通知窗口都被限定为 400px 宽,并通过使用 margin: 0 auto 在页面中居中。同时,我更新了鼠标图标为手指指针,这样用户就知道该元素可点击。我们需要创建一个 jQuery 事件监听器以用于获取用户对取消通知窗口的点击,并运行相应函数。
jQuery动画
我的JS代码实际执行了两个不同的操作。我们首先检测包含在#content DIV中的任何现有.notify元素。一旦用户点击这个.notify框元素,我们需要淡出这个通知盒到透明度为0%(display: none),然后从DOM中移除()此元素。
| 1 2 3 4 5 6 | $(function(){  $('#content').on('click', '.notify', function(){    $(this).fadeOut(350, function(){      $(this).remove(); // after fadeout remove from DOM    });  }); | 
如果你熟悉jQuery,可能首先对这个选择器感到有些奇怪。我们并不是选择#content这个div,而是在寻找这个内容容器里面的任何.notify通知框。如果你查看一下jQuery的 .on() 方法文档,你会注意到我们可以传递另外一个选择器来作为第二个参数,它将在页面被渲染后更新。 这是一个Stack Overflow里出色的帖子,它非常详细地解释了这个概念。
我的脚本的另一部分将会检查用户何时点击了页面下方的两个按钮之一。这两个按钮的ID是#newSuccessBox 和 #newAlertBox。无论用户何时点击,我们会停止加载HREF值的默认行为,代之以创建一个新的HTML块并前置在页面上。
| 1 2 3 4 5 6 7 8 9 10 |   // handle the additional windows  $('#newSuccessBox').on('click', function(e){    e.preventDefault();    varsamplehtml = $('<div class="notify successbox"> <h1>Success!</h1> <span class="alerticon"><img src="data:images/check.png" alt="checkmark" /></span> <p>You did not set the proper return e-mail address. Please fill out the fields and then submit the form.</p> </div>').prependTo('#content');  });  $('#newAlertBox').on('click', function(e){    e.preventDefault();    varsamplehtml = $('<div class="notify errorbox"> <h1>Warning!</h1> <span class="alerticon"><img src="data:images/error.png" alt="error" /></span> <p>You did not set the proper return e-mail address. Please fill out the fields and then submit the form.</p> </div>').prependTo('#content');  });}); | 
每个函数都有它自己的变量,来包含一个我用于警告框的HTML的复制/粘贴镜像。这个HTML内容存储在一个字符串中用jQuery选择器转化为一个对象。我可以使用prependTo()方法选择这个内容DIV使新的警告框出现在页面的最上方。所有新的盒子也可以同样的方式被解除,因为它们的HTML代码和用静态HTML硬编码的盒子完全相同。
英文原文:Building CSS3 Notification Boxes With Fade Animations
巧妙使用 CSS3 的褪色和动画效果制作消息提醒框的更多相关文章
- 使用 CSS3 动感的图片标题动画效果【附源码下载】
		在网站中,有很多地方会需要在图片上显示图片标题.使用 CSS3 过渡和变换可以实现动感的鼠标悬停显示效果.没有使用 JavaScript,所以只能在支持 CSS3 动画的现代浏览器中才能正常工作.您可 ... 
- 基于jQuery CSS3鼠标点击动画效果
		分享基于jQuery CSS3鼠标点击动画效果支持图片或内容滑动,允许设置动画延迟效果.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="co ... 
- anime.js 实战:实现一个带有描边动画效果的复选框
		在网页或者是APP的开发中,动画运用得当可以起到锦上添花的作用.正确使用动画,不但可以有助于用户理解交互的作用,还可以大大提高网页应用的魅力和使用体验.并且在现在的网页开发中,动画已经成为了一个设计的 ... 
- CSS3展现精彩的动画效果 css3的动画属性
		热火朝天的css3无疑吸引了很多前端开发者的眼球,然而在css3中的动画属性则是新功能中的主打招牌,说到css3的动画属性不得不让人想起这三个属性:Transform﹑Transition﹑Anima ... 
- css3中outline切换动画效果
		今天刚看了篇文章<纯CSS实现的outline切换transition动画效果> 里面的效果研究了一下,下图为实现时的效果 代码如下: <!DOCTYPE html> < ... 
- transition:all 0.5s linear;进度条动画效果 制作原理
		Html: <span class="progress"><b ><i></i></b><em>50< ... 
- css3 使用animation实现动画效果
		.rotation { -webkit-animation-name: rotation; -webkit-animation-duration: 30s; -moz-animation-name: ... 
- CSS3知识之立方体动画效果
		效果图: 
- 纯CSS3实现GIF图片动画效果
		在线演示 本地下载 
随机推荐
- openssl 获取证书中的公钥
			PEM 格式 1. FILE *fp = fopen("xx.pem", "r"); 2. X509 *cert = PEM_read_X509(fp, N ... 
- webpack3.0+总结
			ul>li{ color:blue; font-size:20px } .items>ul>li>ul>li{ color:crimson } --> webpac ... 
- windows环境下模仿Linux环境发起curl请求
			1.到官网下载curl工具包 2.在curl.exe目录中使用(使用方式1) 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的目录. 由于博主使用的是windows 64位 的系统,因 ... 
- CodeForces 785D Anton and School - 2
			枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ... 
- java变量的命名使用规则
			1.环境变量通常是指在操作系统中,用来指定操作系统运行时需要的一些参数 2.变量名以字母.下划线或者美元符(4上面的¥)开头,不能以数字开头,后面跟字母.下划线.美元符.数字,变量名对大小写敏感,无长 ... 
- java抽象类、多态、接口
			抽象类 抽象类的产生 当编写一个类时,我们往往会为该类定义一些方法,这些方法是用来描述该类的功能具体实现方式,那么这些方法都有具体的方法体. 但是有的时候,某个父类只是知道子类应该包含怎么样的方法,但 ... 
- mybatis递归,一对多代码示例
			今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题, 由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list集合. 虽然实现了,感觉毕竟,太low ... 
- hdu1028 Ignatius and the Princess III(生成函数整理占坑)upd 已咕
			先咕着 ---------------2018 5 22---------------------- 题解 生成函数处理整数拆分 code #include<cstdio> #includ ... 
- 去除List中的重复元素
			/** * 去重list中的重复元素 * @param list * @return */ public static <T> List<T> removeRepeat(Lis ... 
- bzoj1375 双调路径
			Description 来越多,因此选择最佳路径是很现实的问题.城市的道路是双向的,每条道路有固定的旅行时间以及需要支付的费用.路径由连续的道路组成.总时间是各条道路旅行时间的和,总费用是各条道路所支 ... 
