vue-封装弹出框组件 发表于 2017-12-18 | 分类于 Vue | | 阅读次数 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213<!DOCTYPE html><html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" type="text/css" href="fontFace.css"> <style> [class*=" m-icon-"], [class^=m-icon-] { font-family: element-icons!important; speak: none; font-style: normal; font-weight: 400; font-variant: normal; text-transform: none; line-height: 1; vertical-align: baseline; display: inline-block; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } /*基本样式*/ .m-alert { width: 100%; padding: 8px 16px; margin: 0; box-sizing: border-box; border-radius: 4px; position: relative; background-color: #fff; overflow: hidden; color: #fff; display: table; transition: opacity .2s; margin-top:10px; border: 1px solid #ccc; } .m-content { display: table-cell; padding: 0 8px; } .m-message { font-size: 13px; line-height: 18px; } /*不同状态样式*/ .m-alert-success{ background-color:#13ce66; } .m-alert-info{ background-color:#50bfff; } .m-alert-warning{ background-color:#f7ba2a; } .m-alert-error{ background-color:#ff4949; } /*关闭按钮样式*/ .m-closebtn { font-size: 12px; color: #fff; opacity: 1; top: 12px; right: 15px; position: absolute; cursor: pointer; } .m-icon-close:before { content: "\E60C"; } /*小图标样式*/ .m-icon { font-size: 16px; width: 16px; display: table-cell; color: #fff; vertical-align: middle; } .m-icon-success:before { content: "\E609"; } .m-icon-warning:before { content: "\E615"; } .m-icon-info:before { content: "\E615"; } .m-icon-error:before { content: "\E60B"; } /*自定义图标*/ .m-icon-message:before { content: "\E618"; } .m-icon-menu:before { content: "\E617"; } .m-icon-setting:before { content: "\E61E"; } #app { width: 500px; } </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="app"> <alert type="info" title="这里有一个消息要提示"></alert> <alert type="success" title="成功的消息"></alert> <alert type="error" title="错误的消息"></alert> <alert type="warning" title="警告的消息" :closeable="false" :showicon="true" style="color:#333"></alert> <alert @close-click="closed"> <template slot="messageTemp"> <p>你好张幸</p> <p>你好张幸</p> <p>你好张幸</p> <p>你好张幸</p> <p>你好张幸</p> <p>你好张幸</p> <i slot="iconTemp" class="m-icon m-icon-menu"></i> </template> </alert> </div> <script> /* alert提醒框有四种状态: info success error warning 设置的props: type 提醒框类型 默认为info title 提示信息 '这里有一个消息要提示' closeable 是否禁用关闭 默认为true showicon 是否显示图标 默认为fasle style 设置提醒框样式 默认为{} 定制模板: slot为iconTmp 定制icon模板 slot为titleTmp 定制提示信息结构 监控状态变化: 事件名close-click 点击关闭X触发 */ Vue.component("alert",{ props:{ type:{ type:String, default:"success" }, title:{ type:String, default:"这是一个提醒的消息" }, closeable:{ type:Boolean, default:true }, showicon:{ type:Boolean, default:false }, style:String }, computed:{ classes:function(){ return `m-alert-${this.type}` }, icon:function(){ return `m-icon-${this.type}` } }, template:` <div class="m-alert" :class="[classes]" :style="style" > <slot name="iconTemp"> <i class="m-icon" :class="[icon]" v-if="showicon"></i> </slot> <div class="m-content"> <slot name="messageTemp"> <span class="m-message">{{title}}</span> </slot> <i class="m-closebtn m-icon-close" v-if="closeable" @click="closeHander"></i> </div> </div> `, methods:{ closeHander(){ this.$emit("close-click") } } }) new Vue({ el:"#app", methods:{ closed(){ alert("关闭后执行的行为") } } }) </script> </body></html>