Do you build things with AMP? Fill out the new AMP Developer Survey!
AMP

amp-pan-zoom

Description

Provides zooming and panning for arbitrary content.

Required Scripts

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

Supported Layouts

Behavior

The <amp-pan-zoom> component takes one child of arbitrary content and enables the ability for the user to zoom and pan the content via double tap or pinch-to-zoom actions. Tap events registered on the zoomable content or its children will trigger after a 300ms delay.

Usage

<amp-layout layout="responsive" width="4" height="3">
  <amp-pan-zoom layout="fill">
    <svg>
      ...
    </svg>
  </amp-pan-zoom>
</amp-layout>

Attributes

max-scale (optional) Specifies a max zoom scale, which should be a positive number from 1 - 9. The default value is 3.
initial-scale (optional) Specifies a default zoom scale, which should be a positive number from 1 - 9. The default value is 1.
initial-x, initial-y (optional) Specifies default translation coordinates, otherwise both are set to 0. The value is expected to be a whole number.
reset-on-resize (optional) Refers to the ability to center the image and set the image's scale back to 1. Setting this attribute causes the component to reset the zoomable content on resize of the image itself.
controls (optional) Shows default controls (zoom in / zoom out button) which can be customized via public CSS classes.
common attributes This element includes common attributes extended to AMP components.

Events and actions

transformEnd (event)

The <amp-pan-zoom> component triggers the transformEnd event whenever the pan or zoom animation is complete. This event emits the scale, x, and y parameters. The scale parameter contains the current scale of the child content being zoomed. The x and y parameters contain the x and y translation of the child content from center in pixels, respectively.

Example

This example contains an amp-pan-zoom component that will update amp-state on transformEnd.

<amp-state id="transform">
  <script type="application/json">
    {
      "scale": 1,
      "y": 0,
      "x": 0
    }
  </script>
</amp-state>
<p
  [text]="'Current scale: ' + transform.scale + ', x: ' + transform.x + ', y: ' + transform.y"
>
  Current scale: 1
</p>
<amp-pan-zoom
  layout="responsive"
  width="1"
  height="1"
  id="pan-zoom"
  on="transformEnd:AMP.setState({transform: {scale: event.scale, x: event.x, y: event.y}})"
>
  ...
</amp-pan-zoom>

transform (action)

The transform action takes scale, x, y as parameters and sets the CSS transform property of the child content. If no x or y value is specified, the content zooms to center.

Example

Assuming that there is an <amp-pan-zoom> component with the id pan-zoom on the page, a button with on="tap:pan-zoom.transform(scale=3)" will zoom to scale 3x at the center of the content, a button with on="tap:pan-zoom.transform(scale=3, x=50, y=10)" will first scale the content to 3x scale, and then shift the content 50 pixels towards the left, and 10 pixels upwards. Consider the scale, x, and y attributes directly applied to the content's CSS transform attribute after animation.

Customizing Buttons

The following public CSS classes are exposed to allow customization for the zoom buttons:

.amp-pan-zoom-button .amp-pan-zoom-in-icon .amp-pan-zoom-out-icon;

Use .amp-pan-zoom-button to customize the dimensions, positioning, background-color, border-radius of all buttons. Use .amp-pan-zoom-in-icon to customize the icon for the zoom in button. Use .amp-pan-zoom-out-icon to customize the icon for the zoom out button. You can also hide these buttons entirely and create your own using the transform action. To hide them, just apply

.amp-pan-zoom-button {
  display: none;
}

Validation

See amp-pan-zoom rules in the AMP validator specification.

Eligibile children tags

See the list of eligibles children tags of amp-pan-zoom.

需要更多帮助?

您多次阅读的资料未能涵盖您的所有问题?也许其他人也这么觉得 在 Stack Overflow 上与他们取得联系。

前往 Stack Overflow
找到一个 bug 或者缺失某项功能?

AMP 项目强烈鼓励您参与其中并作出贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您对热衷问题做出单次贡献。

前往 GitHub