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:<视口中位置的百分比> 事件(低信任级别),这些事件可用于触发其他组件上的操作(仅限低信任操作)(例如,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)。

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

  • `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。

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

  • `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