AMP

Important: this documentation is not applicable to your currently selected format email!

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

Usage

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.

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

Valid children tags

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

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.

Actions

transform

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.

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.

Events

transformEnd

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.

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>

Styling

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.

도움이 더 필요하신가요?

이 문서를 수십 번 읽었음에도 여전히 궁금한 점이 남아 있나요? 다른 개발자들도 같은 심정일지 모릅니다. Stack Overflow에서 소통해 보세요.

Stack Overflow로 이동
버그나 누락된 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub로 이동하기