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

news2025/7/13 20:08:03

前言

今天为大家分享一款开源的非常轻量且精美的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 () {}

};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/405375.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

IDEA如何运行SpringBoot项目(超详细截图)

【辰兮要努力】&#xff1a;hello你好我是辰兮&#xff0c;很高兴你能来阅读&#xff0c;昵称是希望自己能不断精进&#xff0c;向着优秀程序员前行&#xff01; 博客来源于项目以及编程中遇到的问题总结&#xff0c;偶尔会有读书分享&#xff0c;我会陆续更新Java前端、后台、…

vue中组件的props属性(详)

今天这篇文章&#xff0c;让你彻底学会props属性…… props主要用于组件的传值&#xff0c;他的工作就是为了接收外面传过来的数据&#xff0c;与data、el、ref是一个级别的配置项。 问题一&#xff1a;那props具体是怎么使用呢&#xff1f;原理又是什么呢&#xff1f;往下看…

css绘制3D炫动ikun

今天做一个3D版的ikun。 先准备图片一张 代码浅析 页面整体div.contrainer,舞台div.stage,控制盒子div.control,图片盒子div.imgWrap,js载入div.img列表。 <div class"contrainer"><div class"stage"><div class"control">…

vue项目引入svg图标(完整步骤)

1. 安装svg依赖 在vue中首先需要安装可以加载svg的依赖。 npm安装&#xff1a;npm install svg-sprite-loader --save-dev 2. 创建svg文件夹存放svg图标 创建icons文件夹&#xff0c;在icons文件夹下创建svg文件夹存放本地svg图标。 3. vue.config.js 中配置svg图片 vue.c…

vue中使用echarts实现动态数据绑定、获取后端接口数据

之前几篇echarts的文章是实现了静态的柱状图、折线图、饼状图、地图&#xff0c;在项目中我们肯定是需要获取后端接口&#xff0c;将后端返回的数据显示在图表上&#xff0c;所以这次就记录一下如何实现echarts的动态数据绑定。 简单来讲&#xff0c;就是从接口获取到的数据&a…

Vue生命周期总结(四个阶段,八个钩子函数)

生命周期就是组件或者实例&#xff0c;从创建到被销毁&#xff08;初始化化数据、编译模板、挂载DOM、渲染一更新一渲染、卸载&#xff09;的一系列过程&#xff0c;我们称这是Vue的生命周期 文章目录一、Vue的生命周期阶段二、生命周期钩子函数1. beforeCreate2. created3. be…

ES6+--》熟知JS中的async函数

目录 async函数 await 表达式 async使用形式 async读取文件 async发送AJAX请求 与生成器(Generator)相比 async函数 async函数的返回值为 promise 对象&#xff0c;promise对象的结果由async函数执行的返回值决定。async函数能使得异步操作变得更加方便&#xff0c;简而…

前端开发常用哪些工具软件?

前端开发必备工具&#xff0c;一篇文章一网打尽 文章目录 一、前端提高“生产力”工具 1.WebStorm 2. 远程开发 - VSCode 3. 接口测试 - Postman 4.API在线文档生成和测试 - SwaggerUI 5.抓包工具 - Wireshark 6.通用数据库管理 - DBeaver 7.MD编辑器 - Typora 8.虚拟…

【数字孪生】UE4虚幻引擎与前端Web页面的结合

目录介绍基础准备鼠标穿透设置备注介绍 UE初学者&#xff0c;非专业UE工程师&#xff0c;在项目中需要使用UE4结合前端页面完成三维场景与前端图表的联动效果&#xff0c;自学总结方法&#xff0c;使用的版本为UE4.26。 基础准备 1. 使用Vue、Echarts创建前端页面&#xff0…

异常:TypeError: ‘caller‘, ‘callee‘, and ‘arguments‘ properties may not be accessed on strict mode func

异常&#xff1a;TypeError: ‘caller‘, ‘callee‘, and ‘arguments‘ properties may not be accessed on strict mode func 问题解决 今天我在给博客添加樱花飘落的特效的时候 下载并引入了一个JS 之后打包执行的时候 发现樱花不会动了 检查报错发现是文章标题的报错…

