草庐IT

jQuery模态弹窗插件(jquery-confirm)

邕州阁主 2023-04-15 原文

前言

今天为大家分享一款开源的非常轻量且精美的jQuery模态弹窗插件:jquery-confirm,它包含Bootstrap,Material等多种主题供选择。

如果你的前端项目中还在使用jQuery,那么jquery-confirm是你模态弹窗的完美选择。

下面我们就来零距离感受一下jquery-confirm的魅力吧。

jquery-confirm官网地址:https://craftpip.github.io/jquery-confirm/
jquery-confirm源码托管地址:https://github.com/craftpip/jquery-confirm

功能特性

jquery-confirm包含了前端开发中界面的多种交互功能,如:

  • Alerts(提示信息)
  • Confirmation(弹窗确认)
  • Prompt(弹窗输入)
  • Dialog(对话框)
  • Asynchronous content(异步加载内容)
  • Auto-close(窗口自动关闭)
  • Keystrokes(快捷键)
  • Animations(动画)
  • Draggable(拖拽窗口)
  • Theme(切换主题)

安装

jquery-confirm组件包含两个资源文件,分别为:样式文件jquery-confirm.min.css和脚本文件jquery-confirm.min.js。

你可以通过CDN直接引用,如下:

登录后复制

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>

或者通过Bower安装:

登录后复制

$ bower install craftpip/jquery-confirm

或者通过npm安装:

登录后复制

$ npm install jquery-confirm

注:jquery-confirm依赖于jQuery,如果你还需要响应式布局,则还依赖Bootstrap。

主题

jquery-confirm提供了多种精美的主题,如:

Modern风格:

Material风格:

Bootstrap风格:

Supervan风格:

开始使用

简单的提示信息($.alert)

使用$.alert()函数即可弹出简单的提示信息,如下:

登录后复制

$.alert({

    title: 'Alert!',

    content: 'Simple alert!',

});

确认弹窗($.confirm)

使用$.confirm()函数可以打开一个确认消息的弹窗,如下:

登录后复制

$.confirm({

    title: 'Confirm!',

    content: 'Simple confirm!',

    buttons: {

        confirm: function () {

            $.alert('Confirmed!');

        },

        cancel: function () {

            $.alert('Canceled!');

        },

        somethingElse: {

            text: 'Something else',

            btnClass: 'btn-blue',

            keys: ['enter', 'shift'],

            action: function(){

                $.alert('Something else?');

            }

        }

    }

});

$.confirm()函数还可以自定义content的内容,让其变成一个弹出的输入框,如下:

登录后复制

$.confirm({

    title: 'Prompt!',

    content: '' +

    '<form action="" class="formName">' +

    '<div class="form-group">' +

    '<label>Enter something here</label>' +

    '<input type="text" placeholder="Your name" class="name form-control" required />' +

    '</div>' +

    '</form>',

    buttons: {

        formSubmit: {

            text: 'Submit',

            btnClass: 'btn-blue',

            action: function () {

                var name = this.$content.find('.name').val();

                if(!name){

                    $.alert('provide a valid name');

                    return false;

                }

                $.alert('Your name is ' + name);

            }

        },

        cancel: function () {

            //close

        },

    },

    onContentReady: function () {

        // bind to events

        var jc = this;

        this.$content.find('form').on('submit', function (e) {

            // if the user submits the form by pressing enter in the field.

            e.preventDefault();

            jc.$$formSubmit.trigger('click'); // reference the button and click it

        });

    }

});

对话框($.dialog)

调用$.dialog()函数可以弹出一个对话框,如下:

登录后复制

$.dialog({

    title: 'Text content!',

    content: 'Simple modal!',

});

以上三种弹窗都提供更简单的参数调用,如:

登录后复制

$.alert('Content here', 'Title here');

$.confirm('A message', 'Title is optional');

$.dialog('Just to let you know');

弹窗按钮

在$.confirm()函数中,你还可以自定义多个按钮,包括按钮的文本,样式,回调等等参数,如下:

