Search code examples
htmlcssamp-html

how to amp bind caption in amp-carousel?


I need a carousel headline news.(some images in slider with text).

this is an official example:

<amp-carousel controls
  type="slides"
  width="400"
  height="300"
  [slide]="selectedSlide"
  on="slideChange:AMP.setState({ selectedSlide: event.index })">
  <amp-img src="/static/samples/img/image1.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image2.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image3.jpg"
    layout="fill"></amp-img>
</amp-carousel>

<h1>Selected slide: <span [text]="+selectedSlide + 1">1</span>/3</h1>

how can set the caption of each image in [text] instead selectedSlide?


Solution

  • I tried this code in AMP playground and i can see Selected slide text changing fine as - on render - Selected slide: 1/3 on first slide - Selected slide: 2/3 on second slide - Selected slide: 3/3

    I am not sure but here code i tested, check if you missed amp-bind script in head?

    <!doctype html>
    <html ⚡>
    <head>
    <meta charset="utf-8">
      <title>amp-carousel</title>
      <script async src="https://cdn.ampproject.org/v0.js"></script>
    
      <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
      <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
      <link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-carousel/index.html">
      <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
      <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
    
    </head>
    <body>
    
    
     <amp-carousel controls
      type="slides"
      width="400"
      height="300"
      [slide]="selectedSlide"
      on="slideChange:AMP.setState({ selectedSlide: event.index })">
      <amp-img src="/static/samples/img/image1.jpg"
        layout="fill"></amp-img>
      <amp-img src="/static/samples/img/image2.jpg"
        layout="fill"></amp-img>
      <amp-img src="/static/samples/img/image3.jpg"
        layout="fill"></amp-img>
    </amp-carousel>
    
    <h1>Selected slide: <span [text]="+selectedSlide + 1">1</span>/3</h1>
    
    
    </body></html>