Popup - 图1

Popup

Popup is a popup window with any HTML content that pops up over App’s main content. Popup as all other overlays is part of so called “Temporary Views”.

Popup Layout

Popup layout is pretty simple:

  1. <body>
  2. ...
  3. <div class="popup">
  4. Any HTML content goes here
  5. </div>
  6. </body>

Popup Size

By default Popup has a bit different size on phones and tablets (iPad). On phones it is fullscreen while on tablets it is 630px width and height. If you want to make it fullscreen size on tablets, you should add additional “popup-tablet-fullscreen“ class to the required popup:

  1. <body>
  2. ...
  3. <!-- This popup has fullscreen size on tablets -->
  4. <div class="popup popup-tablet-fullscreen">
  5. Any HTML content goes here
  6. </div>
  7. </body>

Popup App Methods

Let’s look at related App methods to work with Popup:

app.popup.create(parameters)- create Popup instance

  • parameters - object. Object with popup parameters

Method returns created Popup’s instance

app.popup.destroy(el)- destroy Popup instance

  • el - HTMLElement or string (with CSS Selector) or object. Popup element or Popup instance to destroy.

app.popup.get(el)- get Popup instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Popup element.

Method returns Popup’s instance

app.popup.open(el, animate)- opens Popup

  • el - HTMLElement or string (with CSS Selector). Popup element to open.
  • animate - boolean. Open Popup with animation.

Method returns Popup’s instance

app.popup.close(el, animate)- closes Popup

  • el - HTMLElement or string (with CSS Selector). Popup element to close.
  • animate - boolean. Close Popup with animation.

Method returns Popup’s instance

Popup Parameters

Now let’s look at list of available parameters we need to create Popup:

ParameterTypeDefaultDescription
elHTMLElementPopup element. Can be useful if you already have Popup element in your HTML and want to create new instance using this element
contentstringFull Popup HTML layout string. Can be useful if you want to create Popup element dynamically
backdropbooleantrueEnables Popup backdrop (dark semi transparent layer behind)
closeByBackdropClickbooleantrueWhen enabled, popup will be closed on backdrop click
animatebooleantrueWhether the Popup should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
onobjectObject with events handlers. For example:
  1. var popup = app.popup.create({
  2. content: ‘<div class=”popup”>…</div>’,
  3. on: {
  4. opened: function () {
  5. console.log(‘Popup opened’)
  6. }
  7. }
  8. })

Note that all following parameters can be used in global app parameters under popup property to set defaults for all popups. For example:

  1. var app = new Framework7({
  2. popup: {
  3. closeByBackdropClick: false,
  4. }
  5. });

Popup Methods & Properties

So to create Popup we have to call:

  1. var popup = app.popup.create({ /* parameters */ })

After that we have its initialized instance (like popup variable in example above) with useful methods and properties:

Properties
popup.appLink to global app instance
popup.elPopup HTML element
popup.$elDom7 instance with popup HTML element
popup.backdropElBackdrop HTML element
popup.$backdropElDom7 instance with backdrop HTML element
popup.paramsPopup parameters
popup.openedBoolean prop indicating whether popup is opened or not
Methods
popup.open(animate)Open popup. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation
popup.close(animate)Close popup. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation
popup.destroy()Destroy popup
popup.on(event, handler)Add event handler
popup.once(event, handler)Add event handler that will be removed after it was fired
popup.off(event, handler)Remove event handler
popup.off(event)Remove all handlers for specified event
popup.emit(event, …args)Fire event on instance

Control Popup With Links

It is possible to open and close required popup (if you have them in DOM) using special classes and data attributes on links:

  • To open popup we need to add “popup-open“ class to any HTML element (prefered to link)

  • To close popup we need to add “popup-close“ class to any HTML element (prefered to link)

  • If you have more than one popup in DOM, you need to specify appropriate popup via additional data-popup=”.my-popup” attribute on this HTML element

