• HTMl 原生实现简单的toast 消息提示


    一,需求分析

    1,装toast组件容器需要为一个从上往下进行布局的内容
    2,toast组件添加后需要播放消失动画
    消失动画的表现方式
    1,布局往上推,然后缓慢变透明直至消失
    2,消失过程中,后加入的toast位置随着逐渐消失的toast的位置改变而跟着改变

    二,代码实现

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>简易Toast实现</title>
        <style>
            .toast-layout{
                position: fixed;
                 100vw;
                height: 100vh;
                text-align: center;
            }
            .toast .content{
                margin: auto;
                margin-bottom: 16px;
                 max-content;
                padding: 20px;
                text-align: center;
                color: white;
                background-color: black;
                border-radius: 28px;
            }
    
            .hide-toast{
                animation:remove .5s ease-out;
            }
            @keyframes remove {
                from{
                    height: 56px;
                    opacity: 1;
                }
                to{
                    height: 0;
                    opacity: 0;
                }
            }
        </style>
    </head>
    
    <body>
        <script>
            function showToast(content, duration) {
                var toast = document.getElementById("Toast")
                if (toast == null) {
                    var toast = document.createElement("div")
                    toast.setAttribute("id", "Toast")
                    toast.setAttribute("class", "toast-layout")
                    document.body.appendChild(toast)
                    showToast(content, duration)
                    return
                }
                var toastItem = document.createElement("div")
                toastItem.setAttribute("class", "toast")
                toastItem.innerHTML = '<div class="content">' + content + '</div>'
                setTimeout(function () {
                    toastItem.setAttribute('class', 'toast hide-toast')
                }, duration)
                toastItem.onanimationend = function () {
                    toastItem.remove()
                }
                toast.appendChild(toastItem)
    
            }
        </script>
        <button onclick="showToast('你好啊你好啊', 1200)">测试</button>
    </body>
    
    </html>
    

    三,演示

     查看安卓版本

  • 相关阅读:
    常用的算法
    2017前端面试题
    深入了解php opcode缓存原理
    0=='aa'的结果是true
    关于PHP浮点数之 intval((0.1+0.7)*10) 为什么是7
    linux grep命令
    linux awk命令详解
    PHP socket模拟POST请求
    shell编程之sed
    Shell脚本常用判断
  • 原文地址:https://www.cnblogs.com/Dmail/p/16385128.html
Copyright © 2020-2023  润新知