AMP

重要事項:本文件不適用於您目前選取的格式 廣告

amp-orientation-observer

說明

監控使用者捲動時,視窗內元素的方位,並派發可與其他 AMP 元件搭配使用的事件。

 

必要指令碼

<script async custom-element="amp-orientation-observer" src="https://cdn.ampproject.org/v0/amp-orientation-observer-0.1.js"></script>

支援的版面配置

用法

amp-orientation-observer 元件會監控裝置的方位,並派發低信任層級事件 (alphabetagamma),以回報裝置方位沿著 alphabetagamma 軸線在 anglepercent 方面的變化。這些事件可用於觸發其他元件 (例如 amp-animation) 的動作 (僅限低信任動作)。

amp-orientation-observer 元件只有與其他元件搭配使用時才有用,本身不會執行任何動作。

目前,amp-animation 和 AMP 中的數個影片播放器是唯一允許低信任事件觸發其動作的元件 (例如,開始動畫、在動畫中搜尋位置、暫停影片等)。

捲動綁定的動畫

想像一下,當使用者捲動頁面時,時鐘的時針會旋轉的動畫。

<!-- An animation that rotates a clock hand 180 degrees. -->
<!--
   Note that we are NOT setting `trigger=visibility`
   since we will manually trigger the animation.
-->
<amp-animation id="clockAnim" layout="nodisplay">
  <script type="application/json">
    {
      "duration": "3s",
      "fill": "both",
      "direction": "alternate",
      "animations": [
        {
          "selector": "#clock-scene .clock-hand",
          "keyframes": [
            {"transform": "rotate(-180deg)"},
            {"transform": "rotate(0deg)"}
          ]
        }
      ]
    }
  </script>
</amp-animation>

<!-- The clock container -->
<div id="clock-scene">
  <!--
    Use amp-orientation-observer to tie the movement of the clock scene within
    the viewport to the timeline of the animation
  -->
  <amp-orientation-observer
    on="beta:clockAnim1.seekTo(percent=event.percent)"
    layout="nodisplay"
  >
  </amp-orientation-observer>
  <amp-img layout="responsive" width="2" height="1.5" src="./img/clock.jpg">
    <div class="clock-hand"></div>
  </amp-img>
</div>

屬性

alpha-range (選填)

指定關聯動作應僅針對沿著 z 軸的指定範圍之間的變更執行。指定為以空格分隔的 2 個值清單 (例如,alpha-range="0 180")。依預設,相關動作會針對 0360 度之間的所有變更觸發。

beta-range (選填)

指定關聯動作應僅針對沿著 x 軸的指定範圍之間的變更執行。指定為以空格分隔的 2 個值清單 (例如,beta-range="0 180")。依預設,相關動作會針對 0360 度之間的所有變更觸發。

範例:使用 beta-range 限制要沿著 x 軸觀看的角度範圍

想像一下,當使用者捲動頁面時,時鐘的時針會旋轉的動畫。

<amp-orientation-observer
  beta-range="0 180"
  on="beta:clockAnim1.seekTo(percent=event.percent)"
  layout="nodisplay"
>
</amp-orientation-observer>

gamma-range (選填)

指定關聯動作應僅針對沿著 y 軸的指定範圍之間的變更執行。指定為以空格分隔的 2 個值清單 (例如,gamma-range="0 90"。依預設,相關動作會針對 0360 度之間的所有變更觸發。

smoothing (選填)

啟用時,會輸出最後 n 個值的移動平均值,而不是從感應器讀取的原始值。依預設,啟用時,平滑處理會設定為 4 個點。

活動

以下是 amp-orientation-observer 元件派發的低信任層級事件

alpha

代表裝置繞 z 軸的運動。

beta

代表裝置繞 x 軸的運動。

gamma

代表裝置繞 y 軸的運動。這代表裝置從左到右的運動。

驗證

請參閱 AMP 驗證器規格中的 amp-orientation-observer 規則

需要更多協助嗎?

您已閱讀本文件十幾次,但它仍然沒有真正涵蓋您的所有問題?或許其他人也有同感:在 Stack Overflow 上與他們聯繫。

前往 Stack Overflow
發現錯誤或缺少功能?

AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別熱衷的問題提供一次性貢獻。

前往 GitHub