html中使用js自定义弹窗


方式一

效果图

image-20230914102804898image-20230914102826064

创建

创建一个alert.js

(function($) {
    $.alerts = {
        // 提示信息
        alert: function(title, message) {
            if( title == null ) title = '系统提示';
            $.alerts._showAlert(title, message);
            // 定时任务,三秒后自动关闭
            window.setTimeout(function (){
                $.alerts._hide();
            },3000)
        },

        // 是否确定提示信息
        confirm: function(title, message, callback) {
            if( title == null ) title = 'Confirm';
            $.alerts._show(title, message, null, 'confirm', function(result) {
                if( callback ) callback(result);
            });
        },

        _showAlert: function(title, msg) {
            var _html = "";
            _html += '<div id="mb_box"></div>' +
                '<div id="mb_con">' +
                    '<span id="mb_ico">关闭</span>' +
                    '<span id="mb_tit">' + title +'</span>';
            _html += '<div id="mb_msg">' + msg + '</div>' +
                     '<div id="mb_btnbox"></div>';
            _html += '</div>';
            //必须先将_html添加到body,再设置Css样式
            $("body").append(_html); GenerateCss();
            $("#mb_ico").click( function() &#123;
                $.alerts._hide();
            &#125;);
        &#125;,

        _show: function(title, msg, value, type, callback) &#123;
            var _html = "";
            _html += '<div id="mb_box"></div>' +
                '<div id="mb_con">' +
                    '<span id="mb_ico">关闭</span>' +
                    '<span id="mb_tit">' + title + '</span>';
            _html += '<div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
            if (type == "alert") &#123;
                _html += '<input id="mb_btn_ok" type="button" value="确定" />';
            &#125;
            if (type == "confirm") &#123;
                _html += '<input id="mb_btn_no" type="button" value="取消" />';
                _html += '<input id="mb_btn_ok" type="button" value="确定" />';
            &#125;
            _html += '</div></div>';
            //必须先将_html添加到body,再设置Css样式
            $("body").append(_html); GenerateCss();
            $("#mb_btn_ok").click( function() &#123;
                $.alerts._hide();
                if( callback ) callback(true);
            &#125;);
            $("#mb_btn_no").click( function() &#123;
                $.alerts._hide();
                if( callback ) callback(false);
            &#125;);
            $("#mb_ico").click( function() &#123;
                $.alerts._hide();
                if( callback ) callback(false);
            &#125;);
            $("#mb_btn_no").focus();
            $("#mb_btn_ok, #mb_btn_no").keypress( function(e) &#123;
                if( e.keyCode == 13 ) $("#mb_btn_ok").trigger('click');
                if( e.keyCode == 27 ) $("#mb_btn_no").trigger('click');
            &#125;);
        &#125;,
        _hide: function() &#123;
            $("#mb_box,#mb_con").remove();
        &#125;
    &#125;

    // Shortuct functions
    myAlert = function(title, message) &#123;
        $.alerts.alert(title, message);
    &#125;

    myConfirm = function(title, message, callback) &#123;
        $.alerts.confirm(title, message, callback);
    &#125;;
    myHide = function() &#123;
        $.alerts._hide();
    &#125;;


    //生成Css
    var GenerateCss = function () &#123;

        $("#mb_box").css(&#123; width: '100%', height: '100%', zIndex: '9999', position: 'fixed',
            filter: 'Alpha(opacity=60)', backgroundColor: 'black', top: '0', left: '0', opacity: '0.6'
        &#125;);

        $("#mb_con").css(&#123; zIndex: '999999', width: '500px',height:'auto', position: 'fixed',borderRadius:'15px',
            backgroundColor: 'White',
        &#125;);

        $("#mb_tit").css(&#123; display: 'block', fontSize: '30px', color: '#444', padding: '10px 15px', textAlign:'center',
            backgroundColor: '#fff', borderRadius: '15px 15px 0 0',
            fontWeight: 'bold'
        &#125;);

        $("#mb_msg").css(&#123; padding: '20px', lineHeight: '40px', textAlign:'center',
            fontSize: '30px' ,color:'#4c4c4c'
        &#125;);

        $("#mb_ico").css(&#123; display: 'block', position: 'absolute', right: '10px', top: '9px',
            border: '1px solid Gray', width: '50px', height: '24px', textAlign: 'center',fontSize: '20px',
            lineHeight: '16px', cursor: 'pointer', borderRadius: '12px', fontFamily: '微软雅黑'
        &#125;);

        $("#mb_btnbox").css(&#123;position: 'relative',top: '-20px',  margin: '30px 10px 10px 0', textAlign: 'center' &#125;);
        $("#mb_btn_ok,#mb_btn_no").css(&#123; width: '80px', height: '45px', color: 'white', border: 'none', borderRadius:'15px'&#125;);
        $("#mb_btn_ok").css(&#123; backgroundColor: '#33acfb',color: '#fff' &#125;);
        $("#mb_btn_no").css(&#123; backgroundColor: 'gray', marginRight: '40px' &#125;);


        //右上角关闭按钮hover样式
        $("#mb_ico").hover(function () &#123;
            $(this).css(&#123; backgroundColor: 'Red', color: 'White' &#125;);
        &#125;, function () &#123;
            $(this).css(&#123; backgroundColor: '#DDD', color: 'black' &#125;);
        &#125;);

        var _widht = document.documentElement.clientWidth; //屏幕宽
        var _height = document.documentElement.clientHeight; //屏幕高

        var boxWidth = $("#mb_con").width();
        var boxHeight = $("#mb_con").height();

        //让提示框居中
        $("#mb_con").css(&#123; top: (_height - boxHeight) / 2 + "px", left: (_widht - boxWidth) / 2 + "px" &#125;);
    &#125;


&#125;)(jQuery);

使用

引入alert.js

使用

function delete()&#123;
    myConfirm('系统确认框',',是否确定删除?',function(r)&#123;
        if(r)&#123;
           delete();
        &#125;
    &#125;);
&#125;
function tip()&#123;
    myAlert("系统提示",'请先选择目的单位!');
&#125;

方式二

ChatGPT生成

效果图

image-20231219103626678

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pretty Popup</title>
    <style>
        /* body &#123;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        &#125; */

        #overlay &#123;
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            opacity: 0;
            transition: opacity 0.3s ease-in-out;
        &#125;

        #popup &#123;
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
            border-radius: 8px;
            opacity: 0;
            transform-origin: center;
            transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
        &#125;

        #popup h2 &#123;
            color: #333;
        &#125;

        #popup p &#123;
            color: #666;
        &#125;

        #popup button &#123;
            margin-right: 10px;
            padding: 8px 16px;
            background-color: #4caf50;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        &#125;

        #popup button:last-child &#123;
            margin-right: 0;
        &#125;

        #popup button:hover &#123;
            background-color: #45a049;
        &#125;
        /* 显示动画 */
        #overlay.active, #popup.active &#123;
            display: block;
            opacity: 1;
        &#125;

        #popup.active &#123;
            transform: translate(-50%, -50%) scale(1);
        &#125;
    </style>
</head>
<body>

    <button onclick="openPopup()">显示带两个按钮的漂亮弹出框</button>

    <div id="overlay" onclick="closePopup()"></div>
    <div id="popup">
        <h2>欢迎使用漂亮的弹出框</h2>
        <p>这是一个简单而漂亮的弹出框示例。</p>
        <div style="text-align:center;">
            <button onclick="closePopup()">取消</button>
            <button onclick="confirmAction()">确定</button>
        </div>
        
    </div>

<script>
    function openPopup() &#123;
        document.getElementById('overlay').classList.add('active');
        document.getElementById('popup').classList.add('active');
    &#125;

    function closePopup() &#123;
        document.getElementById('overlay').classList.remove('active');
        document.getElementById('popup').classList.remove('active');
    &#125;

    function confirmAction() &#123;
        alert("你点击了确定按钮!");
        closePopup(); // 可以根据需要执行其他操作
    &#125;
</script>

</body>
</html>

文章作者: 张一雄
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 张一雄 !
  目录