教你手写vue的提示组件

Prompt.vue

<template>
    <div id="prompt" :style="style">
        <span class="close" @click="destroy"></span>

        <p class="msg" :style="{fontSize: fontSize + 'px'}">{{msg}}</p>
        <i class="tip">{{tip}}</i>
    </div>
</template>

<script>
export default {
    name: 'Prompt',
    props: {
        msg: {
            type: String,
            required: true
        },
        tip: {
            type: String,
            default: ''
        },
        side: {
            type: String,
            default: 'right'
        },
        showTime: {
            type: Number,
            default: 3000
        },
        closeSpeed: {
            type: Number,
            default: 5
        },
        style: {
            type: String,
            default: 'backgroundColor: #BEFFF1; width: 260px; height: 100px; fontSize: 24px;'
        }
    },
    data() {
        return {
            length: [...document.querySelectorAll('#prompt')].filter(item => item.style.display !== 'none').length
        }
    },
    methods: {
        destroy() {
            this.$el.style.display = 'none';
        },
        animation() {
            for(let i = 0; i < this.width / 2 + 20; i++) {
                setTimeout(() => {
                    if(this.side === 'left') {
                        this.$el.style.transform = `translateX(-${i * 2}px)`
                    } else if(this.side === 'right') {
                        this.$el.style.transform = `translateX(${i * 2}px)`
                    }
                }, i * this.closeSpeed)
            }
        },
        combine() {
            setTimeout(() => {
                this.animation();
            }, this.showTime)
        
            setTimeout(() => {
                this.destroy();
            }, this.showTime + this.closeSpeed * (this.width / 2 + 20));
        }
    },
    mounted() {
        this.$el.style.bottom = `${this.length * (20 + this.height)}px`;
        
        if(this.side === 'left') {
            this.$el.style.left = '20px';
            this.$el.style.marginLeft = '20px';
        } else if(this.side === 'right') {
            this.$el.style.right = '20px';
            this.$el.style.marginRight = '20px';
        } else {
            throw new Error('[Prompt Error] side must be "left" or "right"');
        }

        this.combine();
    }
}


</script>

<style scoped>
#prompt {
    position: fixed;
    bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    z-index: 999;
    margin-bottom: 20px;
}
.msg {
    font-weight: bold;
}
.tip {
    font-size: 14px;
    color: #999;
    margin-left: 10px;
}
.close {
    position: absolute;
    right: 10px;
    top: 10px;
    width: 20px;
    height: 20px;
    background-color: #444444;
    -webkit-clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
    clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    cursor: pointer;
}
</style>

App.vue

<template>
  <div id="app">
    <button @click="addPrompt">add prompt</button>
    <div class="prompts" v-for="(prompt, index) in prompts" :key="index">
      <prompt :msg="msg" tip="tip"></prompt>
    </div>
  </div>
</template>

<script>
import prompt from './components/Prompt'

export default {
  name: 'App',
  components: {
    prompt
  },
  data() {
    return {
      prompts: [],
      msg: 'Hello Vue!'
    }
  },
  methods: {
    addPrompt() {
      this.prompts.push({})
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
}
</style>
  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值