Pull to Refresh - 图1

Pull To Refresh

Pull to refresh is a special component that can be used to initiate the refreshing of a page’s contents.

Pull To Refresh Layout

Let’s look on how to integrate pull to refresh to page:

  1. <div class="page">
  2. <!-- Page content must have additional "ptr-content" class -->
  3. <div class="page-content ptr-content" data-ptr-distance="55">
  4. <!-- Default pull to refresh preloader-->
  5. <div class="ptr-preloader">
  6. <div class="preloader"></div>
  7. <div class="ptr-arrow"></div>
  8. </div>
  9. <!-- usual content below -->
  10. <div class="list">
  11. ...
  12. </div>
  13. <!-- nested scrollable element -->
  14. <div class="my-scolling-content ptr-watch-scrollable">
  15. ...
  16. </div>
  17. </div>
  18. </div>

Where:

  • **ptr-content** class. This is required to enable pull to refresh
  • **div class="ptr-preloader"** hidden layer with pull to refresh visual elements: preloader and arrow
  • data-ptr-distance="55" additional attribute that allows to set custom pull to refresh trigger distance. By default (if not specified) it is 44px
  • ptr-watch-scrollable - additional class that must be added on nested scrollable elements to prevent pull to refresh on such elements scrolling

Pull To Refresh Sequence

When user starts to pull ptr-content down, then ptr-preloader will receive additional **ptr-pull-down** class.

When user pulls down ptr-content on a distance more than 44px (when ptr-preloader will be fully visible), then ptr-preloader will receive additional **ptr-pull-up** class which changes arrow rotation to notify user about refresh action on release.

When user release pull to refresh content when it is in “ptr-pull-up” state, then ptr-preloader will receive additional **ptr-refreshing** class. In “refreshing” state arrow will be hidden and user will see preloader indicator. On this stage you probably need to do Ajax request and refresh page content.

Pull To Refresh App Methods

There are few App’s methods that can be used with pull to refresh container:

app.ptr.create(el)- initialise PTR on specified HTML element container.

  • el - HTMLElement or string (with CSS selector) - PTR element (ptr-content). Required.

Method returns created PTR instance

Use this method only in case you have added ptr content after page init or if you want to enable it later. Otherwise if there is “ptr-content” element on page init it will be created automatically

app.ptr.destroy(el)- remove PTR event listeners from the specified HTML element

  • el - HTMLElement or string (with CSS selector) - PTR element (ptr-content). Required.

app.ptr.get(el)- get PTR instance by HTML element

  • el - HTMLElement or string (with CSS Selector). PTR element (ptr-content).

Method returns PTR instance

app.ptr.done(el)- reset PTR state on specified PTR content element

  • el - HTMLElement or string (with CSS Selector). PTR element (ptr-content). Required.

app.ptr.refresh(el)- trigger PTR on specified PTR content element

  • el - HTMLElement or string (with CSS Selector). PTR element (ptr-content). Required.

Pull To Refresh Methods & Properties

If we created PTR manually or used app.ptr.get method we will PTR initialized instance with useful methods and properties:

  1. // init ptr manually
  2. var ptr = app.ptr.create('.ptr-content');
  3. // or using get to retreive already created instance
  4. var ptr = app.ptr.get('.ptr-content');
Properties
ptr.appLink to global app instance
ptr.elPTR HTML element (ptr-content)
ptr.$elDom7 instance with PTR HTML element (ptr-content)
Methods
ptr.done()Reset PTR state
ptr.refresh()Trigger PTR
ptr.destroy()Destroy PTR instance and remove PTR event listeners from the specified HTML element

Pull To Refresh Events

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

DOM Events

EventTargetDescription
ptr:pullstartPull To Refresh content<div class=”ptr-content”>Event will be triggered when you start to move pull to refresh content
ptr:pullmovePull To Refresh content<div class=”ptr-content”>Event will be triggered during you move pull to refresh content
ptr:pullendPull To Refresh content<div class=”ptr-content”>Event will be triggered when you release pull to refresh content
ptr:refreshPull To Refresh content<div class=”ptr-content”>Event will be triggered when pull to refresh enters in “refreshing” state. event.detail contain ptr.done method to reset its state after loading completed
ptr:donePull To Refresh content<div class=”ptr-content”>Event will be triggered after pull to refresh is done and it is back to initial state (after calling ptr.done method)
ptr:beforedestroyPull To Refresh content<div class=”ptr-content”>Event will be triggered right before PTR instance will be destroyed