登录后复制

$.confirm({

    buttons: {

        hello: function(helloButton){

            // shorthand method to define a button

            // the button key will be used as button name

        },

        hey: function(heyButton){

            // access the button using jquery

            this.$$hello.trigger('click'); // click the 'hello' button

            this.$$hey.prop('disabled', true); // disable the current button using jquery method

            // jconfirm button methods, all methods listed here

            this.buttons.hello.setText('Helloooo'); // setText for 'hello' button

            this.buttons.hey.disable(); // disable with button function provided by jconfirm

            this.buttons.hey.enable(); // enable with button function provided by jconfirm

            // the button's instance is passed as the first argument, for quick access

            heyButton === this.buttons.hey

        },

        heyThere: {

            text: 'Hey there!', // text for button

            btnClass: 'btn-blue', // class for the button

            keys: ['enter', 'a'], // keyboard event for button

            isHidden: false, // initially not hidden

            isDisabled: false, // initially not disabled

            action: function(heyThereButton){

                // longhand method to define a button

                // provides more features

            }

        },

    }

});

比如,自定义按钮的文本:

登录后复制

$.confirm({

    buttons: {

        hey: function () {

            // here the button key 'hey' will be used as the text.

            $.alert('You clicked on "hey".');

        },

        heyThere: {

            text: 'hey there!', // With spaces and symbols

            action: function () {

                $.alert('You clicked on "heyThere"');

            }

        }

    }

});

自定义按钮的样式:

登录后复制

$.confirm({

    buttons: {

        info: {

            btnClass: 'btn-blue',

            action: function(){}

        },

        danger: {

            btnClass: 'btn-red any-other-class', // multiple classes.

            //...

        },

        warning: {

            btnClass: 'btn-warning',

            //...

        },

    }

});

绑定按钮的快捷键:

登录后复制

$.confirm({

    content: 'Time to use your keyboard, press shift, alert, A or B',

    buttons: {

        specialKey: {

            text: 'On behalf of shift',

            keys: ['shift', 'alt'],

            action: function(){

                $.alert('Shift or Alt was pressed');

            }

        },

        alphabet: {

            text: 'A, B',

            keys: ['a', 'b'],

            action: function(){

                $.alert('A or B was pressed');

            }

        }

    }

});

自定义

自定义对话框类型

在jquery-confirm组件中,你可以通过设置type参数来自定义对话框的类型,如:

登录后复制

$.confirm({

    title: 'Encountered an error!',

    content: 'Something went downhill, this may be serious',

    type: 'red',

    typeAnimated: true,

    buttons: {

        tryAgain: {

            text: 'Try again',

            btnClass: 'btn-red',

            action: function(){

            }

        },

        close: function () {

        }

    }

});

自定义对话框的图标

通过指定icon,可以自定义对话框的图标,如:

登录后复制

$.confirm({

    icon: 'glyphicon glyphicon-heart',

    title: 'glyphicon'

});

$.confirm({

    icon: 'fa fa-warning',

    title: 'font-awesome'

});

$.confirm({

    icon: 'fa fa-spinner fa-spin',

    title: 'Working!',

    content: 'Sit back, we are processing your request!'

});

自定义对话框的宽度

如果使用了Bootstrap响应式布局,你还可以应用Bootstrap的column样式来自定义对话框的宽度,如:

登录后复制

$.confirm({

    columnClass: 'small'

});

$.confirm({

    columnClass: 'col-md-4 col-md-offset-4',

});

$.confirm({

    columnClass: 'col-md-12'

});

$.confirm({

    columnClass: 'col-md-4 col-md-offset-8 col-xs-4 col-xs-offset-8',

    containerFluid: true, // this will add 'container-fluid' instead of 'container'

});

如果没有引用Bootstrap样式,则必需设置jconfirm.defaults.useBootstrap = false,然后再自定义对话框宽度,如:

登录后复制

$.confirm({

    boxWidth: '30%',

    useBootstrap: false,

});

