vue2实现最简toast

最简toast

之前用过基于 jQuery 的无阻塞式 toast,也用过 bootstrap 框架的模式提示框,还用过基于 vue 组件的信息提示框,其实这玩意几行代码就能解决,不用引用一个几十 kb 的 js 和 css 文件,同一个网站信息提示框应该格式统一的,没有复杂的定制要求。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>TOAST 测试</title>
    <script src="vue-2.6.14.min.js"></script>
    <style>
        .toast {
            position: fixed;
            top: 10px;
            right: 10px;
            background-color: #333;
            color: white;
            border-radius: 5px;
            padding: 15px;
            opacity: 0.8;
            transition: opacity 0.35;
        }
    </style>
</head>
<body>
    <div id="app">
        <p>这是 vue 框架下最简单消息弹框插件</p>
        <p>向你的网站访客发送一些易于定制的消息提醒、通知等等</p>
        <p><button @click="test">点我测试 toast</button> </p>
        <div class="toast" v-show="info" v-html="info"></div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: { info: '' },
            methods: {
                toast: function (s, t = 3) { // 参数3为消息停留时间
                    this.info = s;
                    setTimeout(e => { this.info = ''; }, t * 1000);
                },
                test: function () {
                	// 消息框停留5秒后消失
                    this.toast('这是一个 Toast 消息!', 5);
                }
            }
        })
    </script>
</body>
</html>

效果如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yxp_xa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值