App and Pull To Refresh Instance Events

PTR instance emits events on both self instance and app instance. App instance events has same names prefixed with ptr.

EventTargetArgumentsDescription
pullStartptr(el)Event will be triggered when you start to move pull to refresh content. As an argument event handler receives ptr element
ptrPullStartapp(el)
pullMoveptr(el, data)Event will be triggered when you start to move pull to refresh content. As an argument event handler receives ptr element and ptr data containing the following properties:
  • event - touchmove event
  • scrollTop - current scroll top position
  • translate - current translateY offset
  • touchesDiff - touches difference (in px)
ptrPullMoveapp(el, data)
pullEndptr(el, data)Event will be triggered when you release pull to refresh content. As an argument event handler receives ptr element
ptrPullEndapp(el, data)
refreshptr(el, done)Event will be triggered when pull to refresh enters. As an argument event handler receives ptr element and done function to reset PTR state
ptrRefreshapp(el, done)
doneptr(el)Event will be triggered right before PTR instance will be destroyed. As an argument event handler receives ptr element
ptrDoneapp(el)
beforeDestroyptr(ptr)Event will be triggered right before PTR instance will be destroyed. As an argument event handler receives PTR instance
ptrBeforeDestroyapp(ptr)

Example

  1. <div class="page-content ptr-content">
  2. <div class="ptr-preloader">
  3. <div class="preloader"></div>
  4. <div class="ptr-arrow"></div>
  5. </div>
  6. <div class="list media-list">
  7. <ul>
  8. <li class="item-content">
  9. <div class="item-media"><img src="http://lorempixel.com/88/88/abstract/1" width="44"/></div>
  10. <div class="item-inner">
  11. <div class="item-title-row">
  12. <div class="item-title">Yellow Submarine</div>
  13. </div>
  14. <div class="item-subtitle">Beatles</div>
  15. </div>
  16. </li>
  17. <li class="item-content">
  18. <div class="item-media"><img src="http://lorempixel.com/88/88/abstract/2" width="44"/></div>
  19. <div class="item-inner">
  20. <div class="item-title-row">
  21. <div class="item-title">Don't Stop Me Now</div>
  22. </div>
  23. <div class="item-subtitle">Queen</div>
  24. </div>
  25. </li>
  26. <li class="item-content">
  27. <div class="item-media"><img src="http://lorempixel.com/88/88/abstract/3" width="44"/></div>
  28. <div class="item-inner">
  29. <div class="item-title-row">
  30. <div class="item-title">Billie Jean</div>
  31. </div>
  32. <div class="item-subtitle">Michael Jackson</div>
  33. </div>
  34. </li>
  35. </ul>
  36. </div>
  37. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Dummy Content
  4. var songs = ['Yellow Submarine', 'Don\'t Stop Me Now', 'Billie Jean', 'Californication'];
  5. var authors = ['Beatles', 'Queen', 'Michael Jackson', 'Red Hot Chili Peppers'];
  6. // Pull to refresh content
  7. var $ptrContent = $$('.ptr-content');
  8. // Add 'refresh' listener on it
  9. $ptrContent.on('ptr:refresh', function (e) {
  10. // Emulate 2s loading
  11. setTimeout(function () {
  12. var picURL = 'http://lorempixel.com/88/88/abstract/' + Math.round(Math.random() * 10);
  13. var song = songs[Math.floor(Math.random() * songs.length)];
  14. var author = authors[Math.floor(Math.random() * authors.length)];
  15. var itemHTML =
  16. '<li class="item-content">' +
  17. '<div class="item-media"><img src="' + picURL + '" width="44"/></div>' +
  18. '<div class="item-inner">' +
  19. '<div class="item-title-row">' +
  20. '<div class="item-title">' + song + '</div>' +
  21. '</div>' +
  22. '<div class="item-subtitle">' + author + '</div>' +
  23. '</div>' +
  24. '</li>';
  25. // Prepend new list element
  26. $ptrContent.find('ul').prepend(itemHTML);
  27. // When loading done, we need to reset it
  28. app.ptr.done(); // or e.detail();
  29. }, 2000);
  30. });