ons-pull-hook

実例

Pull to refresh

The <ons-pull-hook> is used to perform an action when the user pulls down the page. It’s a common design pattern in mobile apps.

It is normally put as a direct descendant to the <ons-page> element.

<ons-page>
  <ons-toolbar>
    ...
  </ons-toolbar>

  <ons-pull-hook></ons-pull-hook>
</ons-page>

Setting the callback

To set the callback that should be executed when the user pulls the page you can modify the onAction property.

The callback will take a function as an argument that needs to be called when the action is performed. Calling the function will cause the page to snap back into position.

var pullHook = document.getElementById('pull-hook');
pullHook.onAction = function(done) {
  refreshTheData()
    .then(done);
};

States

The <ons-pull-hook> has three different states:

  • initial - Initial state.
  • preaction - Will run the callback when released.
  • action - Currently running the callback.

The element will throw the DOM event changestate when it transitions between any of these states.

pullHook.addEventListener('changestate', function(event) {
  console.log('Changed to state: ' + event.state);
});

Listening to these state changes can be used to change the content of the element to give the user information about what is happening.

Styling with CSS

Another way to change the content or the appearance of the <ons-pull-hook> element is to use CSS and attribute selectors.

The element has the attribute style which will have the value of the current state:

ons-pull-hook[style="initial"] {
  // Write some style for initial state here.
}

...

Customizing the behavior

There are a number of attributes that can be used to configure how the <ons-pull-hook> will behave.

The height attribute defines how long the component must be pulled before transitioning to the preaction state. With the threshold-height attribute the component can be configured to transition directly to the action state without releasing.

There is also an attribute called fixed-content that prevents the page from moving while scrolling.

Additionally, the onPull DOM prop provides a way to gradually modify the appearance of the pull-hook when the user pulls it. This could be useful, for example, to place a <ons-progress-circular> element as the visual part of <ons-pull-hook> and change its progress value as the user pulls.

名前 型 / デフォルト値 概要
disabled この属性がある時、disabled状態になりアクションが実行されなくなります Optional.
height String コンポーネントの高さを指定します。この高さ以上にpull downすると”preaction”状態に移行します。デフォルトの値は”64px”です。 Optional.
threshold-height String 閾値となる高さを指定します。この値で指定した高さよりもpull downすると、このコンポーネントは自動的に”action”状態に移行します。 Optional.
fixed-content この属性がある時、プルフックが引き出されている時にもコンテンツは動きません。 Optional.
名前 概要
fixedContent この属性がある時、プルフックが引き出されている時にもコンテンツは動きません。
onAction This will be called in the action state if it exists. The function will be given a done callback as its first argument. (翻訳中)
onPull Hook called whenever the user pulls the element. It gets the pulled distance ratio (scroll / height) and an animationOptions object as arguments. (翻訳中)
height The height of the pull hook in pixels. The default value is 64px. (翻訳中)
thresholdHeight The thresholdHeight of the pull hook in pixels. The default value is 96px. (翻訳中)
state Current state of the element. (翻訳中)
pullDistance 現在のプルフックが引き出された距離をピクセル数。
disabled 無効化されている場合にtrue
名前 概要
changestate コンポーネントの状態が変わった場合に発火します。状態は、”initial”, “preaction”, “action”のいずれかです。
pull Fired when the pull hook is pulled. (翻訳中)
changestate

コンポーネントの状態が変わった場合に発火します。状態は、”initial”, “preaction”, “action”のいずれかです。

パラメーター
名前 概要
event Object イベントオブジェクト。
event.pullHook Object コンポーネントのオブジェクト。
event.state String 現在の状態名を参照できます。
pull

Fired when the pull hook is pulled. (翻訳中)

パラメーター
名前 概要
event Object イベントオブジェクト。
event.ratio Object The pulled distance ratio (scroll / height). (翻訳中)
event.animationOptions String The animation options object. (翻訳中)

お困りですか?

Onsen UIに関する質問は、Stack Overflowにてonsen-uiタグを付与してください。Onsen UIチームはあなたの問題解決をお手伝いします。

バグ報告や機能要望については、GitHub Issuesに記載をお願いいたします。

あわせて、下記の情報も参考にしてください。