In the last jQuery Mobile post, we talked all about Pages with jQuery Mobile. I want to follow that up with a post about dialogs and popups because they have a lot more similarities with jQuery Mobile pages than you would think.
Dialogs
Dialogs are a great way to present a modal window to users that requires their interaction. These are often best used when the user has an action to perform (e.g. confirming an action before moving forward).
Using jQuery Mobile, there are two different ways to create a dialog. For the most part, you can think of dialogs in a very similar manner to that of a page using jQuery Mobile. The first way we will look at creating a page to be used as a dialog, is strikingly similar to a typical page.
1 2 3 4 5 6 7 8 9 10 11 | |
The snippet above will look very similar to a page using jQuery Mobile. The difference here is the value of data-role. Rather than page, we set a value of dialog and then provide a corresponding id attribute to link to.
This creates a standalone dialog page that is only usable as a dialog. The benefit to creating a standalone dialog page is that any links to the dialog do not require a data-rel attribute which we’ll discuss next.
Opening a dialog from a link
While sometimes dialog-only type pages are okay, you are not required to create dialogs this way. Any standard jQuery Mobile page can be opened as a dialog by simply adding a data-rel="dialog" attribute to a link.
1
| |
When the above anchor linked is tapped or clicked, jQuery Mobile will turn the data-role="page" element into data-role="dialog" and will display the linked page styled as a dialog.
Below is a CodePen example showing the different ways that a jQuery Mobile dialog can be opened using nothing but HTML5 data-attributes.
If the embedded pen below doesn’t function correctly, check it out on CodePen.
Check out this Pen!
Opening a dialog programmatically
You aren’t required to use data attributes to create dialogs using jQuery Mobile. The dialog widget (as well as every jQuery Mobile widget) has a programmatic way to enable the functionality as well using the $.mobile.changePage method.
1 2 3 4 5 | |
Transitions
By default, jQuery Mobile dialogs are shown with a pop transition. However, just like any jQuery Mobile page, you can alter the transition used by adding a data-transition attribute to your link.
1
| |
You can use any transition property including none for opening a jQuery Mobile dialog widget. Just keep in mind that as with standard page transitions, if 3D transforms are not supported a fallback to fade will be used.
Dialogs and State
It’s important to point out that dialogs are not included by jQuery Mobile in the browser history. For example:
- A user starts out on
page1.html - They perform an action that presents them a dialog
- In the dialog they perform an action that takes them to
page2.html - They press the back button from
page2.htmland return topage1.html, not the dialog.
Nothing like the API docs
For complete information on the methods, events and options available with jQuery Mobile’s dialog widget check out the dialog API.
Popups
Popups are very similar to dialogs but there are a lot more use cases bit more with popups. One thing that is very important to remember is that the data-role="popup" element must be within the data-role="content" section.
Here’s a CodePen examples showing some real world use cases for jQuery Mobile’s popup widget. These examples are out of the box with no custom JavaScript additions, simple HTML5 data-attribute configurations.
If the embedded pen below doesn’t function correctly, check it out on CodePen.
Check out this Pen!
Positioning a popup
You can control how and where the popup is positioned when it is opened. By default, popups will show directly over the clicked element. Quite like a simple tooltip.
However, if you prefer to position your popup differently you can do so with a simple data-position-to attribute on the link opening the popup. If you plan on dynamically updating the content of a single tooltip container, this is actually quite nice. You can use a single container and display it as a tooltip or even a lightbox based off of the link opening it.
1 2 3 4 5 6 7 8 | |
Using a popup as a lightbox
jQuery Mobile’s CSS framework includes rules that make using the popup widget as a lightbox dead simple. Because of this, images that are immediate children of the data-role="popup" container will scale to fit the screen. The one issue you may run into is that this doesn’t always adjust the height to screen height across browsers. That can be taken care of with a small bit of JavaScript to set the max-height of the image.
You may want to also include an overlay with your lightbox, like the example use case above. Once again, jQuery Mobile makes this quite simple to do.
1 2 3 | |
Creating popups programmatically
By default, any page that contains a div with a data-role="popup" attribute will be automatically initialized. Just like other jQuery Mobile widgets though, the popup widget gives you a way to programmatically create it as well and it follows standard syntax and API calls you are already used to with jQuery.
1
| |
Opening and closing popups programmatically
All jQuery Mobile widgets are based off of jQuery UI’s Widget Factory pattern. This creates a lot of commonality in how new widgets are developed, making it easier for developers to use widgets and create custom widgets of their own.
jQuery UI’s Widget Factory gives you the ability to call any public methods of your widget by simply calling the widget function and passing an argument with the method name to call.
1
| |
For the popup widget in jQuery Mobile, this makes opening and closing popups programmatically very simple.
1 2 3 4 5 | |
Transitions
The popup widget does not use a transition by default, although you can use any of the native transitions. The nature of the general use case for the popup calls for the information to be displayed as quickly as possible, driving the default data-transition value of none.
1
| |
For performance reasons, the jQuery Mobile team recommends using simpler animations such as pop, fade or none for the popup widget. In my testing, fade tends to be smooth and quick while still looking nice.
Making a popup modal
As of jQuery Mobile 1.3.0, popups now have a data-dismissible attribute that you can add to the data-role="popup" container. When this value is set to false, the popup will not close when you tap outside of it making it “modal” like a dialog widget.
Popups and State
Just like dialogs, popups are not included by jQuery Mobile in the browser history. If popup content links a user to a new page and they press the back button, they will go back to the original page without the popup open.
Nothing like the API docs
For complete information on the methods, events and options available with jQuery Mobile’s popup widget, check out the popup API
What’s next?
Next up in the jQuery Mobile series, we’ll dive into jQuery Mobile listviews and talk about displaying data and using them for navigation.
