- Overview
- Documents
plainModal is a simple jQuery Plugin for customizable modal windows. plainModal has basic functions only, and it does nothing for styles. It has no image files and no CSS files. Just one small file (2KB minified)
Many great plugins already exist.
- The gorgeous plugins which has many functions and rich styles.
- The simple plugins which has small functions and customizable styles.
The many web sites use same functions almost. And these have different styles.
plainModal has basic functions for showing modal windows. That's all. The styling it is your job. You can free style it to perfect match for your web site.
plainModal do:
- Showing specified element as modal window, and hiding it.
- Covering a page with a overlay.
- Avoiding focusing outside elements of the modal window. (by pressing Tab key)
- Avoiding scrolling a page window.
- Hiding the modal window when Escape key is pressed.
Source: anseki.github.io
1. INCLUDE JS FILES
<script src="jquery-1.11.0.min.js"></script> <script src="jquery.plainmodal.min.js"></script>
2. HTML
<div id="sample-style"> <div class="plainmodal-close"></div> <p class="sample-head">js-tutorial.com</p> <p><img src="images/GitHub-Mark-120px-plus.png" width="120" height="120" alt=""></p> </div> <div class="sample-button" id="sample-button1">Open Sample</div>
3. JAVASCRIPT
$('#sample-button1').click(function() { $('#sample-style').plainModal('open'); });
4. OPTIONS
A options Object can be specified to Open method or Initialize method. This Object can have following properties.
Type: Object or Function
Default: Calculated center position
A Object that has left and top, relative to the view area.
$('#modal').plainModal({offset: {left: 100, top: 50}});
Or, a Function that returns above Object. This Function is called by Open method, not Initialize method. Therefore position be able to change according to the situation.
var button = $('#open-button').click(function() { modal.plainModal('open'); }), modal = $('#modal').plainModal({ offset: function() { // Fit the position to a button. var btnOffset = button.offset(), win = $(window); return { left: btnOffset.left - win.scrollLeft() + parseInt(this.css('borderLeftWidth'), 10), top: btnOffset.top - win.scrollTop() + parseInt(this.css('borderTopWidth'), 10) }; } });
Type: Object
Default: {color: '#888', opacity: 0.6, zIndex: 9000}
A Object that can have color (fill-color), opacity and zIndex CSS properties of overlay.
$('#modal').plainModal({overlay: {color: '#fff', opacity: 0.5}});
Type: String
Default: 'plainmodal-close'
If the element that has this class name is found, the Close method is attached to click event of it.
<div> <p>Lorem ipsum dolor ...</p> <div class="plainmodal-close">Close</div> </div>
Type: Number
Default: 200
A number determining how long (milliseconds) the effect animation for showing and hiding the modal window will run.
Type: Object
Default: {open: jQuery.fn.fadeIn, close: jQuery.fn.fadeOut}
A Object that can have open and close Functions for showing and hiding the modal window.
These Functions are called with duration Number (see above) and complete Function.
It's same to standard effect methods of jQuery (slideDown(), slideUp(), animate(), etc.). Therefore, those methods can be specified.
$('#modal').plainModal({effect: {open: $.fn.slideDown, close: $.fn.slideUp}});
Custom animation:
$('#modal').plainModal({ offset: {left: 300, top: 100}, duration: 500, effect: { open: function(duration, complete) { this.css({ display: 'block', marginTop: -100 - this.outerHeight() }) .animate({marginTop: 0}, duration, complete); }, close: function(duration, complete) { this.animate({ marginTop: -100 - this.outerHeight() }, duration, function() { $(this).css({display: 'none'}); complete(); }); } } });
These Functions can ignore duration, but it must call complete, when the effect was finished.
$('#modal').plainModal({ effect: { open: function(duration, complete) { var that = this.css({ display: 'block', color: '#fff', backgroundColor: '#f1e470' }); setTimeout(function() { that.css({color: '', backgroundColor: ''}); complete(); }, 500); }, close: function(duration, complete) { var that = this.css({ color: '#fff', backgroundColor: '#f1e470' }); setTimeout(function() { that.css({display: 'none'}); complete(); }, 500); } } });
Type: Number
Default: options.overlay.zIndex + 1
A z-index CSS property of the modal window. This number have to be bigger than options.overlay.zIndex.
Type: Function
Default: undefined
A plainmodalopen event handler.
$('#modal').plainModal({open: function(event) { console.log(event); } });
Type: Function
Default: undefined
A plainmodalclose event handler.
$('#modal').plainModal({close: function(event) { console.log(event); } });
5. METHODS
element.plainModal('open'[, options])
Show specified element as modal window.
If options is specified, element is initialized with specified options before it is shown. If element is not initialized yet, element is initialized even if options is not specified.
A element can be initialized by new options any number of times.
element.plainModal('close')
Hide the modal window.
element.plainModal([options])
Initialize specified element as modal window.
The Open method can initialize too. This is used to initialize without showing the modal window at voluntary time.
You can specify options to every Open method. But, if options of a element isn't changed, re-initializing it isn't needed. Then, you specify options to only first Open method, or use this method for initializing it only once.
If you don't customize Options (using default all), this method isn't needed because options isn't specified to Open method, and element is initialized at only first time.
In this code, unneeded initializing is done again, again, and again.
$('#open-button').click(function() { // Same initializing per every showing $('#modal').plainModal('open', {duration: 500}); });
In this code, initializing is done at once.
// Initialize without showing var modal = $('#modal').plainModal({duration: 500}); $('#open-button').click(function() { // Show without initializing modal.plainModal('open'); });
In this code, initializing is done at once.
$('#open-button').click(function() { // Initializing is done at only first time modal.plainModal('open'); });
6. EVENTS
plainmodalopen
Triggered when the modal window is opened. (after the effect.open took duration to complete.)
An event handler can be attached when initializing too via options.open.
$('#modal').on('plainmodalopen', function(event) { $('textarea', event.target).addClass('highlight'); });
Triggered when the modal window is closeed. (after the effect.close took duration to complete.)
An event handler can be attached when initializing too via options.close.
$('#modal').on('plainmodalclose', function(event) { $('#screen').show(); });