- Overview
- Documents
jBox is a powerful and flexible jQuery plugin, taking care of all your modal windows, tooltips, notices and more.
Source: stephanwagner.me
1. INCLUDE CSS AND JS FILES
<script src="//code.jquery.com/jquery-2.1.0.min.js"></script> <script src="jBox/Source/jBox.min.js"></script> <link href="jBox/Source/jBox.css" rel="stylesheet">
2. HTML AND JAVASCRIPT
Tooltips
Html
<span class="tooltip" title="My first tooltip">Hover me!</span> <span class="tooltip" title="My second tooltip">Hover me!</span>
Javascript
$('.tooltip').jBox('Tooltip');
Modal windows
Html
<div id="myModal">Click me to open a modal window!</div>
Javascript
new jBox('Modal', { width: 300, height: 200, attach: $('#myModal'), title: 'My Modal Window', content: '<i>Hello there!</i>' });
Notices
Html
<div id="myNotice">Click me to show a notice!</div>
Javascript
$('#myNotice').click(function() { new jBox('Notice', { position: { y: 65, x: 15 }, content: 'Hurray! A notice!' }); });
4. OPTIONS
You can set the options for your jBoxes right away or as a second argument when you are using a type:
var options = { title: 'My title', content: 'My content' }; new jBox(options); new jBox('Tooltip', options);
There are following types: Tooltip, Mouse, Modal, Notice.
ID |
||
id | string | Choose a unique id, otherwise jBox will set one for you (jBox1, jBox2, ...) |
Dimensions |
||
width | auto, integer | Width of container |
height | auto, integer | Height of container |
Attaching jBox |
||
attach | jQuery selector | Sets the open and close functions to elements |
trigger |
click, mouseenter |
Defines with which event the jBox opens or closes when interacting with the attached element |
preventDefault | boolean | Prevents the default event when clicking on the attached element, e.g. don't follow the href of a link |
Content |
||
title | string | Adds a title to your jBox |
content |
string, jQuery selector |
Sets the content of your jBox. You can use jQuery selectors to append elements (set CSS style display to none so the elements won't show up on your page) |
getTitle | string | The attribute from which jBox should get its title |
getContent | string | The attribute from which jBox should get its content |
ajax | string | An URL to get content from an AJAX call, the response will get set as the new content |
data | string | AJAX data to send with your AJAX call, e.g. id=1982 |
reload | boolean | Reload the content from AJAX every time jBox opens |
Position |
||
target | jQuery selector | The target element where jBox will be positioned in or at. If no target element is provided, jBox will use the attached element as target |
position | object | Set an object with the horizontal position x and the vertical position y, e.g. {x: 'right', y: 'center'}. You can also set numbers for an absolute position |
outside | x, y, xy | Moves your jBox outside of the target element |
offset | integer, object | Offset to final position. You can set different values for x andy with an object, e.g. {x: 15, y: -10} |
attributes | object | Choose what CSS attributes should be used, e.g.{x: 'right', y: 'bottom'}. Note that right andbottom can only be used when your position values are integer, e.g. {x: 300, y: 20} |
adjustPosition |
flip, move, boolean |
Adjusts the position when there is not enough space to show the whole jBox. The value flip positions the jBox on the opposite outside position, the value move works only with a pointer. Set to true to use both |
adjustTracker |
scroll, resize, boolean |
By default jBox adjusts its position when it opens, to also adjust while scrolling or when you resize the browser, usescroll, resize or true for both events. This option affects performance, use with care |
adjustDistance | integer, object | Distance to the window edge when adjusting should start. Use an object to set different values, e.g. {top: 50,right: 20, bottom: 5, left: 20} |
fixed | boolean | Sets a fixed position rather than an absolute |
reposition | boolean | Recalculates the position when the browser size changes |
Pointer |
||
pointer |
boolean, left, right, top, bottom, center |
Your pointer will always point towards the target element, so the option outside needs to be x or y. By default the pointer is centered, set a position to move it to any side. You can also add an offset, e.g. left:30, center:-20 |
Animations |
||
fade | integer | Fade duration in ms, set 0 or false to disable |
animation |
object, zoomIn, zoomOut, pulse, move, slide, flip, tada |
Animation when jBox opens or closes. To use different animations for opening and closing, use an object: {open: 'tada', close: 'flip'}. You can also set the direction of the move and slideanimations: {open: 'move:right',close: 'slide:top'}. You'll need a modern browser for animations to work, or IE from version 10 |
Appearance |
||
theme | string | Default themes that come with jBox are ModalBorder,NoticeBorder, TooltipBorder, TooltipDark. Remember to load the CSS file of your theme |
addClass | string | Adds classes to the wrapper |
overlay | boolean | Adds an overlay when jBox opens |
zIndex | integer | Use a high zIndex. Your overlay will have the lowest zIndex of all your jBoxes minus one |
Delays |
||
delayOpen | integer | Delay opening in ms. Note that the delay will be ignored if your jBox didn't finish closing |
delayClose | integer | Delay closing in ms. Note that there is always a closing delay of at least 10ms to ensure jBox won't be closed when opening right away |
Closing jBox |
||
closeOnEsc | boolean | Close jBox when pressing the [ESC] key |
closeOnClick |
boolean, body, box |
Close jBox with a mouseclick: true closes when you click anywhere, box when clicking on the jBox itself and bodywhen you click anywhere but the jBox |
closeOnMouseleave | boolean | Close jBox when the mouse leaves the jBox element |
closeButton |
boolean title, box |
Adds a close button to your jBox. The value true will add the close button to the title if there is one, otherwise to the jBox element |
Other options |
||
constructOnInit | boolean | Construct jBox when it's being initialized or when it opens for the first time |
blockScroll | boolean | Blocks scrolling when jBox is open |
appendTo | jQuery selector | Provide an element if you want the jBox to be positioned inside a specific element. This option is only useful for fixed positions or when the position values are numbers |
draggable |
boolean, title, jQuery selector |
Makes your jBox draggable. Use title or provide any child element of jBox to use as the handle |
Events |
||
onInit | function |
Triggered when jBox is initialized. Note that you can use this in the event functions, it refers to your jBox object, e.g. onInit: function() {this.open(); } |
onCreated | function | Triggered when jBox is created and is availible in DOM |
onOpen | function | Triggered when jBox is opening |
onClose | function | Triggered when jBox is closing |
onAjax | function | Triggered when the AJAX call starts |
onAjaxComplete | function | Triggered when the AJAX call is completed |
Options only for notices |
||
autoClose | integer, boolean | Time in milliseconds when the notice should close automatically, set to 0 or false to disable |
color |
black, red, green, blue, yellow |
Makes your notices colorful |
stack | boolean | Wheather to stack or only show single notices |
audio | string | The url to an audio file to play when the notice opens, e.g./audio/filename. jBox will look for .mp3 and .ogg files |
volume | integer | Use a value between 0 and 100 to set the audio volume |
5. METHODS
The best way to use methods on your jBoxes is to save your jBox in a variable:
var myModal = new jBox('Modal'); myModal.setTitle('My Title'); myModal.setContent('My Content'); myModal.open();
All methods return the jBox instance so you can chain them:
var myModal = new jBox('Modal').setTitle('My Title').setContent('My Content').open();
Open and close |
|
.open(options) |
Opens the jBox. You can set a new target with the option target, e.g. {target:$('#newTarget')}. If your jBox has an opening delay, you can force it to open immediately with the option ignoreDelay, e.g. {ignoreDelay: true} |
.close(options) |
Closes the jBox. If your jBox has a closing delay, you can force it to close immediately with the option ignoreDelay, e.g. {ignoreDelay: true} |
.toggle(options) | Calls the method open when jBox is closed and close when it is open |
Attaching jBox |
|
.attach(elements) |
Sets the open and close functions to elements. Providing the elements is optional. If you don't tell the method which elements to use, it will use the elements defined in the options. This method should be called when elements, which should open or close a jBox, are being created in runtime |
.detach(elements) | Removes the open and close function from elements |
Content |
|
.setTitle(title) | Sets the title of your jBox. If there is no title yet, it will be created |
.setContent(content) |
Sets the content of your jBox. You can use jQuery selectors to append elements (set CSS style display to none so the elements won't show up on your page) |
.ajax(options) |
Loads content from an AJAX call. You can set the options url and data, e.g. {url: '/example.php',data: 'id=1982'} |
Position |
|
.position(options) |
Recalculates your jBoxes position. You can set a new target with the option target, e.g. {target:$('#newTarget')} |
Disable and enable |
|
.disable() | Disables your jBox, you won't be able to open or close it until enabled |
.enable() | Enables your jBox, so you can close and open it again |
Destroy jBox |
|
.destroy() | Destroys your jBox and removes it from DOM |