According to above note:

  1. <!-- In data-popup attribute we specify CSS selector of popup we need to open -->
  2. <p><a href="#" data-popup=".my-popup" class="popup-open">Open Popup</a></p>
  3. <!-- And somewhere in DOM -->
  4. <div class="popup my-popup">
  5. <div class="view">
  6. <div class="page">
  7. <div class="navbar">
  8. <div class="navbar-inner">
  9. <div class="title">Popup</div>
  10. <div class="right">
  11. <!-- Link to close popup -->
  12. <a class="link popup-close">Close</a>
  13. </div>
  14. </div>
  15. </div>
  16. <div class="page-content">
  17. ...
  18. </div>
  19. </div>
  20. </div>
  21. ...
  22. </div>

Popup Events

Popup will fire the following DOM events on popup element and events on app and popup instance:

DOM Events

EventTargetDescription
popup:openPopup Element<div class=”popup”>Event will be triggered when Popup starts its opening animation
popup:openedPopup Element<div class=”popup”>Event will be triggered after Popup completes its opening animation
popup:closePopup Element<div class=”popup”>Event will be triggered when Popup starts its closing animation
popup:closedPopup Element<div class=”popup”>Event will be triggered after Popup completes its closing animation

App and Popup Instance Events

Popup instance emits events on both self instance and app instance. App instance events has same names prefixed with popup.

EventArgumentsTargetDescription
openpopuppopupEvent will be triggered when Popup starts its opening animation. As an argument event handler receives popup instance
popupOpenpopupapp
openedpopuppopupEvent will be triggered after Popup completes its opening animation. As an argument event handler receives popup instance
popupOpenedpopupapp
closepopuppopupEvent will be triggered when Popup starts its closing animation. As an argument event handler receives popup instance
popupClosepopupapp
closedpopuppopupEvent will be triggered after Popup completes its closing animation. As an argument event handler receives popup instance
popupClosedpopupapp
beforeDestroypopuppopupEvent will be triggered right before Popup instance will be destroyed. As an argument event handler receives popup instance
popupBeforeDestroypopupapp

Examples

  1. <body>
  2. ...
  3. <div class="page-content">
  4. <div class="block">
  5. <!-- Open About Popup -->
  6. <p><a class="link popup-open" href="#" data-popup=".popup-about">Open About Popup</a></p>
  7. <!-- Open Services Popup -->
  8. <p><a class="link popup-open" href="#" data-popup=".popup-services">Open Services Popup</a></p>
  9. <p><a class="link dynamic-popup" href="#">Create Dynamic Popup</a></p>
  10. </div>
  11. </div>
  12. ...
  13. <div class="popup popup-about">
  14. <div class="block">
  15. <p>About</p>
  16. <!-- Close Popup -->
  17. <p><a class="link popup-close" href="#">Close popup</a></p>
  18. <p>Lorem ipsum dolor sit amet...</p>
  19. </div>
  20. </div>
  21. <div class="popup popup-services">
  22. <div class="block">
  23. <p>Services</p>
  24. <!-- Close Popup -->
  25. <p><a class="link popup-close" href="#">Close popup</a></p>
  26. <p>Lorem ipsum dolor sit amet...</p>
  27. </div>
  28. </div>
  29. </body>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // DOM events for About popup
  4. $$('.popup-about').on('popup:open', function (e, popup) {
  5. console.log('About popup open');
  6. });
  7. $$('.popup-about').on('popup:opened', function (e, popup) {
  8. console.log('About popup opened');
  9. });
  10. // Create dynamic Popup
  11. var dynamicPopup = app.popup.create({
  12. content: '<div class="popup">'+
  13. '<div class="block">'+
  14. '<p>Popup created dynamically.</p>'+
  15. '<p><a href="#" class="link popup-close">Close me</a></p>'+
  16. '</div>'+
  17. '</div>',
  18. // Events
  19. on: {
  20. open: function (popup) {
  21. console.log('Popup open');
  22. },
  23. opened: function (popup) {
  24. console.log('Popup opened');
  25. },
  26. }
  27. });
  28. // Events also can be assigned on instance later
  29. dynamicPopup.on('close', function (popup) {
  30. console.log('Popup close');
  31. });
  32. dynamicPopup.on('closed', function (popup) {
  33. console.log('Popup closed');
  34. });
  35. // Open dynamic popup
  36. $$('.dynamic-popup').on('click', function () {
  37. dynamicPopup.open();
  38. });