当代Web设计方案技术性容许开发设计者迅速完成大多数数访问器适用的动漫。我想警示信息是很普遍的,由于默认设置的JavaScript警示框的款式常常(与你自身设计方案的好看款式)很不融洽很囧。这使开发设计者步入找出哪样处理计划方案能更好地完成更友善的客户页面的路面。
具体演试 – 免费下载源码
创建网页页面
最先, 大家必须建立两个文档取名为“index.html” 和 “style.css”. 我将从Google CDN上启用全新的jQuery 库. HTML是是非非常简易的,由于大家只必须在警示框里添加1些文字. 全部的JavaScript编码被加在了网页页面的底部,这样能够节约HTTP恳求時间.
XML/HTML Code拷贝內容到剪贴板
- <!doctype html>
- <html lang="en-US">
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=utf⑻">
- <title>CSS3 Notification Boxes Demo</title>
- <link rel="shortcut icon" href="http://designshack.net/favicon.ico">
- <link rel="icon" href="http://designshack.net/favicon.ico">
- <link rel="stylesheet" type="text/css" media="all" href="style.css">
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
- </head>
头顶部编码设定了外界启用文档和 HTML5文本文档标准 . 并不是很繁杂由于大家只是创建1个简易的示例. 针对提醒对话框我界定两个不一样的款式 – 取得成功的和不正确的. 也有1些其它设计风格的比如警示框和信息内容框, 可是我沒有建立更多的,由于我更关心的是实际效果.
XML/HTML Code拷贝內容到剪贴板
- <div id="content">
-
- <div class="notify successbox">
- <h1>Success!</h1>
- <span class="alerticon"><img src="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>
-
- <div class="notify errorbox">
- <h1>Warning!</h1>
- <span class="alerticon"><img src="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>
-
- <div class="btns clearfix">
- <a href="#" id="newSuccessBox" class="flatbtn">New Success Box</a>
- <a href="#" id="newAlertBox" class="flatbtn">New Alert Box</a>
- </div>
- </div>
每一个标志文档来自 完全免费的PSD 和UI著作. 这些标志被我调剂为适度的尺寸.怎样你必须警示/信息内容标志你能够变变色调建立成你自身的. 这个类名 .notify 被加上到每个信息DIV上. 它界定了DIV的黑影和字体样式种类.
其它的类比如 .successbox 和 .errorbox 充许大家更改色调和alert对话框里的细节. 你能够看到在我的检测页里载入了两个alert对话框. 每一个网页页面底部的按钮点一下后能够在页左右方追加1个新的alert对话框.
CSS 盒子款式
我不容易将太多 CSS 重设的细节,那些在我以前的实例教程中很明了了。我建立了1个默认设置的排版,并将内嵌 #content 的div垂直居中。这样建立了1个容许jQuery在网页页面最上面加上新警示元素的盒子地区。
CSS Code拷贝內容到剪贴板
-
- 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: .1em 0;
- color: #444;
- position: relative;
- overflow: hidden;
- whitewhite-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: 0 0 0 ⑸0%;
- }
- h1:after {
- left: .5em;
- margin: 0 ⑸0% 0 0;
- }
- h1 > span {
- display: inline-block;
- vertical-align: middle;
- whitewhite-space: normal;
- }
-
- p {
- display: block;
- font-size: 1.35em;
- line-height: 1.5em;
- margin-bottom: 22px;
- }
-
-
-
- #w {
- display: block;
- width: 750px;
- margin: 0 auto;
- padding-top: 30px;
- }
-
- #content {
- display: block;
- width: 100%;
- background: #fff;
- padding: 25px 20px;
- padding-bottom: 35px;
- -webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
- -moz-box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
- box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
- }
-
- .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: 12px 22px 12px 22px;
- line-height: normal;
- text-align: center;
- vertical-align: middle;
- cursor: pointer;
- text-transform: uppercase;
- text-shadow: 0 1px 0 rgba(0,0,0,0.3);
- -webkit-border-radius: 3px;
- -moz-border-radius: 3px;
- border-radius: 3px;
- -webkit-box-shadow: 0 1px 0 rgba(15, 15, 15, 0.3);
- -moz-box-shadow: 0 1px 0 rgba(15, 15, 15, 0.3);
- box-shadow: 0 1px 0 rgba(15, 15, 15, 0.3);
- }
- .flatbtn:hover {
- color: #fff;
- background-color: #c4b237;
- }
- .flatbtn:active {
- -webkit-box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.1);
- -moz-box-shadow:inset 0 1px 5px rgba(0, 0, 0, 0.1);
- box-shadow:inset 0 1px 5px rgba(0, 0, 0, 0.1);
- }
让实际效果更醒目地网页页面合理布局十分简易。任何熟习前端开发网页页面开发设计的人都应当可以将其移殖到自身的款式表格中。我在这个扁平按钮中应用了独特的样如同,并转化成新的警示对话框。一样的,我升级了每一个 .notify类元素的內部款式。
CSS Code拷贝內容到剪贴板
-
- .notify {
- display: block;
- background: #fff;
- padding: 12px 18px;
- max-width: 400px;
- margin: 0 auto;
- cursor: pointer;
- -webkit-border-radius: 3px;
- -moz-border-radius: 3px;
- border-radius: 3px;
- margin-bottom: 20px;
- box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 2px 0px;
- }
-
- .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;
- }
我设定了1些在我的合理布局示例中运作优良的默认设置假定。全部信息通告对话框都被限制为 400px 宽,并根据应用 margin: 0 auto 在网页页面中垂直居中。另外,我升级了电脑鼠标标志为手指指针,这样客户就了解该元素可点一下。大家必须建立1个 jQuery 恶性事件监视器以用于获得客户对撤销通告对话框的点一下,并运作相应涵数。
jQuery动漫
我的JS编码具体实行了两个不一样的实际操作。大家最先检验包括在#content DIV中的任何现有.notify元素。1旦客户点一下这个.notify框元素,大家必须淡出这个通告盒到全透明度为0%(display: none),随后从DOM中移除()此元素。
JavaScript Code拷贝內容到剪贴板
- $(function(){
- $('#content').on('click', '.notify', function(){
- $(this).fadeOut(350, function(){
- $(this).remove();
- });
- });
假如你熟习jQuery,将会最先对这个挑选器觉得一些怪异。大家其实不是挑选#content这个div,而是在找寻这个內容器皿里边的任何.notify通告框。假如你查询1下jQuery的 .on() 方式文本文档,你会留意到大家能够传送此外1个挑选器来做为第2个主要参数,它将在网页页面被3D渲染后升级。 这是1个Stack Overflow里优异的帖子,它十分详尽地解释了这个定义。
我的脚本制作的另外一一部分可能查验客户什么时候点一下了网页页面正下方的两个按钮之1。这两个按钮的ID是#newSuccessBox 和 #newAlertBox。不管客户什么时候点一下,大家会终止载入HREF值的默认设置个人行为,代之以建立1个新的HTML块并外置在网页页面上。
JavaScript Code拷贝內容到剪贴板
-
- $('#newSuccessBox').on('click', function(e){
- e.preventDefault();
- var samplehtml = $('<div class="notify successbox"> <h1>Success!</h1> <span class="alerticon"><img src="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();
- var samplehtml = $('<div class="notify errorbox"> <h1>Warning!</h1> <span class="alerticon"><img src="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');
- });
- ;
每一个涵数都有它自身的自变量,来包括1个我用于警示框的HTML的拷贝/粘贴镜像系统。这个HTML內容储存在1个标识符串选用jQuery挑选器转换为1个目标。我可使用prependTo()方式挑选这个內容DIV使新的警示框出現在网页页面的最上方。全部新的盒子还可以一样的方法被消除,由于它们的HTML编码和用静态数据HTML硬编号的盒子彻底同样。