Popup Widgetversion added: 1.2
Description: Opens content in a Popup.
To create a popup, add the data-role="popup"
attribute to a div with the popup contents. Then create a link with the href
set to the id
of the popup div, and add the attribute data-rel="popup"
to tell the framework to open the popup when the link is tapped. This is a similar markup pattern to the dialog widget. A popup div has to be nested inside the same page as the link.
1
2
3
4
5
|
|
This will result in the following popup:
The popup consists of two elements: the screen, which is a transparent or translucent element that covers the entire document, and the container, which is the popup itself. If your original element had an id
attribute, the screen and the container will each receive an id
attribute based on it. The screen's id
will be suffixed with "-screen", and the container's id
will be suffixed with "-popup" (in the above example, id="popupBasic-screen"
and id="popupBasic-popup"
, respectively).
The framework adds a small amount of margin to text elements, but it's really just a container with rounded corners and a shadow which serves as a blank slate for your designs (even these features can be disabled via options).
This simple styling makes it easy to add in widgets to create a variety of different interactions. Here are a few real-world examples that combine the various settings and styles you can achieve out of the box with popups:
Scaling images: Lightbox examples
The framework CSS contains rules that make images that are immediate children of the popup scale to fit the screen. Because of the absolute positioning of the popup container and screen, the height is not adjusted to screen height on all browsers. You can prevent vertical scrolling with a simple script that sets the max-height
of the image.
In the two examples below the divs with data-role="popup"
have a class of photopopup
.
The handler is bound to the popupbeforeposition
event, which ensures the image is not only scaled before it is shown but also when the window is resized (e.g. orientation change). In this code example the height is reduced by 60 to take a top and bottom tolerance of 30 pixels into account.
1
2
3
4
5
6
7
8
|
|
Working with iframes in popups
You may need to embed an iframe into a popup to use a 3rd party widget. Here, we'll walk through a few real-world examples of working with iframes: videos and maps.
Video example
Here is an example of a 3rd party video player embedded in a popup:
The markup is an iframe inside a popup container. The popup will have a 15 pixels padding because of class ui-content
and a one pixel border because the framework will add class ui-body-d
to the popup.
1
2
3
4
5
|
|
When using an iframe inside a popup it is important to initially set the width and height attributes to 0. This prevents the page to breaking on platforms like Android 2.3. Note that you have to set the attributes, because setting the width and height with CSS is not sufficient. You can leave the actual width and height in the markup for browsers that have JavaScript disabled and use attr()
to set the zero values upon the pageinit
event.
Next, bind to the popupbeforeposition
event to set the desired size of the iframe when the popup is shown or when the window is resized (e.g. orientation change). For the iframe examples on this page a custom function scale()
is used to scale down the iframe to fit smaller screens. Expand the section below to view the code of this function.
scale()
The window width and height are decreased by 30 to take the tolerance of 15 pixels at each side into account.
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
|
|
Note: This function is not part of the framework. Copy the code into your script to use it.
When the popup is closed the width and height should be set back to 0. You can do this by binding to the popupafterclose
event.
This is the complete script and the link to open the video popup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
|
Note that the video will still be playing in the iframe when the popup is closed. If available you can use the 3rd party API to stop the video on the popupafterclose
event. Another way is to create the iframe when the popup is opened and destroy it when the popup is closed, but this would drop support for browsers that have JavaScript disabled.
Map example
In the second example, an iframe is used to display Google Maps API. Using an iframe prevents issues with the controls of the map.
This is the markup of the popup including a right close button:
1
2
3
4
5
6
7
|
|
Expand the section below to view the source of the iframe.
map.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
|
|
Setting the size of the iframe is done exactly the same as for the video example, with one exception. You should also set the width and height of the div that contains the map to prevent the page to break on platforms like Android 2.3. In this example the ID of this div is #map_canvas
.
This is the complete script and the link to open the map popup:
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
|
|
Overlay panels
Taking customization further, here is an example of a popup that has been customized to look like a vertical panel with three mini buttons:
Here is the HTML markup for the button and panel:
1
2
3
4
5
6
7
8
9
|
|
To style the panel, and attach it to the right edge, the following CSS is used. Note that #popupPanel-popup
is the ID of the container div generated by the framework.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
|
Because the popup container is positioned absolute, you can't make the panel full height with height:100%;
. This small script sets the height of the popup to the actual screen height.
1
2
3
4
5
6
7
|
|
Calling the popup plugin
This plugin will autoinitialize on any page that contains a div with the attribute data-role="popup"
. However, if needed you can directly call the popup
plugin on any selector, just like any jQuery plugin and programmatically work with the popup options, methods, and events API:
1
|
|
Opening popups
Using the markup-based configuration, when a link with the data-rel="popup"
is tapped, the corresponding popup container with the id referenced in the href
of the link will be shown. To open a popup programmatically, call popup with the open
method on the popup container:
1
|
|
Closing popups
By default popups can be closed either by clicking outside the popup widget or by pressing the Esc
key. To prevent this, the data-dismissible="false"
attribute can be added to the popup. Popups can also be closed via the close
method:
1
|
|
To add an explicit close button to a popup, add a link with the role of button into the popup container with a data-rel="back"
attribute which will close the popup when tapped. We have created helper classes to position buttons in the upper left (ui-btn-left
) or upper right (ui-btn-right
) of the popup but you may need to tweak these or add custom positioning styles depending on your design. We recommend adding standard content padding to the popup to make room for the buttons (see next section).
1
2
3
4
|
|
Adding padding
For popups with formatted text, padding is needed. We recommend adding the ui-content
class to the popup container which adds the standard 15px of padding, just like the page content container. Write your own styles to create a more customized design if needed.
1
2
3
4
5
|
|
This will result in the following popup with content padding:
When padding is added, we apply a few style rules to negate the top margin for the first heading or paragraph in the popup and do the same for the last element's bottom margin. This keep the popups from having too much vertical space when the content padding and element margins combine.
Positioning options
By default, popups open centered vertically and horizontally over the thing you clicked (the origin) which is good for popups used as tooltips or menus. The framework also applies some basic collision detection rules to ensure that the popup will appear on-screen so the ultimate position may not always be centered over the origin.
For situations like a dialog or lightbox where the popup should appear centered within the window instead of over the origin, add the data-position-to
attribute to the link and specify a value of window
.
It's also possible to specify any valid selector as the value of position-to
in addition to origin
and window
. For example, if you add data-position-to="#myElement"
the popup will be positioned over the element with the id myElement
.
A few examples:
The popup's placement constraints, which may cause the popup not to appear centered as desired, are as follow:
- The width of the popup will be limited using CSS
max-width
to the width of the window minus a tolerance of 15px on either side. - A tolerance from the edges of the window (15px from each of the sides and 30px from the top and the bottom) will be observed when the popup fits inside the window. Tall popups are allowed to overflow the top and bottom edges of the window. Those parts of the popup can be viewed by manually scrolling the document. This tolerance is a configurable option.
- The top coordinate of the popup will never be negative. This ensures that the top of the popup will not be cut off.
- If centering the popup over an element would cause the overall height of the document to increase, the popup is shifted upwards at most until its top coordinate becomes 0.
Also note that a popup is currently always placed at the center of the window after an orientation change or window resize event.
See methods for information about setting the popup position programmatically, including the option to specify x and y coordinates.
Setting transitions
By default, popups have no transition to make them open as quickly as possible. To set the transition used for a popup, add the data-transition
attribute to the link that references the popup. The reverse version of the transition will be used when closing the popup.
1
2
3
|
|
For performance reasons on mobile devices, we recommend using simpler transitions like pop, fade or none for smooth and fast popup animations, especially with larger or complex widgets within a popup. To view all transition types, you must be on a browser that supports 3D transforms. By default, devices that lack 3D support (such as Android 2.x) will fallback to "fade" for all transition types. See the transitions page for detailed information on the transition system.
When you launch the popup from any of the buttons, the data-transition
set on the button will be used. However, if you launch the popup programmatically, such as via $( "#transitionExample" ).popup( "open" )
, the data-transition
attribute specified in the definition of the popup will be used if present.
Theming the popup and overlay
The popup
plugin provides two theme-related options: data-theme
and data-overlay-theme
. The data-theme
option refers to the theme of the popup itself, whereas data-overlay-theme
refers to the theme of the popup's background, which covers the entire window behind the popup.
data-theme
will be inherited from the page, and will always have a valid value when the popup opens, unless you explicitly specify data-theme="none"
, in which case the popup will have a transparent background.
The data-overlay-theme
will never be set, and the popup's background, although always present when the popup is shown, will be completely transparent, unless explicitly set using for example data-overlay-theme="a"
. In this case, the background will fade in, partially obscuring the rest of the window, to further direct attention to the popup. Here is an example of an explicitly themed popup:
1
2
3
|
|
Note: Chaining of popups not allowed
data-rel="popup"
inside a popup will not do anything at all.This also means that custom select menus will not work inside popups, because they are themselves implemented using popups. If you place a select menu inside a popup, it will be rendered as a native select menu, even if you specify data-native-menu="false"
.
A workaround to get chained popups working is the use of a timeout for example in the popupafterclose
event bound to the invoking popup. In the following example, when the first popup is closed, the second will be opened by a delayed call to the open
method:
1
2
3
4
5
6
7
|
|
Options
corners
true
Sets whether to draw the popup with rounded corners.
This option is also exposed as a data attribute: data-corners="false"
.
Initialize the popup with the corners
option specified:
1
2
3
|
|
Get or set the corners
option, after initialization:
1
2
3
4
5
|
|
dismissible
true
Sets whether clicking outside the popup or pressing Escape while the popup is open will close the popup.
Note: When history support is turned on, pressing the browser's "Back" button will dismiss the popup even if this option is set to false.
This option is also exposed as a data attribute: data-dismissible="false"
.
Initialize the popup with the dismissible
option specified:
1
2
3
|
|
Get or set the dismissible
option, after initialization:
1
2
3
4
5
|
|
history
true
Sets whether to alter the url when a popup is open to support the back button.
This option is also exposed as a data attribute: data-history="false"
.
Initialize the popup with the history
option specified:
1
2
3
|
|
Get or set the history
option, after initialization:
1
2
3
4
5
|
|
initSelector
":jqmData(role='popup')"
This is used to define the selectors (element types, data roles, etc.) that will automatically be initialized as popups. To change which elements are initialized, bind this option to the mobileinit event:
1
2
3
|
|
overlayTheme
null
Sets the color scheme (swatch) for the popup background, which covers the entire window. If not explicitly set, the background will be transparent.
This option is also exposed as a data attribute: data-overlay-theme="a"
.
Initialize the popup with the overlayTheme
option specified:
1
2
3
|
|
Get or set the overlayTheme
option, after initialization:
1
2
3
4
5
|
|
positionTo
"origin"
Sets the element relative to which the popup will be centered. It has the following values:
"origin" |
When the popup opens, center over the coordinates passed to the open() call (see details on this method).
|
"window" |
When the popup opens, center in the window. |
jQuery selector |
When the popup opens, create a jQuery object based on the selector, and center over it. The selector is filtered for elements that are visible with ":visible" . If the result is empty, the popup will be centered in the window.
|
This option is also exposed as a data attribute: data-position-to="window"
.
Initialize the popup with the positionTo
option specified:
1
2
3
|
|
Get or set the positionTo
option, after initialization:
1
2
3
4
5
|
|
shadow
true
Sets whether to draw a shadow around the popup.
This option is also exposed as a data attribute: data-shadow="false"
.
Initialize the popup with the shadow
option specified:
1
2
3
|
|
Get or set the shadow
option, after initialization:
1
2
3
4
5
|
|
theme
null, inherited from parent
Sets the color scheme (swatch) for the popup contents. Unless explicitly set to 'none', the theme for the popup will be assigned the first time the popup is shown by inheriting the page theme or, failing that, by the hard-coded value 'c'. If you set it to 'none', the popup will not have any theme at all, and will be transparent.
Possible values: swatch letter (a-z).
This option is also exposed as a data attribute: data-theme="a"
.
Initialize the popup with the theme
option specified:
1
2
3
|
|
Get or set the theme
option, after initialization:
1
2
3
4
5
|
|
tolerance
"30,15,30,15"
Sets the minimum distance from the edge of the window for the corresponding edge of the popup. By default, the values above will be used for the distance from the top, right, bottom, and left edge of the window, respectively.
You can specify a value for this option in one of four ways:
- Empty string, null, or some other falsey value. This will cause the popup to revert to the above default values.
- A single number. This number will be used for all four edge tolerances.
- Two numbers separated by a comma. The first number will be used for tolerances from the top and bottom edge of the window, and the second number will be used for tolerances from the left and right edge of the window.
- Four comma-separated numbers. The first will be used for tolerance from the top edge, the second for tolerance from the right edge, the third for tolerance from the bottom edge, and the fourth for tolerance from the left edge.
Initialize the popup with the tolerance
option specified:
1
2
3
|
|
Get or set the tolerance
option, after initialization:
1
2
3
4
5
|
|
transition
none
Sets the default transition for the popup. The default value will result in no transition.
If the popup is opened from a link, and the link has the data-transition attribute set, the value specified therein will override the value of this option at the time the popup is opened from the link.
Initialize the popup with the transition
option specified:
1
2
3
|
|
Get or set the transition
option, after initialization:
1
2
3
4
5
|
|
Methods
close()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the close method:
1
|
|
open( options )Returns: jQuery (plugin only)
If x
or y
is missing, and no jQuery selector is given as the value of positionTo
, the middle of the window will be used.
transition
can be used to override the popup's own transition
option. This will result in the popup opening via the transition specified in the call, but the popup's transition
option will not be updated.
Similarly, data-position-to
can be used to override the popup's default positioning without modifying the value of the popup's positionTo
option. The values available for positionTo
are the same as those for the popup's positionTo
option.
-
optionsType: Object
-
x (default:
)
Type: StringThe x-coordinate where the popup is to be displayed. -
y (default:
)
Type: StringThe y-coordinate where the popup is to be displayed. -
transition (default:
"none"
)Type: StringThe transition to use during the opening sequence. -
positionTo (default:
"origin"
)Type: StringThe positioning to use.
-
Invoke the open method:
1
|
|
reposition( options )Returns: jQuery (plugin only)
open()
method for a description of the keys recognized from the options
object.-
optionsType: Object
Invoke the reposition method:
1
|
|
Events
afterclose( event )Type: popupafterclose
Triggered when a popup has completely closed
This event is triggered when the popup has completely disappeared from the screen, meaning that all associated animations have completed.
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the popup with the afterclose callback specified:
1
2
3
|
|
Bind an event listener to the popupafterclose event:
1
|
|
afteropen( event )Type: popupafteropen
Triggered after a popup has completely opened
This event is triggered when the popup has completely appeared on the screen, meaning that all associated animations have completed.
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the popup with the afteropen callback specified:
1
2
3
|
|
Bind an event listener to the popupafteropen event:
1
|
|
beforeposition( event, ui )Type: popupbeforeposition
Triggered before a popup computes the coordinates where it will appear
This event is triggered when the popup has completed preparations for appearing on the screen, when the document is resized and the popup needs to move to another location, or when the reposition()
method is called. At this point the popup has not yet started the opening animations and it has not yet calculated the coordinates where it will appear on the screen. Handling this event gives an opportunity to modify the content of the popup before it appears on the screen. For example, the content can be scaled or parts of it can be hidden or removed if it is too wide or too tall. You can also modify the options
parameter to affect the popup's placement. The properties inside the options
object available for modification are the same as those used by the reposition
method.
Note: The ui
object is empty but included for consistency with other events.
Initialize the popup with the beforeposition callback specified:
1
2
3
|
|
Bind an event listener to the popupbeforeposition event:
1
|
|
create( event, ui )Type: popupcreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the popup with the create callback specified:
1
2
3
|
|
Bind an event listener to the popupcreate event:
1
|
|
Example:
A basic example of a Popup.
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
|
|