事件监听 页面滚动(页面滚动到某一位置时显示/隐藏某元素,Vue环境)

目录 一、效果展示 二、实现步骤 三、涉及要点 1. Vue 语法 v-show 2. 获取窗口到元素顶端的距离 3. 监听事件 一、效果展示 最近在做项目时有一个网页渲染是这样的&#xff0c;某一个元素在开始不显示&#xff0c;只有当页面滑动到指定的位置时才显示该元素。效果如下&a…

基于物联网的智慧农业监测系统(前端界面有web端和微信小程序端)

摘要 农业是国民经济的基础&#xff0c;在国家经济发展中有着不可替代的重要作用。随着物联网技术的快速发展&#xff0c;智慧农业已成为了现代农业发展的新方向。基于此&#xff0c;本文设计并实现了一套基于物联网的智慧农业监测系统&#xff0c;系统采用ESP32作为主控板&am…

Vue组合式API

目录 一. 为什么要使用Composition API 1.1.一个Options API实例 1.2.Options API存在的问题 1.3.Composition API简介 二.Composition API 2.1.setup()入口 2.2.ref 响应式监听 2.3.reactive与toRefs 2.4.computed的用法 2.5.watch的用法 2.6.setup()参数 2.6.1.p…

前端如何将项目部署到服务器(Nginx)

文章目录一、准备环境二、安装Nginx1、 安装Nginx依赖2、下载Nginx3、解压下载好的Nginx 压缩包4、编译安装Nginx5、启动Nginx服务三、操作步骤1、使用Xshell连接服务器2、上传静态资源文件3、 配置Nginx4、 重启Nginx服务我们在会开发项目的同时&#xff0c;也应该了解一下前端…

基于Java Web的随意购商城系统(开源项目)

提示&#xff1a;此项目仅作为本博主的学习笔记记录&#xff0c;不作为商品售卖&#xff0c;资源往下翻看源码获取 文章目录前言Web端功能设计首页热销商品新到商品商品分类商品详情购物车添加地址提交订单部分代码展示可能会出现的错误如果拿到项目后发现图片不显示源码获取前…

在Vue中使用高德地图

在Vue中使用高德地图一、如何在Vue中引入基础高德地图1、步骤一&#xff1a;注册并登录高德地图开放平台&#xff0c;申请密钥2、步骤二&#xff1a;安装高德地图加载器3、封装一个自定义地图组件&#xff0c;并初始化地图二、根据关键词搜索&#xff0c;并定位到搜索的位置三、…

谷歌浏览器自带翻译网页插件没用了怎么办?这里有解决办法。

前言 正当我打算来一波科学上网的时候&#xff0c;当我用谷歌浏览器打开文档网站时候&#xff0c;发现发现google浏览器网页翻译插件没用了。经过了我一段时间的搜寻&#xff0c;终于有了解决方案。 原因 从 10 月 20 日起&#xff0c;谷歌在陆续移除国内服务器上的谷歌翻译…

Vue学习之从入门到神经(两万字收藏篇)

写在前面 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家:人工智能学习网站 Vue写在前面前言Vue.js三种安装方式一、 Vue导入二、Vue基本语法1.钩子函数2. 插值表达式3.显示数据(v-text和v-html)4.数据双向…

深入理解Vue响应式原理

前言 Vue响应式原理是Vue最独特的特性之一&#xff0c;当数据模型进行修改时&#xff0c;视图就会进行更新&#xff0c;这使得状态管理简单直接&#xff0c;但是其底层的细节还是需要我们深入学习理解&#xff0c;这样遇到一些问题我们才能快速进行定位&#xff0c;并解决&…

【UML】-- 顺序图练习题含答案(自动售货机、学生选课、提款机、购买地铁票、洗衣机工作)

注意&#xff1a;对象表示法对象名需要下划线&#xff0c;此文章没有标注 一、练习一 根据下面的叙述&#xff0c;绘制一幅关于顾客从自动售货机中购买物品的顺序图。顾客&#xff08;User&#xff09;先向自动售货机的前端&#xff08;Front&#xff09;投币&#xff1b;售货…