$.confirm({

    boxWidth: '500px',

    useBootstrap: false,

});

可拖拽窗口

jquery-confirm实现可拖拽窗口非常简单,只需要设置draggable参数的值为true即可,如:

登录后复制

$.confirm({

    title: 'Hello there',

    content: 'click and hold on the title to drag',

    draggable: true,

});

默认情况下,拖拽不受浏览器窗口的限制,也就是说你可以把窗口拖拽到浏览器可视区域外,如:

登录后复制

$.confirm({

    title: 'Hello there',

    content: 'Drag this modal out of the window',

    draggable: true,

    dragWindowBorder: false,

});

如果想要窗口在浏览器可视区域内拖拽,则设置dragWindowGap参数,如下:

登录后复制

$.confirm({

    title: 'Hello there',

    content: 'try to drag this modal out of the window',

    draggable: true,

    dragWindowGap: 0, // number of px of distance

});

Ajax加载

jquery-confirm还支持Ajax加载,你只需要设置content参数以url:为前缀即可,如:

登录后复制

$.confirm({

    title: 'Title',

    content: 'url:text.txt',

    onContentReady: function () {

        var self = this;

        this.setContentPrepend('<div>Prepended text</div>');

        setTimeout(function () {

            self.setContentAppend('<div>Appended text after 2 seconds</div>');

        }, 2000);

    },

    columnClass: 'medium',

});

Esc键关闭

jquery-confirm支持快捷键Esc键来关闭对话框,如:

登录后复制

$.confirm({

    escapeKey: true,

    backgroundDismiss: false,

});

$.confirm({

    escapeKey: 'buttonName',

    buttons: {

        buttonName: function(){

            $.alert('Button name was called');

        },

        close: function(){

        }

    }

});

内置的回调函数

jquery-confirm内置了许多回调函数,如:

登录后复制

$.confirm({

    title: false,

    content: 'url:callback.html',

    onContentReady: function () {

        // when content is fetched & rendered in DOM

        alert('onContentReady');

        var self = this;

        this.buttons.ok.disable();

        this.$content.find('.btn').click(function(){

            self.$content.find('input').val('Chuck norris');

            self.buttons.ok.enable();

        });

    },

    contentLoaded: function(data, status, xhr){

        // when content is fetched

        alert('contentLoaded: ' + status);

    },

    onOpenBefore: function () {

        // before the modal is displayed.

        alert('onOpenBefore');

    },

    onOpen: function () {

        // after the modal is displayed.

        alert('onOpen');

    },

    onClose: function () {

        // before the modal is hidden.

        alert('onClose');

    },

    onDestroy: function () {

        // when the modal is removed from DOM

        alert('onDestroy');

    },

    onAction: function (btnName) {

        // when a button is clicked, with the button name

        alert('onAction: ' + btnName);

    },

    buttons: {

        ok: function(){

        }

    }

});

全局参数配置

在jquery-confirm组件被正确加载之后,你可以自定义配置它的全局参数,jquery-confirm提供的全局参数如下:

登录后复制

jconfirm.defaults = {

    title: 'Hello',

    titleClass: '',

    type: 'default',

    typeAnimated: true,

    draggable: true,

    dragWindowGap: 15,

    dragWindowBorder: true,

    animateFromElement: true,

    smoothContent: true,

    content: 'Are you sure to continue?',

    buttons: {},

    defaultButtons: {

        ok: {

            action: function () {

            }

        },

        close: {

            action: function () {

            }

        },

    },

    contentLoaded: function(data, status, xhr){

    },

    icon: '',

    lazyOpen: false,

    bgOpacity: null,

    theme: 'light',

    animation: 'scale',

    closeAnimation: 'scale',

    animationSpeed: 400,

    animationBounce: 1,

    rtl: false,

    container: 'body',

    containerFluid: false,

    backgroundDismiss: false,

    backgroundDismissAnimation: 'shake',

    autoClose: false,

    closeIcon: null,

    closeIconClass: false,

    watchInterval: 100,

    columnClass: 'col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1',

    boxWidth: '50%',

    scrollToPreviousElement: true,

    scrollToPreviousElementAnimate: true,

    useBootstrap: true,

    offsetTop: 40,

    offsetBottom: 40,

    bootstrapClasses: {

        container: 'container',

        containerFluid: 'container-fluid',

        row: 'row',

    },

    onContentReady: function () {},

    onOpenBefore: function () {},

    onOpen: function () {},

    onClose: function () {},

    onDestroy: function () {},

    onAction: function () {}

};

