重要事項:本文件不適用於您目前選取的格式 email!
amp-orientation-observer
描述
監控 viewport 中元素的方位,並分派可與其他 AMP 元件搭配使用的事件。
必要腳本
<script async custom-element="amp-orientation-observer" src="https://cdn.ampproject.org/v0/amp-orientation-observer-0.1.js"></script>
支援版面配置
用法
amp-orientation-observer
元件會監控裝置的方位,並分派低信任度層級事件 (alpha
、beta
、gamma
),以 angle
和 percent
為單位回報裝置方位沿 alpha
、beta
和 gamma
軸的變更。這些事件可用於觸發其他元件 (例如 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"
)。依預設,相關動作會針對 0
到 360 度
之間的所有變更觸發。
beta-range (選填)
指定關聯動作僅應在 x 軸上指定範圍內的變更時發生。指定為以空格分隔的 2 個值清單 (例如,beta-range="0 180"
)。依預設,相關動作會針對 0
到 360 度
之間的所有變更觸發。
範例:使用 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"
。依預設,相關動作會針對 0
到 360 度
之間的所有變更觸發。
smoothing (選填)
啟用時,會輸出最後 n
個值的移動平均值,而不是從感應器讀取的原始值。依預設,啟用時,平滑處理會設定為 4 點。
活動
以下是 amp-orientation-observer
元件分派的低信任度事件
Alpha
代表裝置繞 z 軸的運動。
Beta
代表裝置繞 x 軸的運動。
Gamma
代表裝置繞 y 軸的運動。這代表裝置由左至右的運動。
驗證
請參閱 AMP 驗證器規格中的 amp-orientation-observer 規則。
您已閱讀本文件十幾次,但它並未真正涵蓋您的所有問題?可能其他人也有同樣的感受:請在 Stack Overflow 上與他們聯繫。
前往 Stack Overflow 發現錯誤或缺少功能?AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎針對您特別熱衷的問題提供一次性貢獻。
前往 GitHub