AMP

amp-position-observer

描述

监视元素在用户滚动时在视口中的位置,并分派可与其他 AMP 组件一起使用的事件。

 

必需的脚本

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

支持的布局

用法

amp-position-observer 组件监视元素在用户滚动时在视口中的位置,并分派 enterexitscroll:<Position In Viewport As a Percentage> 事件(低信任级别),这些事件可用于触发其他组件上的操作(仅限低信任操作)(例如,amp-animation)。

amp-position-observer 组件仅在与其他组件一起使用时才有用,本身没有任何作用。

目前,amp-animation 和 AMP 中的几个视频播放器是仅有的允许低信任事件触发其操作的组件(例如,启动动画、查找动画中的位置、暂停视频等)。

滚动绑定动画

amp-animation 组件公开了一个 seekTo 操作,可以将其绑定到 amp-position-observerscroll 事件以实现滚动绑定动画。

示例:动画随用户滚动旋转

想象一下,当用户滚动页面时,时钟的时针旋转的动画。


<!-- 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-position-observer to tie the movement of the clock scene within
    the viewport to the timeline of the animation
  -->
  <amp-position-observer
    intersection-ratios="1"
    on="scroll:clockAnim.seekTo(percent=event.percent)"
    layout="nodisplay"
  >
  </amp-position-observer>
  <amp-img layout="responsive" width="2" height="1.5" src="./img/clock.jpg">
    <div class="clock-hand"></div>
  </amp-img>
</div>

根据视口中的可见性开始/暂停的动画场景

amp-animation 组件还公开了 startpause 操作,可以将其绑定到 amp-position-observerenterexit 事件,以根据可见性控制动画的开始/暂停。

amp-position-observer 组件公开了各种可见性配置,例如 intersection-ratiosviewport-margins(类似于 IntersectionObserver),可用于微调何时认为目标可见。

示例:动画根据可见性开始和暂停

考虑相同的时钟动画,但这次指针随时间动画,除非我们希望当钟表至少 50% 可见时动画开始,并在钟表变为小于 50% 可见时暂停。


<!-- An animation that rotates a clock hand 180 degrees. -->
<!--
   Note that we are NOT setting `trigger=visibility`
   since we will manually trigger the animation
-->
<!--
   Also note that this is the same animation as the scroll-bound version above
   the animation is the same, just the triggering mechanism with
   `amp-position-observer` is different!
-->
<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-position-observer to tie the start/pause of the animation with
    the visibility of the scene.
  -->
  <amp-position-observer
    intersection-ratios="0.5"
    on="enter:clockAnim.start;exit:clockAnim.pause"
    layout="nodisplay"
  >
  </amp-position-observer>

  <amp-img layout="responsive" width="2" height="1.5" src="./img/clock.jpg">
    <div class="clock-hand"></div>
  </amp-img>
</div>

基于滚动和可见性的动画的辅助功能注意事项

一般来说,动画可能会给某些用户群体带来问题。特别是滚动绑定和视差动画可能会对患有前庭疾病的用户产生问题。请务必查看针对 amp-animation 的辅助功能注意事项中提供的建议。

属性

target(可选)

指定要观察的元素的 ID。如果未指定,则将 <amp-position-observer> 的父级用作目标。

intersection-ratios(可选)

定义在 <amp-position-observer> 触发其任何事件之前,目标在视口中应可见的程度。该值是 0 到 1 之间的数字(默认为 0)。

您可以通过提供两个值(<顶部> <底部>)来指定顶部与底部的不同比率。

  • `intersection-ratios="0"` 表示当目标的单个像素进入视口时,会触发 `enter`,当目标的最后一个像素离开视口时,会触发 `exit`。
  • `intersection-ratios="0.5"` 表示当 50% 的目标进入视口时,会触发 `enter`,当小于 50% 的目标在视口中时,会触发 `exit`。
  • `intersection-ratios="1"` 表示当目标完全可见时,会触发 `enter`,当单个像素离开视口时,会触发 `exit`。
  • `intersection-ratios="0 1"` 根据目标是从顶部(将使用 0)还是底部(将使用 1)进入/退出,使条件不同。

viewport-margins(可选)

一个 pxvh 值,可用于缩小用于可见性计算的视口区域。不带单位的数字将被假定为 px。默认为 0。

您可以通过提供两个值(<顶部> <底部>)来指定顶部与底部的不同值。

  • `viewport-margins="100px"` 表示从顶部和底部将视口缩小 100px。
  • `viewport-margins="25vh"` 表示从顶部和底部将视口缩小 25%。有效地仅考虑视口的中间 50%。
  • `viewport-margins="100px 10vh"` 表示从顶部将视口缩小 100px,从底部缩小 10%。

once(可选)

仅触发一次 enterexit 事件。scroll 事件也只执行一次迭代。

属性的存在表示 true 值,属性的缺失表示 false 值。如果属性存在,则其值必须为空字符串、once 或未分配。

验证

请参阅 AMP 验证器规范中的 amp-position-observer 规则

需要更多帮助?

您已阅读本文档十几次,但它并没有真正涵盖您所有的问题?也许其他人也有同样的感觉:请在 Stack Overflow 上联系他们。

转到 Stack Overflow
发现错误或缺少功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的持续参与者,但也欢迎您针对您特别关注的问题进行一次性贡献。

转到 GitHub