有关jQuery模态弹窗插件(jquery-confirm)的更多相关文章

  1. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  2. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  3. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  4. ruby-on-rails - 您希望看到哪些 Rails 插件? - 2

    您认为可以作为插件很好地存在于您的Rails应用程序中必须实现的哪些行为?您过去曾搜索过哪些插件功能但找不到?哪些现有的Rails插件可以改进或扩展,如何改进或扩展? 最佳答案 我希望在管理界面中看到一个引擎插件,它提供了应用程序中所有模型的仪表板摘要,以及可配置的事件图表。 关于ruby-on-rails-您希望看到哪些Rails插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questio

  5. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  6. ruby-on-rails - 禁用设备的 :confirmable on-the-fly to batch-generate users - 2

    Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation

  7. ruby-on-rails - 在 Rails 中自定义 "Password confirmation doesn' t 匹配密码 - 2

    有没有办法在Rails中为确认字段自定义消息?例如在设计中我必须输入密码和password_confirmation并且错误消息是:Passwordconfirmationdoesn'tmatchPassword我可以更改事件记录语言环境消息(“不匹配”),但它会在该语言环境消息的开头和结尾输出密码确认和密码,所以我得到如下内容:"PasswordconfirmationmustmatchPassword"有没有办法将其更改为不同的字符串?PasswordconfirmationandPasswordmustmatch.编辑另一件事是拥有完全自定义的消息,例如:'Setpassword

  8. ruby - vagrant 从 github 安装插件 - 2

    我们正在使用Vagrant进行部署,我们最终希望将此集群部署在Rackspace上。vagrant-rackspace插件是一个自然的选择,但它有一些错误,这些错误未包含在最新的0.1.1版本中(notablythatvagrantprovisiondoesn'twork)。我已经在我的personalfork中解决了这个问题通过合并其他人的工作来对存储库进行改造。是否可以从github安装vagrant插件?显而易见的事情没有奏效:[unix]$vagrantplugininstallvagrant-rackspace--plugin-sourcehttps://github.com

  9. IDEA使用LeetCode插件 - 2

    前言我们习惯用idea编写、调试代码,在LeetCode上刷题时,如果能够在IDEA编写代码,并且做好代码管理,是一件事半功倍的事情。对于后续复习题目,做笔记也会非常便利。本文目的在于介绍LeetCodeEditor的使用,以及配置工具类,最终目录结构如下:note:放置笔记src:放置代码leetcode.editor.cn:插件LeetCodeEditor自动生成utils:自定义的工具包,可用于自动化输入测试用例,定义题目需要的类(结构体)out:运行测试时自动生成LeetCodeEditorGitHub:https://github.com/shuzijun/leetcode-edit

  10. regex - Ruby 是否有类似于 Perl 6 语法的插件? - 2

    多年来,Perl一直是我首选的编程语言工具之一。Perl6语法看起来像是一个很棒的语言特性。我想知道是否有人开始为Ruby做这样的事情。 最佳答案 如果您想在Ruby中使用实际的Perl6语法,最好的选择是Cardinal,Parrot上的ruby​​编译器。它目前尚未完成并且非常缓慢,但我非常希望它最终成为一个可行的ruby​​实现。它目前大部分处于非事件状态,等待Parrot中的一些基础架构更改以支持改进的解析速度和其他功能。 关于regex-Ruby是否有类似于Perl6语法的插件

随机推荐