bootstrap强化模态窗口插件
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | <!DOCTYPE html> <html lang= "zh" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge,chrome=1" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>bootstrap 4模态窗口增强插件 </title> <link rel= "stylesheet" href= "http://cdn.bootstrapmb.com/bootstrap/4.3.1/css/bootstrap.min.css" > <link href= "./src/css/simple-bs-dialog.css" rel= "stylesheet" type= "text/css" /> <script src= "http://cdn.bootstrapmb.com/jquery/jquery-3.3.1.min.js" ></script> <script src= "http://cdn.bootstrapmb.com/bootstrap/4.3.1/js/bootstrap.min.js" ></script> <script src= "https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js" ></script> <script src= "./src/js/simple-bs-dialog.js" ></script> <style> .login-dialog .modal-header { color: #0480be; } </style> </head> <body> <div class = "container" > <h2>Make use of Bootstrap's 4 Modal in a more friendly way.</h2> <hr /> <h3>Full example</h3> <p>Full functionality of SimpleBsDialog.</p> <div class = "source-code runnable" > <!-- SimpleBsDialog.show({ width: '900px' , autoWidth: false , height: '30%' , autoHeight: true , title: 'Simple Bootstrap 4 Dialog (v' + SimpleBsDialog.version + ')' , closable: true , spinner: true , spinnerIcon: '<span class="spinner-border text-primary" role="status"></span>' , closeByBackdrop: true , closeByKeyboard: true , html: '' , cssClass: 'login-dialog' , buttons: [{ id: 'btn-ok' , label: 'OK' , cssClass: 'btn-primary' , action: function(dialogRef) { console.log( 'OK button pressed!' ); dialogRef.close(); }, }, { id: 'btn-cancel' , label: 'Cancel' , cssClass: 'btn-warning' , action: function(dialogRef) { console.log( 'Cancel button pressed!' ); dialogRef.close(); }, }, ], onShow: function(dialogRef) { console.log( 'onShow' ); dialogRef.getButtons().prop( 'disabled' , true ); }, onShown: function(dialogRef) { console.log( 'onShown' ); setTimeout(function() { dialogRef. set ({ 'spinner' : false , }).getModalBody().html( 'Your content goes here...' ); dialogRef.getButtons().prop( 'disabled' , false ); }, 1000); }, onHide: function(dialogRef) { console.log( 'onHide' ); }, onHidden: function(dialogRef) { console.log( 'onHidden' ); }, }); --> </div> <!-- Available options --> <br /> <a name= "available-options" ></a> <h2>Available options</h2> <hr /> <p> Please note that all options described below are <strong>optional</strong>, but you will have a weird dialog if you don't even give it a title to display. <br /> Most options can be set via init options or property setters. </p> <table class = "table table-bordered" > <thead> <tr> <th> Option </th> <th> Type </th> <th> Default value </th> <th> Description </th> </tr> </thead> <tbody> <tr> <td>width</td> <td> String or Integer </td> <td> 500 </td> <td> The dialog's width in pixels or percent. Examples: <div class = "source-code runnable" > SimpleBsDialog.show({ width: '900px' , }); </div> <hr /> <div class = "source-code runnable" > SimpleBsDialog.show({ width: '70%' , }); </div> <hr /> <div class = "source-code runnable" > SimpleBsDialog.show({ width: '600' , }); </div> </td> </tr> <tr> <td>autoWidth</td> <td> Boolean </td> <td> false </td> <td> Enables resizing the dialog when the document is resized. It applies only when the 'width' value is set in percent. <div class = "source-code runnable" > SimpleBsDialog.show({ width: '80%' , autoWidth: true , }); </div> </td> </tr> <tr> <td>height</td> <td> String or Integer </td> <td> 280 </td> <td> The dialog's height in pixels or percent. Examples: <div class = "source-code runnable" > SimpleBsDialog.show({ height: '300px' , }); </div> <hr /> <div class = "source-code runnable" > SimpleBsDialog.show({ height: '60%' , }); </div> </td> </tr> <tr> <td>autoHeight</td> <td> Boolean </td> <td> false </td> <td> Enables resizing the dialog when the document is resized. It applies only when the 'height' value is set in percent. <div class = "source-code runnable" > SimpleBsDialog.show({ height: '60%' , autoHeight: true , }); </div> </td> </tr> <tr> <td>title</td> <td> String </td> <td></td> <td> The dialog's title. </td> </tr> <tr> <td>closable</td> <td> Boolean </td> <td> true </td> <td> Show|hide the close button at the top right corner. </td> </tr> <tr> <td>spinner</td> <td> Boolean </td> <td> false </td> <td> Show|hide the spinner icon. </td> </tr> <tr> <td>spinnerIcon</td> <td> String </td> <td> <div class = "source-code" ><span class = "spinner-border" role= "status" ></span></div> </td> <td> Set the spinner's icon. </td> </tr> <tr> <td>closeByBackdrop</td> <td> Boolean </td> <td> true </td> <td> When it's true , you can close it by clicking outside the dialog. </td> </tr> <tr> <td>closeByKeyboard</td> <td> Boolean </td> <td> true </td> <td> When it's true , you can close it by pressing the ESC key. </td> </tr> <tr> <td>html</td> <td> String </td> <td></td> <td> The dialog's content. </td> </tr> <tr> <td>cssClass</td> <td> String </td> <td></td> <td> Additional css classes that will be added to your dialog. </td> </tr> <tr> <td>buttons</td> <td> Array </td> <td></td> <td> Example: <div class = "source-code runnable" > SimpleBsDialog.show({ buttons: [{ id: 'btn-ok' , label: 'OK' , cssClass: 'btn-primary' , action: function(dialogRef) { dialogRef.close(); }, }, ], }); </div> <strong>id</strong>: optional, otherwise a random id will be generated.<br /> <strong>label</strong>: optional, the button's title.<br /> <strong>cssClass</strong>: optional, additional css class to be added to the button.<br /> <strong>action</strong>: optional, if provided, the callback will be invoked after the button is clicked, and the dialog instance will be passed to the callback function.<br /> </td> </tr> <tr> <td>onShow</td> <td> function </td> <td></td> <td> If provided, it will be invoked when the dialog is popping up. </td> </tr> <tr> <td>onShown</td> <td> function </td> <td></td> <td> If provided, it will be invoked when the dialog is popped up. </td> </tr> <tr> <td>onHide</td> <td> function </td> <td></td> <td> If provided, it will be invoked when the dialog is popping down. </td> </tr> <tr> <td>onHidden</td> <td> function </td> <td></td> <td> If provided, it will be invoked when the dialog is popped down. </td> </tr> </tbody> </table> <!-- Available methods --> <br /> <a name= "available-methods" ></a> <h2>Available methods</h2> <hr /> <table class = "table table-bordered" > <thead> <tr> <th> Method </th> <th> Description </th> </tr> </thead> <tbody> <tr> <td>open()</td> <td>Opens the dialog. Usage: dialogInstance.open().</td> </tr> <tr> <td>close()</td> <td>Closes the dialog. Usage: dialogInstance.close().</td> </tr> <tr> <td> get (option)</td> <td> Getter for options. You can get the following options:<br /> <strong>width, autoWidth, height, autoHeight, title, closable, spinner, spinnerIcon</strong> </td> </tr> <tr> <td> set (option, value)</td> <td> Setter for a given option. You can set the following options:<br /> <strong>width, autoWidth, height, autoHeight, title, closable, spinner, spinnerIcon</strong> </td> </tr> <tr> <td> set (options)</td> <td> Setter for many options. You can set the following options:<br /> <strong>width, autoWidth, height, autoHeight, title, closable, spinner, spinnerIcon</strong> </td> </tr> <tr> <td>getModalBody()</td> <td>Returns the raw modal body.</td> </tr> <tr> <td>getButton(id)</td> <td>Returns a button by id as a jQuery object .</td> </tr> <tr> <td>getButtons()</td> <td>Returns all available buttons as jQuery objects.</td> </tr> <tr> <td>SimpleBsDialog.show(options)</td> <td>Creates a new dialog with options, opens it and returns the dialog object .</td> </tr> <tr> <td>SimpleBsDialog.version</td> <td>Returns the current SimpleBsDialog's version.</td> </tr> </tbody> </table> <hr /> </div> <script type= "text/javascript" > $(function () { $( '.source-code' ).each(function (index) { var $section = $( this ); var code = $( this ).html().replace( '<!--' , '' ).replace( '-->' , '' ); // Code preview var $codePreview = $( '<pre class="prettyprint lang-javascript"></pre>' ); $codePreview.text(code); $section.html($codePreview); // Run code if ($section.hasClass( 'runnable' )) { var $button = $( '<button class="btn btn-primary">Run the code</button>' ); $button. on ( 'click' , { code: code }, function ( event ) { eval( event .data.code); }); $button.insertAfter($section); $( '<div class="clearfix" style="margin-bottom: 10px;"></div>' ).insertAfter($button); } }); }); </script> </body> </html> |
simple-bs-dialog.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .simple-bs-dialog .modal-dialog, .simple-bs-dialog .modal-content { min-height: 200px; } .simple-bs-dialog .modal-body { min-height: 80px; } .simple-bs-dialog .modal-spinner { background: white; z-index: 1; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } |
simple-bs-dialog.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | (function(w, $) { 'use strict' ; // register the main object w.SimpleBsDialog = function(options) { this .options = $.extend({}, { id: newGuid(), width: 500, autoWidth: false , height: 280, autoHeight: false , title: '' , closable: true , spinner: false , spinnerIcon: '<span class="spinner-border" role="status"></span>' , closeByBackdrop: true , closeByKeyboard: true , html: '' , cssClass: '' , buttons: [], onShow: function(dialogRef){}, onShown: function(dialogRef){}, onHide: function(dialogRef){}, onHidden: function(dialogRef){}, }, options); }; // private methods function newGuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' .replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = (c == 'x' ? r : r & 0x03 | 0x08); return v.toString(16); }); } function setWidth(e, init) { var width = 0; // parse width if (e.data.options.width.toString().indexOf( 'px' ) >= 0) { width = parseInt(e.data.options.width.toString().substring(0, e.data.options.width.toString().indexOf( 'px' ))); } else if (e.data.options.width.toString().indexOf( '%' ) >= 0) { width = parseInt((parseInt(e.data.options.width.toString().substring(0, e.data.options.width.toString().indexOf( '%' ))) / 100) * $(window).outerWidth( true ), 10); } else { width = parseInt(e.data.options.width); } // set left $( '#' + e.data.options.id).find( '.modal-dialog' ).css({ 'margin-left' : parseInt(($(window).outerWidth( true ) - width) / 2, 10) + 'px' }); // set width if (init === true || (e.data.options.autoWidth === true && e.data.options.width.toString().indexOf( '%' ) >= 0)) { $( '#' + e.data.options.id).find( '.modal-content' ).css({ 'width' : width + 'px' }).find( '.modal-body' ).css({ 'min-width' : (width - 120) + 'px' }); } } function setHeight(e, init) { var height = 0; // parse height if (e.data.options.height.toString().indexOf( 'px' ) >= 0) { height = parseInt(e.data.options.height.toString().substring(0, e.data.options.height.toString().indexOf( 'px' ))); } else if (e.data.options.height.toString().indexOf( '%' ) >= 0) { height = parseInt((parseInt(e.data.options.height.toString().substring(0, e.data.options.height.toString().indexOf( '%' ))) / 100) * ($(window).outerHeight( true ) - parseInt($( '#' + e.data.options.id).find( '.modal-dialog' ).css( 'margin-top' ), 10)), 10); } else { height = parseInt(e.data.options.height); } // set height if (init === true || (e.data.options.autoHeight === true && e.data.options.height.toString().indexOf( '%' ) >= 0)) { $( '#' + e.data.options.id).find( '.modal-content' ).css({ 'min-height' : height + 'px' }).find( '.modal-body' ).css({ 'min-height' : (height - 120) + 'px' }); } } function setSize(e) { setWidth(e, false ); setHeight(e, false ); } function validOptions() { return [ 'width' , 'autoWidth' , 'height' , 'autoHeight' , 'title' , 'closable' , 'spinner' , 'spinnerIcon' , ]; } function parseCssClass(cssClass) { var cssClasses = cssClass.split( ' ' ); if (cssClasses.length == 0) { return '' ; } return ' ' + cssClasses. join ( ' ' ); } // public methods SimpleBsDialog.prototype.open = function() { // store this var dialog = this ; // bootstrap dialog $( 'body' ).append( '<div class="simple-bs-dialog modal fade' + parseCssClass(dialog.options.cssClass) + '" id="' + dialog.options.id + '" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">' + dialog.options.title + '</h5><button type="button" class="close' + (dialog.options.closable ? '' : ' d-none' ) + '" data-dismiss="modal">×</button></div><div class="modal-html"><div class="modal-body">' + dialog.options.html + '</div><div class="modal-spinner' + (dialog.options.spinner ? '' : ' d-none' ) + '">' + dialog.options.spinnerIcon + '</div></div>' + (dialog.options.buttons.length > 0 ? '<div class="modal-footer"></div>' : '' ) + '</div></div></div>' ); // add buttons $.each(dialog.options.buttons, function(index, options) { var opts = $.extend({}, { id: newGuid(), label: '' , cssClass: '' , action: function(dialogRef){}, }, options); var button = $( '<button type="button" class="btn btn-default' + (opts.cssClass.length > 0 ? ' ' : '' ) + opts.cssClass + '" id="' + opts.id + '">' + opts.label + '</button>' ); button. on ( 'click' , function(e) { opts.action(dialog); }); $( '#' + dialog.options.id).find( '.modal-footer' ).append(button); }); // destroy dom element $( '#' + dialog.options.id). on ( 'show.bs.modal' , function() { // initial width setWidth({ data: dialog, }, true ); // initial height setHeight({ data: dialog, }, true ); // set dialog's auto size $(window). on ( 'resize' , dialog, setSize); // onShow event dialog.options.onShow(dialog); }). on ( 'shown.bs.modal' , function() { // onShown event dialog.options.onShown(dialog); }). on ( 'hide.bs.modal' , function(e) { // onHide event dialog.options.onHide(dialog); }). on ( 'hidden.bs.modal' , function(e) { // cancel dialog's auto size $(window).off( 'resize' , setSize); // remove dialog's div $( '#' + dialog.options.id).remove(); // onHidden event dialog.options.onHidden(dialog); }).modal({ 'backdrop' : dialog.options.closeByBackdrop, 'keyboard' : dialog.options.closeByKeyboard, }); // return main object return this ; } SimpleBsDialog.prototype.close = function() { $( '#' + this .options.id).modal( 'hide' ); // return main object return this ; } SimpleBsDialog.prototype. get = function(option) { if (validOptions().indexOf(option) > -1) { return this .options[option]; } } SimpleBsDialog.prototype. set = function(options, value) { if (!$.isPlainObject(options) && validOptions().indexOf(options) > -1) { var option = options; options = {}; options[option] = value; } if ($.isPlainObject(options)) { // store this var dialog = this ; $.each(options, function(option, value) { switch (option) { case 'width' : { // new width dialog.options.width = value; // init new size setWidth({ data: dialog, }, true ); break ; } case 'autoWidth' : { // new autoWidth dialog.options.autoWidth = value; // init new size setWidth({ data: dialog, }, true ); break ; } case 'height' : { // new height dialog.options.height = value; // init new size setHeight({ data: dialog, }, true ); break ; } case 'autoHeight' : { // new autoHeight dialog.options.autoHeight = value; // init new size setHeight({ data: dialog, }, true ); break ; } case 'title' : { // update dialog's title $( '#' + dialog.options.id).find( '.modal-title' ).html(dialog.options.title = value); break ; } case 'closable' : { // update dialog's closable if (dialog.options.closable = value) { $( '#' + dialog.options.id).find( '.modal-header button.close' ).removeClass( 'd-none' ); } else { $( '#' + dialog.options.id).find( '.modal-header button.close' ).addClass( 'd-none' ); } break ; } case 'spinner' : { // update dialog's spinner if (dialog.options.spinner = value) { $( '#' + dialog.options.id).find( '.modal-spinner' ).removeClass( 'd-none' ); } else { $( '#' + dialog.options.id).find( '.modal-spinner' ).addClass( 'd-none' ); } break ; } case 'spinnerIcon' : { // update dialog's spinnerIcon $( '#' + dialog.options.id).find( '.modal-spinner' ).html(dialog.options.spinnerIcon = value); break ; } } }); } // return main object return this ; } SimpleBsDialog.prototype.getModalBody = function() { // get modal body return $( '#' + this .options.id).find( '.modal-body' ); } SimpleBsDialog.prototype.getButton = function(id) { // get button's object return $( '#' + this .options.id).find( '.modal-footer button#' + id); } SimpleBsDialog.prototype.getButtons = function() { // get all the buttons' object return $( '#' + this .options.id).find( '.modal-footer button' ); } // for lazy people SimpleBsDialog.show = function(options) { return ( new SimpleBsDialog(options)).open(); } // current version SimpleBsDialog.version = '1.0.1' ; }(window, jQuery)); |
参考:http://www.bootstrapmb.com/item/6561/preview
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 欧阳的2024年终总结,迷茫,重生与失业
· Bolt.new 30秒做了一个网站,还能自动部署,难道要吊打 Cursor?
· 史上最全的Cursor IDE教程
· 关于产品设计的思考
· 我的编程之路 2024
2019-06-20 CSS .css边框属性(border)
2019-06-20 教你两招用纯CSS写Tab切换
2019-06-20 css等比例分割父级容器(完美三等分)