js 自定义吐司 toast 组件


要有遥不可及的梦想,也要有脚踏实地的本事。----------- Grapefruit.Banuit Gang(香柚帮)


       js实现自定义的一个吐司,很简单,直接上代码:

      一 、 首先写布局和样式

              html


<div class="toast_box">
     <p id="toast"></p>
</div>

             css

        
        .toast_box {
            width: 100%;
            position: absolute;
            bottom: 50px;
            justify-content: center;
            display: none;
        }

        .toast_box p {
            box-sizing: border-box;
            padding: 10px 20px;
            max-width: 72%;
            width: max-content;
            background: #000;
            color: #FFF;
            font-size: 16px;
            text-align: center;
            border-radius: 6px;
            opacity: 0.8;
        }

     二、封装吐司的方法


        function toast(text, time) {
            var toast = document.getElementById('toast');
            var toast_box = document.getElementsByClassName('toast_box')[0];
            toast.innerHTML = text;
            toast_box.style.display = 'flex';
            setTimeout(() => {
                toast_box.style.display = 'none';
            }, time)
        }

     三、最后直接在需要的地方调用这个方法就行了,例如:


toast('手机号码不能为空', 3000);

        自定义吐司已经完成,但只是单纯的显示和隐藏似乎是有些不太好看,用户体验会不太好,最终,柚子又加入了一些显示和隐藏的动画效果,会更好看一些,下面贴出所有代码:

        
        <body>
            <div class="toast_box">
                <p id="toast"></p>
            </div>
        </body>

        <style>
            @keyframes show {
                0% {
                    opacity: 0;
                }

                100% {
                    opacity: 1;
                }
            }

            @keyframes hide {
                0% {
                    opacity: 1;
                }

                100% {
                    opacity: 0;
                }
            }

            .toast_box {
                width: 100%;
                position: absolute;
                bottom: 50px;
                justify-content: center;
                display: none;
            }

            .toast_box p {
                box-sizing: border-box;
                padding: 10px 20px;
                max-width: 72%;
                width: max-content;
                background: #000;
                color: #FFF;
                font-size: 16px;
                text-align: center;
                border-radius: 6px;
                opacity: 0.8;
            }
        </style>

        <script>
            function toast(text, time) {
                var toast = document.getElementById('toast');
                var toast_box = document.getElementsByClassName('toast_box')[0];
                toast.innerHTML = text;
                toast_box.style.animation = 'show 1.5s'
                toast_box.style.display = 'flex';
                setTimeout(() => {
                    toast_box.style.animation = 'hide 1.5s'
                    setTimeout(() => {
                        toast_box.style.display = 'none';
                    }, 1400)
                }, time)
            }
        </script>

    好了,就是这些了,希望能帮助到一些有需要的朋友!!!

 

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@Grapefruit

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

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

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

打赏作者

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

抵扣说明:

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

余额充值