不知道大家有沒有遇到這種情況。平時過于依賴組件庫,如果產(chǎn)品對某個組件有特殊的要求。我們與其去改,去覆蓋組件庫的源碼。還不如自己寫一個組件,畢竟是自己寫的,不管你想怎么改,都方便容易的很。這里就實現(xiàn)一個dialog組件和input組件,不基于任何組件庫。供大家參考
效果展示

在這里插入圖片描述

在這里插入圖片描述
MDialog.vue
<template>
<transition name="fade">
<div class="dialog-mask">
<div class="dialog-wrapper animated fadeInUp" ref="dialogWrapper">
<div class="dialog-container">
<div class="dialog-header">{{title}}</div>
<div class="dialog-body">
<slot></slot>
</div>
<div class="dialog-footer">
<div class="left i-block" @click="dialogClose">{{cancel}}</div>
<div class="right i-block" @click="submit">{{confirm}}</div>
</div>
</div>
</div>
</div>
</transition>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
import '@/../public/css/animate.min.css';
@Component({
components: {}
})
export default class extends Vue {
@Prop()
private title!: string;
@Prop()
private cancel!: string;
@Prop()
private confirm!: string;
private dialogClose() {
this.$emit("close");
}
private submit() {
this.$emit("submit");
}
}
</script>
<style scoped lang="scss">
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.4s;
}
.fade-enter,
.fade-leave-active {
opacity: 0;
}
.dialog-mask {
background-color: rgba(0, 0, 0, 0.5);
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 99;
display: table;
animation-duration: 0.6s;
.dialog-wrapper {
display: table-cell;
vertical-align: middle;
animation-duration: 0.3s;
.dialog-container {
margin: auto;
top: 50%;
background-color: white;
width: 260px;
height: 150px;
border-radius: 12px;
.dialog-header {
background-color: #f3fff7;
color: #42b983;
height: 32px;
border-radius: 12px 12px 0 0;
border-bottom: 1px solid #cccccc;
display: flex;
justify-content: center;
align-items: center;
font-size: 13px;
color: #38c26b;
}
.dialog-body {
display: flex;
justify-content: center;
align-items: center;
margin: 20px 0;
height: 50px;
font-size: 13px;
}
.dialog-footer {
height: 40px;
width: 100%;
background-color: #ffffff;
border-radius: 0 0 12px 12px;
display: table;
.left {
width: 50%;
height: 100%;
vertical-align: middle;
display: table-cell;
font-size: 13px;
color: #38c26b;
}
.right {
width: 50%;
height: 100%;
vertical-align: middle;
display: table-cell;
font-size: 13px;
color: #38c26b;
}
.left:hover {
cursor: pointer;
}
.right:hover {
cursor: pointer;
}
}
}
}
}
</style>
DialogInput.vue
<template>
<div class="input-wrap">
<input
type="text"
:value="value"
:placeholder="placeholder"
@input="$emit('input',$event.target.value)"
/>
</div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
components: {}
})
export default class extends Vue {
@Prop()
private value!: string;
@Prop()
private placeholder!: string;
}
</script>
<style scoped lang="scss">
.input-wrap {
border: 1px solid #56d16a;
border-radius: 5px;
height: 32px;
width: 90%;
background-color: #f3fff7;
display: flex;
align-items: center;
justify-content: center;
input {
background-color: #f3fff7;
font-size: 13px;
height: 25px;
width: 95%;
border: 0px;
text-indent: 3px;
}
}
</style>
父組件中使用
<m-dialog
@close="closeDialog"
@submit="handleSubmit"
v-if="visible"
cancel="取消"
confirm="確定"
title="提示"
>
<dialog-input placeholder="請輸入手機號碼" v-model="value"></dialog-input>
<!-- 您確定要取消該訂單嗎 -->
</m-dialog>
這里script部分是用ts寫的,你可以改成vue的語法。template和style部分不用改。
窗體部分使用了animate.css的動畫,直接下好css,在script導(dǎo)入就可以了。遮罩層使用了transition做fadein fadeout效果