AMP

重要提示:此文档不适用于你当前选择的格式 email

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 组件监视设备的朝向,并分派低信任级别的事件 (alpha, beta, gamma),报告设备沿 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